您的位置:首页 > 其它

不引用服务而使用WCF,手动编写客户端代理类

2016-07-11 10:36 525 查看
http://foreversky12.iteye.com/blog/2308191

原文:http://www.wcftutorial.net/Introduction-to-WCF.aspx 

自托管Self Hosting 

如果是web服务,我们只能将服务托管在IIS上,但是WCF提供了让服务可以托管在任何应用程序(比如控制台应用程序,Windows Form等等)中的能力。许多有趣开发者有责任提供和管理托管进程的生命周期。服务端和客户端可以同时存在在相同的进程中。现在,让我们创建一个托管在控制台应用程序中的WCF服务。我们将会以抽象类ClientBase为基类创建一个代理。 

Note:托管程序必须在客户端访问之前运行,这也意味着你得预启动它。 

Step1: 让我们从创建服务七月和它的实现开始吧。创建一个控制台应用程序,并且命名为MyCalculatorService。这是一个简单的服务,它只是返回两个数想加的和。 



Step 2: 添加System.ServiceModel引用 


 

Step 3: 创建ISimpleCalculator接口,加入ServiceContract和OperationContract到类和方法上,你会在随后的内容中了解它们的作用。这些Attribute将会把服务公布给调用方。 

IMyCalculatorService.cs 

C#代码  


using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

using System.ServiceModel;  

  

namespace MyCalculatorService  

{  

    [ServiceContract()]  

    public interface ISimpleCalculator  

    {  

        [OperationContract()]  

        int Add(int num1, int num2);  

    }  

}  

Step 4: 如下所示,MyCalculatorService是IMyCalculatorService接口的实现类 

MyCalculatorService.cs 

C#代码  


using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

  

namespace MyCalculatorService  

{  

    class SimpleCalculator : ISimpleCalculator  

    {  

        public int Add(int num1, int num2)  

        {  

            return num1 + num2;  

        }  

    }  

}  

Step 5: 现在服务已经准备好了,让我们开始实现托管进程吧,创建一个新的控制台应用程序MyCalculatorServiceHost 



Step 6: ServiceHost是托管WCF服务的核心类。它的构造函数可以接受已经实现了的契约类和服务地址作为参数,你可以用逗号分隔注册多个基地址,但是地址必须基于同样的传输协议(http等) 

C#代码  


Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator");  

  

Uri tcpUrl = new Uri("net.tcp://localhost:8090/MyService/SimpleCalculator");  

  

ServiceHost host = new ServiceHost(typeof(MyCalculatorService.SimpleCalculator), httpUrl, tcpUrl);  

使用AddServiceEndpoint()可以加入多个终结点。host.Open()将启动服务,之后就可以为客户端提供服务了。 

Step 7: 以下程序说明了如果实现托管进程 

C#代码  


using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

using System.ServiceModel;  

using System.ServiceModel.Description;  

  

namespace MyCalculatorServiceHost  

{  

    class Program  

    {  

        static void Main(string[] args)  

        {  

            //Create a URI to serve as the base address  

            Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator");  

            //Create ServiceHost  

            ServiceHost host   

            = new ServiceHost(typeof(MyCalculatorService.SimpleCalculator), httpUrl);  

            //Add a service endpoint  

            host.AddServiceEndpoint(typeof(MyCalculatorService.ISimpleCalculator)  

            , new WSHttpBinding(), "");  

            //Enable metadata exchange  

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();  

            smb.HttpGetEnabled = true;  

            host.Description.Behaviors.Add(smb);  

            //Start the Service  

            host.Open();  

  

            Console.WriteLine("Service is host at " + DateTime.Now.ToString());  

            Console.WriteLine("Host is running... Press <Enter> key to stop");  

            Console.ReadLine();  

        }  

    }  

}  

Step 8: 服务一旦被托管启动,我们就可以创建为客户端服务的带来类了,创建代理有不同的方法 
    1.使用SvcUtil.exe创建代理以及相关联的配置文件 
    2.将服务引用添加到客户端程序 
    3.实现基类ClientBase<T> 
在这些方法中,实现ClientBase<T>是最好的实践。假如你使用另外两个,当服务实现发生变化的时候,你需要每次都创建代理类。但是使用ClientBase<T>就不需要。它会在启动的时候创建代理,所以它可以处理任何变化。 

MyCalculatorServiceProxy.cs 

C#代码  


using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

using System.ServiceModel;  

using MyCalculatorService;  

namespace MyCalculatorServiceProxy  

{  

    public class MyCalculatorServiceProxy :   

        //WCF create proxy for ISimpleCalculator using ClientBase  

        ClientBase<ISimpleCalculator>,  

        ISimpleCalculator  

    {  

        public int Add(int num1, int num2)  

        {  

            //Call base to do funtion  

            return base.Channel.Add(num1, num2);  

        }  

    }  

}  

Step 9: 在客户端,我们创建代理类的实例,并且使用如下的方法调用,将代理dll的参照加入到这个工程中。 

C#代码  


using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

using System.ServiceModel;  

  

namespace MyCalculatorServiceClient  

{  

    class Program  

    {  

        static void Main(string[] args)  

        {  

            MyCalculatorServiceProxy.MyCalculatorServiceProxy proxy ;  

            proxy= new MyCalculatorServiceProxy.MyCalculatorServiceProxy();  

            Console.WriteLine("Client is running at " + DateTime.Now.ToString());  

            Console.WriteLine("Sum of two numbers... 5+5 ="+proxy.Add(5,5));  

            Console.ReadLine();  

        }  

    }  

}  

Step 10 : 终结点信息需要被加入到客户端程序的配置文件中。 

Xml代码  


<?xml version="1.0" encoding="utf-8" ?>  

<configuration>  

    <system.serviceModel>  

        <client>  

            <endpoint address ="http://localhost:8090/MyService/SimpleCalculator"   

                    binding ="wsHttpBinding"  

                    contract ="MyCalculatorService.ISimpleCalculator">  

            </endpoint>  

        </client>  

    </system.serviceModel>  

</configuration>  

Step 11: 运行客户端之前,你需要先启动服务端,客户端的输出如下所示。 


 

自托管展现了in-Pro hosting的好处。以编程的方式访问,并且可以编程为单例服务。我系统你会喜欢上自托管的方式,现在让我们进入下一步,将服务托管在Windows Activation service中。

==================================================================================================================

我们之所以说WCF比一般的Web Service要强大得多,是因为它要比一般的Web服务要灵活得多,而且它不仅仅能在IIS服务器上运行,其实它可以用很多种方法来运行,哪怕一个控制台应用程序。

现在,大家可以回忆一下前面我写的《传说中的WCF》,我上面的例子绝大多数都是控制台应用程序类型的。我们应当把WCF理解为一种通信技术,而不只是服务。前面的例子中我是告诉大家,完成服务器端后,就在客户端项目中添加服务引用,这样就生成了客户端代理类,我们就可以像平时使用一般类型一样使用了。

其实按照我们前面所讲的方法,也足以完成许多实际任务了。大家是否还想拓展一下呢? 有朋友肯定会问了:再拓展会不会变得很难? 放心吧,不会很难,相信我,老周从来不会讲大家都看不懂的东西的。

我们现在不妨尝试一下,在客户端不添加服务引用,而是由我们自己来编写调用服务的代理类。要做到这一点,首先我们要明确的,其实我们所编写的服务协定,在服务器和客户端都需要用到,如果大家查看过添加服务引用时由工具生成的代码,会发现其实它在客户端也生成了服务协定的代码。所以,在我们手动编写调用服务的代码时,也需要这样,因此有两种方法可以在服务器和客户端之间共用服务协定,一是把代码复制一下粘贴到客户端中,另一种方法,我们可以新建一个类库,然后把服务协定写到这个类库中,最后在服务器端和客户端都引用这个类库即可。举个例子,假如有以下定义的协定:

[csharp] view
plain copy

 print?

[ServiceContract]  

public interface ITest  

{  

    [OperationContract]  

    int Add(int a, int b);  

  

    [OperationContract]  

    int GetRandmon();  

  

    [OperationContract]  

    int Multiply(int a, int b);  

}  

 

然后,我们在服务器端实现协定,注意:接口在服务器端实现即可,客户端不需要。

[csharp] view
plain copy

 print?

// 实现服务  

public class MyService : CommonLib.ITest  

{  

    Random m_rand = null;  

  

    // 构造函数  

    public MyService()  

    {  

        m_rand = new Random();  

    }  

  

    public int Add(int a, int b)  

    {  

        return a + b;  

    }  

  

    public int GetRandmon()  

    {  

        return m_rand.Next();  

    }  

  

    public int Multiply(int a, int b)  

    {  

        return a * b;  

    }  

}  

接着,和以前一样,创建服务主机,并侦听客户端调用。

[csharp] view
plain copy

 print?

static void Main(string[] args)  

{  

    ServiceHost host = new ServiceHost(typeof(MyService));  

    // HTTP方式  

    WSHttpBinding httpBinding = new WSHttpBinding(SecurityMode.None);  

    host.AddServiceEndpoint(typeof(CommonLib.ITest), httpBinding, "http://localhost:8900/");  

    // TCP方式  

    NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);  

    host.AddServiceEndpoint(typeof(CommonLib.ITest), tcpBinding, "net.tcp://localhost:1700/");  

    // 打开服务  

    host.Open();  

    Console.WriteLine("服务已启动。");  

    Console.Read();  

    host.Close();  

}  

大家可以细心看一下,和以前的代码有什么不同? 不妨比较一下,看看。

1、以前,我们在创建ServiceHost时会指定一个HTTP基地址,但这里不需要了,基址是便于工具生成代理类的,我们既然要手动来写了,就不用生成代码了,也不用基址了。

2、以前,我们会在ServiceHost.Description.Behaviors集合中加一个ServiceMetadataBehavior对象,以提供WSDL,帮肋工具生成代码。现在我们都自己手动写了,当然就不用提供WSDL了。

认真想想,看是不是这样? 如果你有兴趣,也可以为ServiceHost弄一个基址,但不添加ServiceMetadataBehavior,然后在客户端项目中添加引用,你会发现……呵呵,你懂的。

那现在在客户端怎么调用服务呢? 使用通道,可能有朋友会看到IChannel接口,又派生出很多接口,但貌似没有一个是类的,是不是要自己来写通道啊? 不用,当然你要扩展通道层是另一回事,通常我们无需扩展通道,因为现有的已经足够牛逼了。我们在“对象浏览器”中是看不到与通道相关的可用的类,因为.NET内部是有实现的,只是没有定义为public而已,是internal。

我们根本可以不必理会如何找通道的问题,就好像我们坐在一辆全自动导航或者有专业司机驾驶的车上,司机知道怎么走,我们不必要担心不知道怎么走这段路。同理,我们可以不直接操作通道,为什么呢?因为我们定义的每一个服务协定都可以认为是一个通道。

上面我们定义的那么ITest就是一个通道,WCF内部已经帮我们把它变成一个通道了,不信的话,你往后看例子。

我们已经知道,编写的服务协定可以当成一个通道来操作,所以,在客户端中,我们要手动写代码来调用服务,要可以遵循以下步骤,有兴趣的话你可以背下来,但告诉你,背了没用。

1、创建与服务器匹配的Binding,这个就不用怀疑的了,你跟别人签合同,那肯定是一式两份,对方持一份,你拿一份,你肯定不会拿一张白纸回家保存吧。

2、创建通道,使用ChannelFactory<TChannel>类可以创建通道,因为它是“工厂”嘛,工厂当然是用来生产的,但ChannelFactory工厂不是用来生产老鼠药也不是生产地雷的,它是专门生产Channel(通道)的。这个TChannel就可以写上你定义的服务协定的接口,如上面的ITest。

3、得到的通道就是ITest,然后就可以调用服务了,比如要两个数相加,就调用ITest.Add。

4、关闭通道,把ITest强制转换为IClientChannel就可以调用Close方法关闭通道。

可能你对这些步骤还有疑问,没关系,你不妨先疑一下。我们继续往下操作。

前面定义服务主机的时候,我们使用了两个终结点,一个是HTTP的,另一个是TCP调用。所以我们这里也要分别用这两种方法调用。我可没说一定要用这两种方法调用,我只是多写了一个作演示。

在客户端,先声明这两个终结点地址,就是我们在服务器定义的两个地址。

[csharp] view
plain copy

 print?

EndpointAddress edpHttp = new EndpointAddress("http://localhost:8900/");  

EndpointAddress edpTcp = new EndpointAddress("net.tcp://localhost:1700/");  

然后,分别用两种Binding调有服务。

[csharp] view
plain copy

 print?

private void btnHTTP_Click(object sender, EventArgs e)  

{  

    // 创建Binding  

    WSHttpBinding httpBinding = new WSHttpBinding(SecurityMode.None);  

    // 创建通道  

    ChannelFactory<CommonLib.ITest> factory = new ChannelFactory<CommonLib.ITest>(httpBinding);  

    CommonLib.ITest channel = factory.CreateChannel(edpHttp);  

    // 调用  

    int resAdd = channel.Add(int.Parse(txtNum11.Text), int.Parse(txtNum12.Text));  

    txtResAdd.Text = resAdd.ToString();  

  

    int resMult = channel.Multiply(int.Parse(txtNum21.Text), int.Parse(txtNum22.Text));  

    txtResMulti.Text = resMult.ToString();  

  

    int rand = channel.GetRandmon();  

    txtRand.Text = rand.ToString();  

  

    // 关闭通道  

    ((IClientChannel)channel).Close();  

}  

  

private void btnTCP_Click(object sender, EventArgs e)  

{  

    // 创建Binding  

    NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);  

    // 创建通道  

    ChannelFactory<CommonLib.ITest> factory = new ChannelFactory<CommonLib.ITest>(tcpBinding);  

    CommonLib.ITest channel = factory.CreateChannel(edpTcp);  

    // 调用  

    txtResAdd.Text = channel.Add(int.Parse(txtNum11.Text),int.Parse(txtNum12.Text)).ToString();  

    txtResMulti.Text = channel.Multiply(int.Parse(txtNum21.Text),int.Parse(txtNum22.Text)).ToString();  

    txtRand.Text = channel.GetRandmon().ToString();  

  

    // 关闭通道  

    ((IClientChannel)channel).Close();  

}  

你也许会问,ITest不是接口来的吗,怎么可以调用? 别忘了,我们在服务器端已经实现过了,WCF内部会帮我们找到关联的类。

现在,你就兴奋地看看结果吧。记着,运行服务器端需要管理员身份运行,这个我说了三千五百遍了。



 

嘿嘿,乍一看,好像可以了,已经能调用了,但是,这样是不是不太简洁呢? 而且我们不能将其当成一人类来用,每次调用要通过ChannelFactory来生产,比较麻烦,更重要的是,如果有服务器回调协定,就不好弄了。

因此,对于上面的客户端代码我们是否考虑进一个封装呢? 这里我们完全可以考虑使用ClientBase<TChannel>类,它对于通道和相关操作作了进一步封装,当然它是抽象类,不能直接拿来玩,要先派生出一个类。

[csharp] view
plain copy

 print?

/// <summary>  

/// 用于调用服务的类  

/// </summary>  

public class MyClient : ClientBase<CommonLib.ITest>,CommonLib.ITest  

{  

    public MyClient(System.ServiceModel.Channels.Binding binding, EndpointAddress edpAddr)  

        : base(binding, edpAddr) { }  

  

    public int Add(int a, int b)  

    {  

        return base.Channel.Add(a, b);  

    }  

  

    public int GetRandmon()  

    {  

        return base.Channel.GetRandmon();  

    }  

  

    public int Multiply(int a, int b)  

    {  

        return base.Channel.Multiply(a, b);  

    }  

}  

有人会问,为什么从ClientBase<CommonLib.ITest>派生,又要实现一次CommonLib.ITest接口呢? 当然,你不实现也无所谓,再实现一次CommonLib.ITest接口是为了让这个类的公共方法和ITest的方法一样,这样方便调用。

通过访问base.Channel就可以得到一个对ITest的引用,无需要我们自己创建通道,因为基类中已经带了默认实现。

现在,把前面的调用代码改一下,是不是觉得简洁了?

[csharp] view
plain copy

 print?

private void btnHTTP_Click(object sender, EventArgs e)  

{  

    MyClient client = new MyClient(new WSHttpBinding(SecurityMode.None), edpHttp);  

    txtResAdd.Text = client.Add(int.Parse(txtNum11.Text), int.Parse(txtNum12.Text)).ToString();  

    txtResMulti.Text = client.Multiply(int.Parse(txtNum21.Text), int.Parse(txtNum22.Text)).ToString();  

    txtRand.Text = client.GetRandmon().ToString();  

}  

  

private void btnTCP_Click(object sender, EventArgs e)  

{  

    MyClient client = new MyClient(new NetTcpBinding(SecurityMode.None), edpTcp);  

    txtResAdd.Text = client.Add(int.Parse(txtNum11.Text), int.Parse(txtNum12.Text)).ToString();  

    txtResMulti.Text = client.Multiply(int.Parse(txtNum21.Text), int.Parse(txtNum22.Text)).ToString();  

    txtRand.Text = client.GetRandmon().ToString();  

}  

现在看看我们自己写的这段代码,是不是与VS生成的代码比较接近了? 而且连配置文件也省了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: