子曾经曰过

  博客园  :: 首页  ::  ::  ::  :: 管理

若要调用服务的操作,客户端首先需要导入服务契约到客户端的本地描述中,如果客户端使用了WCF,调用操作的常见做法是使用代理,代理是一个CLR类,它公开了一个单独的CLR接口用以表示服务契约,代理完全封装了服务的每个方面:服务位置,实现技术,运行时平台以及通信传输。

生成代理

VS2008 添加服务引用 或者

Svcutil http://localhost:8002/(mex)   /out:Proxy.cs

重点:创建和使用代理

代理类派生自ClientBase<T>类

View Code
using System;
using System.ComponentModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Threading;

namespace System.ServiceModel
{
// 摘要:
// 提供基实现,用于创建可调用服务的 Windows Communication Foundation (WCF) 客户端对象。
//
// 类型参数:
// TChannel:
// 用于连接服务的通道。
public abstract class ClientBase<TChannel> : ICommunicationObject, IDisposable where TChannel : class
{
// 摘要:
// 使用应用程序配置文件中的默认目标终结点初始化 System.ServiceModel.ClientBase<TChannel> 类的新实例。
//
// 异常:
// System.InvalidOperationException:
// 配置文件中没有默认的终结点信息,或者文件中有多个终结点,或者没有配置文件。
protected ClientBase();
//
// 摘要:
// 通过使用 callbackInstance 作为双工对话中的回调对象来初始化 System.ServiceModel.ClientBase<TChannel>
// 类的新实例。
//
// 参数:
// callbackInstance:
// 客户端应用程序用以侦听来自所连接服务的消息的回调对象。
//
// 异常:
// System.ArgumentNullException:
// 回调实例为 null。
//
// System.InvalidOperationException:
// 配置文件中没有默认的终结点信息,或者文件中有多个终结点,或者没有配置文件。
protected ClientBase(InstanceContext callbackInstance);
//
// 摘要:
// 使用应用程序配置文件中由 endpointConfigurationName 指定的配置信息来初始化 System.ServiceModel.ClientBase<TChannel>
// 类的新实例。
//
// 参数:
// endpointConfigurationName:
// 应用程序配置文件中的终结点的名称。
//
// 异常:
// System.ArgumentNullException:
// 指定的终结点信息为 null。
//
// System.InvalidOperationException:
// 无法找到终结点或者终结点协定无效。
protected ClientBase(string endpointConfigurationName);
//
// 摘要:
// 使用指定的绑定和目标地址初始化 System.ServiceModel.ClientBase<TChannel> 类的新实例。
//
// 参数:
// binding:
// 用于调用服务的绑定。
//
// remoteAddress:
// 服务终结点的地址。
//
// 异常:
// System.ArgumentNullException:
// 绑定为 null。
//
// System.ArgumentNullException:
// 远程地址为 null。
protected ClientBase(Binding binding, EndpointAddress remoteAddress);
//
// 摘要:
// 使用指定的回调服务和终结点配置信息初始化 System.ServiceModel.ClientBase<TChannel> 类的新实例。
//
// 参数:
// callbackInstance:
// 客户端用以侦听来自所连接服务的消息的回调对象。
//
// endpointConfigurationName:
// 应用程序配置文件中的终结点的名称。
//
// 异常:
// System.ArgumentNullException:
// 回调实例为 null。
//
// System.ArgumentNullException:
// 终结点为 null。
//
// System.InvalidOperationException:
// 无法找到终结点或者终结点协定无效。
protected ClientBase(InstanceContext callbackInstance, string endpointConfigurationName);
//
// 摘要:
// 使用指定的目标地址和终结点信息初始化 System.ServiceModel.ClientBase<TChannel> 类的新实例。
//
// 参数:
// endpointConfigurationName:
// 应用程序配置文件中的终结点的名称。
//
// remoteAddress:
// 服务的地址。
//
// 异常:
// System.ArgumentNullException:
// 终结点为 null。
//
// System.ArgumentNullException:
// 远程地址为 null。
//
// System.InvalidOperationException:
// 无法找到终结点或者终结点协定无效。
protected ClientBase(string endpointConfigurationName, EndpointAddress remoteAddress);
//
// 摘要:
// 初始化 System.ServiceModel.ClientBase<TChannel> 类的新实例。
//
// 参数:
// endpointConfigurationName:
// 应用程序配置文件中的终结点的名称。
//
// remoteAddress:
// 服务的地址。
//
// 异常:
// System.ArgumentNullException:
// 终结点为 null。
//
// System.ArgumentNullException:
// 远程地址为 null。
//
// System.InvalidOperationException:
// 无法找到终结点或者终结点协定无效。
protected ClientBase(string endpointConfigurationName, string remoteAddress);
//
// 摘要:
// 初始化 System.ServiceModel.ClientBase<TChannel> 类的新实例。
//
// 参数:
// callbackInstance:
// 回调服务。
//
// binding:
// 用于调用服务的绑定。
//
// remoteAddress:
// 服务终结点的地址。
//
// 异常:
// System.ArgumentNullException:
// 回调实例为 null。
//
// System.ArgumentNullException:
// 绑定为 null。
//
// System.ArgumentNullException:
// 远程地址为 null。
protected ClientBase(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress);
//
// 摘要:
// 初始化 System.ServiceModel.ClientBase<TChannel> 类的新实例。
//
// 参数:
// callbackInstance:
// 客户端用以侦听来自所连接服务的消息的回调对象。
//
// endpointConfigurationName:
// 应用程序配置文件中的终结点的名称。
//
// remoteAddress:
// 服务的地址。
//
// 异常:
// System.ArgumentNullException:
// 回调实例为 null。
//
// System.ArgumentNullException:
// 终结点为 null。
//
// System.ArgumentNullException:
// 远程地址为 null。
//
// System.InvalidOperationException:
// 无法找到终结点或者终结点协定无效。
protected ClientBase(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress);
//
// 摘要:
// 初始化 System.ServiceModel.ClientBase<TChannel> 类的新实例。
//
// 参数:
// callbackInstance:
// 客户端用以侦听来自所连接服务的消息的回调对象。
//
// endpointConfigurationName:
// 应用程序配置文件中的终结点的名称。
//
// remoteAddress:
// 服务的地址。
//
// 异常:
// System.ArgumentNullException:
// 回调实例为 null。
//
// System.ArgumentNullException:
// 终结点为 null。
//
// System.ArgumentNullException:
// 远程地址为 null。
//
// System.InvalidOperationException:
// 无法找到终结点或者终结点协定无效。
protected ClientBase(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress);

// 摘要:
// 获取用于与服务通信的内部通道。
//
// 返回结果:
// 一个以类型参数形式传递给构造函数的目标服务协定接口的实现。
protected TChannel Channel { get; }
//
// 摘要:
// 获取基础 System.ServiceModel.ChannelFactory<TChannel> 对象。
//
// 返回结果:
// 一个 System.ServiceModel.ChannelFactory<TChannel> 对象。
public ChannelFactory<TChannel> ChannelFactory { get; }
//
// 摘要:
// 获取用于调用操作的客户端凭据。
//
// 返回结果:
// 返回一个 System.ServiceModel.Description.ClientCredentials,它表示客户端所提供的身份证明。
public ClientCredentials ClientCredentials { get; }
//
// 摘要:
// 获取 WCF 客户端可连接的服务的目标终结点。
//
// 返回结果:
// 目标终结点。
public ServiceEndpoint Endpoint { get; }
//
// 摘要:
// 获取基础 System.ServiceModel.IClientChannel 实现。
//
// 返回结果:
// WCF 客户端对象的客户端通道。
public IClientChannel InnerChannel { get; }
//
// 摘要:
// 获取 System.ServiceModel.ClientBase<TChannel> 对象的当前状态。
//
// 返回结果:
// 对象的 System.ServiceModel.CommunicationState 的值。
public CommunicationState State { get; }

// 摘要:
// 使 System.ServiceModel.ClientBase<TChannel> 对象立即从其当前状态转换到关闭状态。
public void Abort();
//
// 摘要:
// 使 System.ServiceModel.ClientBase<TChannel> 对象从其当前状态转换到关闭状态。
public void Close();
//
// 摘要:
// 返回新的服务通道。
//
// 返回结果:
// 一个类型为服务协定的通道。
protected virtual TChannel CreateChannel();
//
// 摘要:
// 指示内部通道显示在使用通道之前初始化通道时所需的用户界面。
public void DisplayInitializationUI();
//
// 摘要:
// 复制 C# 中默认关键字的行为。
//
// 类型参数:
// T:
//
// 返回结果:
// 如果 T 为引用类型,则返回 null;如果 T 为数值类型,则返回零。
protected T GetDefaultValueForInitialization<T>();
//
// 摘要:
// 为实现基于事件的异步模式提供支持。有关该模式的更多信息,请参见基于事件的异步模式概述。
//
// 参数:
// beginOperationDelegate:
// 一个用于调用异步操作的委托。
//
// inValues:
// 异步调用的输入值。
//
// endOperationDelegate:
// 一个用于在异步调用完成后将其结束的委托。
//
// operationCompletedCallback:
// 一个在异步方法完成时调用的由客户端提供的回调。系统会将该回调传递给 System.ServiceModel.ClientBase<TChannel>.BeginOperationDelegate。
//
// userState:
// 要与异步调用关联的 userState 对象。
protected void InvokeAsync(ClientBase<TChannel>.BeginOperationDelegate beginOperationDelegate, object[] inValues, ClientBase<TChannel>.EndOperationDelegate endOperationDelegate, SendOrPostCallback operationCompletedCallback, object userState);
//
// 摘要:
// 使 System.ServiceModel.ClientBase<TChannel> 对象从已创建状态转换到已打开状态。
public void Open();

// 摘要:
// 一个委托,由 System.ServiceModel.ClientBase<TChannel>.InvokeAsync(System.ServiceModel.ClientBase.BeginOperationDelegate,System.Object[],System.ServiceModel.ClientBase.EndOperationDelegate,System.Threading.SendOrPostCallback,System.Object)
// 用于调用客户端的异步操作。
//
// 参数:
// inValues:
// 异步调用的输入值。
//
// asyncCallback:
// 引用在相应异步操作完成时调用的方法。
//
// state:
// 一个 System.Object,可让客户端区分不同的异步调用。客户端可在事件完成回调的实参参数中进行使用。
//
// 返回结果:
// 异步调用的结果。
protected delegate IAsyncResult BeginOperationDelegate(object[] inValues, AsyncCallback asyncCallback, object state);

// 摘要:
// System.ServiceModel.ClientBase<TChannel>.InvokeAsync(System.ServiceModel.ClientBase.BeginOperationDelegate,System.Object[],System.ServiceModel.ClientBase.EndOperationDelegate,System.Threading.SendOrPostCallback,System.Object)
// 成功完成对 System.ServiceModel.ClientBase<TChannel>.BeginOperationDelegate 的调用之后,由
// System.ServiceModel.ClientBase<TChannel>.InvokeAsync(System.ServiceModel.ClientBase.BeginOperationDelegate,System.Object[],System.ServiceModel.ClientBase.EndOperationDelegate,System.Threading.SendOrPostCallback,System.Object)
// 调用的委托。
//
// 参数:
// result:
// System.ServiceModel.ClientBase<TChannel>.InvokeAsync(System.ServiceModel.ClientBase.BeginOperationDelegate,System.Object[],System.ServiceModel.ClientBase.EndOperationDelegate,System.Threading.SendOrPostCallback,System.Object)
// 通过对System.ServiceModel.ClientBase<TChannel>.BeginOperationDelegate 的调用所返回的结果。
//
// 返回结果:
// 一个 System.Object 数组,包含调用异步方法的结果。此项操作可能会有多个返回值,这些值都将返回到该对象数组中。
protected delegate object[] EndOperationDelegate(IAsyncResult result);

// 摘要:
// 存储客户端进行异步调用的结果。
protected class InvokeAsyncCompletedEventArgs : AsyncCompletedEventArgs
{
// 摘要:
// 获取客户端进行异步调用的结果。
//
// 返回结果:
// 一个 System.Object 数组,包含客户端进行异步调用的结果。
public object[] Results { get; }
}
}
}

可以看到该类定义有一定重载定义。ClientBase<T>类通过泛型类型参数识别代理封装的服务契约。ClientBase<T>的Channel属性类型就是泛型参数的类型,ClientBase<T>的子类通过Channel调用它指向的服务契约的方法(怎么理解?)实际上,简单理解的话,T就是IMyContract(服务契约)。服务如果只有一个终结点,客户端可以省略构造函数里的终结点名,否则需要根据重载来填写构造函数里的终结点来实例化一个代理,例子

ServiceReference1.ServiceClient sc = new WindowsFormsApplication2.ServiceReference1.ServiceClient();

编程方式配置客户端

WSHttpBinding wsBinding = new WSHttpBinding(); //绑定
EndpointAddress endpointaddress
= new EndpointAddress("http://localhost:90
MyContraceClient proxy
");//终结点= new MyContraceClient(wsBinding, endpointaddress); //代理类实例化
proxy.method(); //通过代理类调用服务方法
proxy.Close();

WCF体系架构

宿主体系架构

posted on 2011-02-23 09:03  人的本质是什么?  阅读(632)  评论(0编辑  收藏  举报