65.9K
CodeProject 正在变化。 阅读更多。
Home

如何在 MVC 中使用 WCF 服务而不添加服务引用

starIconstarIconstarIconstarIconstarIcon

5.00/5 (5投票s)

2016年7月19日

CPOL

3分钟阅读

viewsIcon

26729

downloadIcon

283

如何在ASP.net MVC应用程序中使用WCF服务,而无需添加代理或服务引用

引言

让我们首先确定代码的目的是什么。

对于本文,代码的目的是在ASP.NET MVC应用程序中使用WCF服务,而无需添加代理或服务引用。有一种方法可以使用服务模型元数据实用程序工具(SVCUTIL.EXE),但我们也有一个更好的解决方案,即编写自己的代码。

使用代码

首先,我们需要将我们的服务契约和数据契约放入一个共享库(WCF服务库),我们的客户端应用程序可以使用它。

步骤1:创建新项目(WCF服务库)

1) 在文件菜单上,单击新建项目

2) 在“新建项目”对话框的项目类型下,展开Visual C#,然后单击WCF并选择WCF服务库。然后在名称框中,键入“ServiceLibrary”,在解决方案名称中,键入“WCFDemo”,然后单击“确定”。

3) 现在,删除默认创建的接口和类“IService1.cs”和“Service1.cs

4) 通过右键单击ServiceLibrary(项目) > 添加 > 新建项,然后选择“接口”,命名为“IUser.cs”,然后单击添加,添加一个新接口。

5) 将public访问修饰符设置为接口IPayment

public interface IPayment
{

}

6) 我们需要将[ServiceContract]属性添加到接口IPayment

[ServiceContract]
public interface IPayment
{

}

7) 我们在IPayment接口中声明一个名为ReverseString的操作,并添加[OperationContract]属性。

[ServiceContract]
public interface IPayment
{
    [OperationContract]
    string ReverseString(string orignal);
}

步骤2:向解决方案资源管理器中添加一个新项目

1) 右键单击解决方案资源管理器“WCFDemo”> 添加> 新建项目

2) 在“新建项目”对话框的项目类型下,展开Visual C#,然后单击WCF并选择WCF服务应用程序。然后在名称框中,键入 “WCFService”,然后单击“确定”。

3) 删除默认创建的接口和服务“IService1.cs”和“Service1.svc”

4) 添加新的WCF服务。右键单击WCFService (项目) > 添加 > 新建项。然后在“添加新项”对话框中选择WCF服务,并将其命名为“User.SVC”

5) 从WCFService (项目)中删除接口IUser.cs。因为我们已经在ServiceLibrary (项目)上添加了这个接口。

6) 将“ServiceLibrary”的引用添加到“WCFService”。右键单击WCFService (项目)的引用。然后单击添加引用

7) 在“引用管理器”对话框中,在“项目”下选择“解决方案”,然后在“ServiceLibrary”中选中,然后单击“确定”。

8) 现在,将“ServiceLibrary”项目的引用添加到了“WCFService”项目中。现在是时候实现我们的ReverseString操作了。将以下代码粘贴到User.svc.cs

//Declare Namespace: using ServiceLibrary;
public class User : IUser
    {
        public string ReverseString(string orignal)
        {

            if (string.IsNullOrWhiteSpace(orignal))
                return string.Empty;

            char[] charArray = orignal.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
            
        }
    }

步骤3 - 这是在ASP.NET MVC应用程序中访问WCF服务而不添加代理或服务引用的最后一步

1) 右键单击解决方案资源管理器“WCFDemo” > 添加 > 新建项目

2) 在“新建项目”对话框的项目类型下,展开Visual C#,然后单击Web并选择ASP.NET Web应用程序。然后将其命名为“WebApps,然后单击“确定”。

3) 现在在对话框中,单击ASP.NET 4.5.2模板下的“空”,然后选中“MVC”复选框 ,然后单击“确定”。

4) 通过右键单击“控制器”文件夹 > 添加 > 控制器来创建一个控制器。然后选择“MVC 5控制器 - 空”。然后单击“添加”,最后将其命名为“HomeController”。

5) 默认创建一个get操作方法,即Index

// GET: Home
public ActionResult Index()
{
     return View();
}

6) 将“ServiceLibrary”的引用添加到“WebApps”中。右键单击WebApps (项目)的引用。然后单击添加引用。在“引用管理器”对话框中,在“项目”下选择“解决方案”,然后在“ServiceLibrary”中选中,然后单击“确定”。现在,将“ServiceLibrary”项目的引用添加到了“WCFService”项目中。

7) 终于,一切都完成了。现在是时候在不使用代理或服务引用的情况下访问我们创建的操作方法了。

将以下代码粘贴到您的Index方法中。

 public ActionResult Index()
        {
            //Create a ChannelFactory
            //Required Namespace: using System.ServiceModel;
            //Required Namespace: using ServiceLibrary;
            ChannelFactory<IUser> channelFactory = null;

            try
            {
                //Create a binding of the type exposed by service
                BasicHttpBinding binding = new BasicHttpBinding();

                //Create EndPoint address
                EndpointAddress endpointAddress = new EndpointAddress("https://:50419/User.svc");

                //Pass Binding and EndPoint address to ChannelFactory
                channelFactory = new ChannelFactory<IUser>(binding, endpointAddress);

                //Now create the new channel as below
                IUser channel = channelFactory.CreateChannel();

                //Call the service method on this channel as below
                string result = channel.ReverseString("Suchit Khunt");

                return View(result);
            }
            catch (TimeoutException)
            {
                //Timeout error
                if (channelFactory != null)
                    channelFactory.Abort();

                throw;
            }
            catch (FaultException)
            {
                if (channelFactory != null)
                    channelFactory.Abort();

                throw;
            }
            catch (CommunicationException)
            {
                //Communication error    
                if (channelFactory != null)
                    channelFactory.Abort();

                throw;
            }
            catch (Exception)
            {
                if (channelFactory != null)
                    channelFactory.Abort();

                throw;
            }

        }
© . All rights reserved.