您的位置:知识库 » .NET技术

WCF分布式开发步步为赢(2)自定义托管宿主WCF解决方案开发配置过程详解

作者: Frank Xu Lei  来源: 博客园  发布时间: 2009-04-01 00:11  阅读: 6935 次  推荐: 0   原文链接   [收藏]  
【4】客户端的服务引用、配置和开发过程:

    服务类和服务宿主已经配置完毕,下面我们来讲述客户端添加WCF服务的引用、配置和服务调用过程。

    首先要运行宿主程序,这样才能在客户端添加服务引用,从元数据获取服务类的相关信息,生成本地类。

4.1】添加WCF服务引用:

  服务浏览器,单击客户端项目,添加Services Reference.在弹出的窗口地址里输入服务的基地址,首先查找TCP服务。

保持地址和配置文件里服务的基地址相同,:查找成功后的窗口如下:

  我们可以看到WCF服务类公布的操作,输入命名空间的名字为ServiceReferenceTcp。同样的方式添加对HTTP服务的引用。添加成功后我们可以查看所有文件,在客户端项目的服务引用的窗口看到所有的服务引用的文件信息如图:

证明我们添加WCF服务成功。客户端app.config文件里会生成相应的服务代理的相关信息,包括客户端终结点的信息:

<client>
            
<endpoint address="http://localhost:8001/WCFService" binding="wsHttpBinding"
                bindingConfiguration
="WSHttpBinding_IWCFService" contract="ServiceReferenceTcp.IWCFService"
                name
="WSHttpBinding_IWCFService">
                
<identity>
                    
<userPrincipalName value="FRANK\Administrator" />
                
identity>
            
endpoint>
            
<endpoint address="net.tcp://localhost:8002/WCFService" binding="netTcpBinding"
                bindingConfiguration
="NetTcpBinding_IWCFService" contract="ServiceReferenceTcp.IWCFService"
                name
="NetTcpBinding_IWCFService">
                
<identity>
                    
<userPrincipalName value="FRANK\Administrator" />
                
identity>
            
endpoint>
            
<endpoint address="http://localhost:8001/WCFService" binding="wsHttpBinding"
                bindingConfiguration
="WSHttpBinding_IWCFService1" contract="ServiceReferenceHttp.IWCFService"
                name
="WSHttpBinding_IWCFService1">
                
<identity>
                    
<userPrincipalName value="FRANK\Administrator" />
                
identity>
            
endpoint>
            
<endpoint address="net.tcp://localhost:8002/WCFService" binding="netTcpBinding"
                bindingConfiguration
="NetTcpBinding_IWCFService1" contract="ServiceReferenceHttp.IWCFService"
                name
="NetTcpBinding_IWCFService1">
                
<identity>
                    
<userPrincipalName value="FRANK\Administrator" />
                
identity>
            
endpoint>
        
client>

   客户端的配置方式和宿主托管方式非常类似,同样包括地址、绑定、契约等信息。

4.2】调用服务:

     要调用相应的服务,需要实例化服务代理类的实例,首先添加命名空间的引用using ServiceReferenceHttp;
using ServiceReferenceTcp;
这样可以使用本地反序列化生成的类和其他配置的信息。我们分别实例化HTTP和TCP代理的类,非别调用服务的不同操作,USER的实例也分别为不同的命名空间中的类型,需要分别指定命名空间。

具体测试代码如下:

 //HTTP WSHttpBinding_IWCFService1
            ServiceReferenceHttp.WCFServiceClient wcfServiceProxyHttp = new ServiceReferenceHttp.WCFServiceClient("WSHttpBinding_IWCFService1");
            
//通过代理调用SayHello服务
            Console.WriteLine(wcfServiceProxyHttp.SayHello("Frank Xu Lei WSHttpBinding"));
            
////通过代理调用调用SayHelloToUser,传递对象
            ServiceReferenceHttp.User user = new ServiceReferenceHttp.User();
            user.FirstName 
= "WSHttpBinding";
            user.LastName 
= "Frank";
            Console.WriteLine(wcfServiceProxyHttp.SayHelloToUser(user));

            
//TCP NetTcpBinding_IWCFService
            ServiceReferenceTcp.WCFServiceClient wcfServiceProxyTcp = new ServiceReferenceTcp.WCFServiceClient("NetTcpBinding_IWCFService");
            
//通过代理调用SayHello服务
            Console.WriteLine(wcfServiceProxyTcp.SayHello("Frank Xu Lei NetTcpBinding"));
            
////通过代理调用调用SayHelloToUser,传递对象
            ServiceReferenceTcp.User userTcp = new ServiceReferenceTcp.User();
            userTcp.FirstName 
= "NetTcpBinding";
            userTcp.LastName 
= "Frank";
            Console.WriteLine(wcfServiceProxyTcp.SayHelloToUser(userTcp));

  运行结果如图:

   两者不同的协议服务调用都成功执行,并且返回正确的结果。

【总结】:

    以上就是本节关于自定义托管宿主WCF服务解决方案开发与配置的详细过程,包括服务代码的编写、宿主程序的开发与配置、客户端服务的引用和调用。我们这里托管宿主服务使用了配置文件,来配置WCF服务的信息,这里也可以编码实现。

    另外客户端要想通过元数据交换来反序列换生成本地的WCF服务类等相关代码,就需要在托管宿主里配置可以使用的元数据交换节点,这里缺少设置,就会出现获取服务元数据的异常,导致客户端添加服务出错。(最近论文答辩有点忙,所以这个文章更新的比较慢,不好意思)另外给出本文的参考代码:/Files/frank_xl/WCFServiceConfigFrankXuLei.rar

希望本篇文章能给大家WCF分布式开发项目的配置带来一些帮助。~

 


 

老徐的博客

【作者】:Frank Xu Lei

【地址】:http://www.cnblogs.com/frank_xl/archive/2009/03/17/1414686.html

0
0
标签:WCF

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻