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

Bon*_*omi 1 c# asp.net-mvc wcf

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

更新25/09/2014

  1. 使用现有模板Visual Studio 2014创建WcfService应用程序
  2. 使用Visual Studio 2014中的现有模板创建MVC 4 Web App
  3. 将WcfService引用添加到MVC应用程序. 在此输入图像描述

控制器代码:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

添加服务引用后:

在此输入图像描述

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

谢谢

Sha*_*ini 7

请试试这个,

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

希望它会起作用,谢谢.