c# - 在 Asp.Net MVC 应用程序中调用 wcf 服务

标签 c# asp.net-mvc wcf

这可能是一个非常基本的问题,但我还没有弄清楚如何在 mvc 应用程序中调用简单的 wcf 服务。 我添加了服务引用,最后我希望使用服务引用名称调用它,但在解决方案中我找不到它! 我做错了什么?

2014 年 9 月 25 日更新

  1. 使用现有模板 Visual Studio 2014 创建 WcfService 应用
  2. 使用 Visual Studio 2014 中的现有模板创建 MVC 4 Web 应用
  3. 将 WcfService 引用添加到 MVC 应用程序。 enter image description here

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;
        }
    }
}

添加服务引用后:

enter image description here

报错是ServiceReference1.Service1 srv = new ServiceReference1.Service1();不存在。

谢谢

最佳答案

请试试这个,

  • 右键单击 MVC 项目中的“ServiceReference1”
  • 点击“配置服务引用”
  • 如果选中“在引用的程序集中重用类型”字段,请将其删除(取消选中此字段)。
  • 点击确定按钮。

希望它会起作用, 谢谢。

关于c# - 在 Asp.Net MVC 应用程序中调用 wcf 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26026731/

相关文章:

asp.net-mvc - 在不使用模型的情况下从 View 到 Controller 获取开始日期和结束日期值

c# - 如何并排显示 4 个三角形图案

c# - 使用 Visual Studio 连接远程 Mysql 数据库

c# - 端口更改时无法连接到 mysql 服务器 C#

wcf - WCF服务无法在HTTPS上运行

.net - 当 WCF 客户端为同一合约指定多个端点时会发生什么?

c# - 将 NetTCPBinding 与 TcpClientCredentialType.Windows 结合使用的 WCF 安全性

c# - Visual Studio 2015 C# 将应用程序部署到桌面

c# - 404 时重定向到 home/index

asp.net - 什么是 ASP.NET MVC 中的环境路由值及其工作原理?