这可能是一个非常基本的问题,但我还没有弄清楚如何在 mvc 应用程序中调用简单的 wcf 服务。 我添加了服务引用,最后我希望使用服务引用名称调用它,但在解决方案中我找不到它! 我做错了什么?
2014 年 9 月 25 日更新
- 使用现有模板 Visual Studio 2014 创建 WcfService 应用
- 使用 Visual Studio 2014 中的现有模板创建 MVC 4 Web 应用
- 将 WcfService 引用添加到 MVC 应用程序。
Controller 代码:
namespace MvcApplication4.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
ServiceReference1.Service1 srv = new ServiceReference1.Service1();
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
WCF 服务代码:
namespace WcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
添加服务引用后:
报错是ServiceReference1.Service1 srv = new ServiceReference1.Service1();不存在。
谢谢
最佳答案
请试试这个,
- 右键单击 MVC 项目中的“ServiceReference1”
- 点击“配置服务引用”
- 如果选中“在引用的程序集中重用类型”字段,请将其删除(取消选中此字段)。
- 点击确定按钮。
希望它会起作用, 谢谢。
关于c# - 在 Asp.Net MVC 应用程序中调用 wcf 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26026731/