WCF客户端具体搭建方法解析

来源:互联网 发布:网络教育的考试 编辑:程序博客网 时间:2024/06/16 05:31

搭建WCF客户端,最重要就是要遵循服务端的契约,客户端通过代理(Proxy)来访问服务端点,而并不关心服务端的具体实现。代理要做的就是通过与服务端确认通讯协议,并通过信道(channels)交换数据。在服务端,ServiceHost会为每个端点创建一个信道侦听器,由侦听器产生信道。而客户端代理则产生一个信道发生器,产生客户端信道。只有在服务端信道和客户端信道一致的情况下,双方才允许进行通讯。信道会对通讯过程进行监控,保障通讯的安全性。

为了简单的完成一个WCF客户端,微软为我们准备了一个小工具,就是Service Model MetadataUtility。这个工具能帮你快速的从服务地址中生成客户代理和配置文件。

首先允许服务器端程序,等服务启动后。在VS2008命令行窗口中输入如下命令:svcutil.exehttp://localhost:8080/MyService回车后得到如下页面。

 

从上面画面中可以看到,wcf为客户端生成了一个客户代理类MyService.cs和一个配置文件output.config。客户端只需要整合这两个文件就可以与服务端通讯了。我们来看看这两个文件的内容:

MyService.cs

//------------------------------------------------------------------------------
//
//    此代码由工具生成。
//    运行库版本:2.0.50727.1433
//
//    对此文件的更改可能会导致不正确的行为,并且如果
//    重新生成代码,这些更改将会丢失。
//
//------------------------------------------------------------------------------

 

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel","3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="IMyService")]
public interface IMyService
{
   
   [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IMyService/SayHello",ReplyAction="http://tempuri.org/IMyService/SayHelloResponse")]
    stringSayHello(string Name);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel","3.0.0.0")]
public interface IMyServiceChannel : IMyService,System.ServiceModel.IClientChannel
{
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel","3.0.0.0")]
public partial class MyServiceClient :System.ServiceModel.ClientBase, IMyService
{
   
    publicMyServiceClient()
    {
    }
   
    publicMyServiceClient(string endpointConfigurationName) :
           base(endpointConfigurationName)
    {
    }
   
    publicMyServiceClient(string endpointConfigurationName, stringremoteAddress) :
           base(endpointConfigurationName, remoteAddress)
    {
    }
   
    publicMyServiceClient(string endpointConfigurationName,System.ServiceModel.EndpointAddress remoteAddress) :
           base(endpointConfigurationName, remoteAddress)
    {
    }
   
    publicMyServiceClient(System.ServiceModel.Channels.Binding binding,System.ServiceModel.EndpointAddress remoteAddress) :
           base(binding, remoteAddress)
    {
    }
   
    publicstring SayHello(string Name)
    {
       return base.Channel.SayHello(Name);
    }
}

output.config



   
       
           
               
                   openTimeout="00:01:00" receiveTimeout="00:10:00"sendTimeout="00:01:00"
                   bypassProxyOnLocal="false" transactionFlow="false"hostNameComparisonMode="StrongWildcard"
                   maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                   messageEncoding="Text" textEncoding="utf-8"useDefaultWebProxy="true"
                   allowCookies="false">
                   
                       maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
                   
                       enabled="false" />
                   
                       
                           realm="" />
                       
                           algorithmSuite="Default" establishSecurityContext="true"/>
                   
               
           
       
       
           http://localhost:9000/MyService" binding="wsHttpBinding"
               bindingConfiguration="WSHttpBinding_IMyService"contract="IMyService"
               name="WSHttpBinding_IMyService">
               
                   
               
           
       
   

从这个文件可以看到,WCF客户端实际上是继承了两个接口,System.ServiceModel.ClientBase<IContract>和IContract。其中IContract是服务端契约的接口;

output.config文件则定义了和服务端匹配的endpoint,有了这两个文件,最后要做的事情就是将其整合到WCF客户端程序中,其步骤如下:

1)建立一个空白解决方案,方案的名称叫MyWCFClient,添加一个名称为MyWCF.Client的ConsoleApplication项目。在该项目中添加System.ServiceModel的引用。

2)另外在方案中再添加一个类库项目,项目名称叫MyWCF.ClientBase,为项目添加System.ServiceModel的引用,类名改为ClientBase。将TemperatureService.cs文件中的代码拷贝到ClientBase中的命名空间引用下。

3)在项目MyWCF.Client项目中添加一个App.config文件,将output.config文件的代码粘贴到该文件中覆盖原来的代码。并为该项目添加对MyWCF.ClientBase项目和System.ServiceModel的引用。

4)在项目MyWCF.Client的Main方法中添加如下代码

 

 public void SayHello(string Name)
       {
           if (Name.Equals(""))
           {
               return;
           }
           MyServiceClient proxy = newMyServiceClient("WSHttpBinding_IMyService");
           try
           {
               proxy.Open();
           }
           catch (Exception e)
           {
               MessageBox.Show("服务引用失败");
           }
           MessageBox.Show(proxy.SayHello(Name));
       }

 

注意:自动生成的output.config文件在添加到项目中后要重命名为app.config,否则在实例化代理(proxy)时,会找不到指定的终节点名称和契约!!!

 

本文文本部分参考:http://developer.51cto.com/art/201002/185219.htm,如有错误,敬请斧正!

0 0
原创粉丝点击