您的位置:首页 > 其它

[WCF]使用Svcutil.exe生成客户端代理

2012-06-07 11:20 489 查看



svcutil.exe



参数



1 /async

/async 同时生成同步和异步方法签名。


默认设置:只生成同步方法签名。

缩写形式:/a



2 /tcv:Version35

/tcv:Version35


指定应用程序针对 .NET Framework 的哪个版本。有效值为:Version30 和 Version35。默认值为 Version30。

缩写形式:/tcv

Version30:如果为使用 .NET Framework 3.0 的客户端生成代码,则使用 /tcv:Version30。

Version35:如果为使用 .NET Framework 3.5 的客户端生成代码,则使用 /tcv:Version35。如果将 /tcv:Version35
与 /async 开关一起使用,则会同时生成基于事件的异步方法和基于回调/委托的异步方法。



3/collectionType:<类型>

/collectionType:<类型>


从架构中生成代码时,指定要用作集合数据类型的完全限定或程序集限定名称。

缩写形式:/ct



4/reference:<文件路径>

/reference:<文件路径>


引用指定程序集中的类型。在生成客户端时,使用此选项来指定可能包含类型的程序集,这些类型表示所导入的元数据



无法使用此开关指定消息协定和 XmlSerializer 类型。

如果引用了 DateTimeOffset,则会使用此类型,而不是生成新类型。如果应用程序是使用 .NET Framework 3.5 编写

的,则 SvcUtil.exe 会自动引用 DateTimeOffset。

缩写形式:/r

5/enableDataBinding

/enableDataBinding


在所有数据协定类型上实现 INotifyPropertyChanged 接口以启用数据绑定。

缩写形式:/edb



示例:



1

生成同步,带事件的异步代码,集合使用System.Collections.ObjectModel.ObservableCollection集合,指定程序集引用

svcutil /a /d:d:/temp http://localhost:1998/Implement/AgriProductService.svc /ser:DataContractSerializer

/tcv:Version35 /ct:System.Collections.ObjectModel.ObservableCollection`1 /reference:C:/"Program

Files"/"Reference Assemblies"/Microsoft/Framework/.NETFramework/v4.0/WindowsBase.dll



2

生成同步,带事件的异步代码,集合使用System.Collections.Generic.List集合

svcutil /a /d:d:/temp http://localhost:1998/Implement/AgriProductService.svc /ser:DataContractSerializer

/tcv:Version35 /ct:System.Collections.Generic.List`1



3

生成同步,带事件的异步代码,集合映射为数组

svcutil /a /d:d:/temp http://localhost:1998/Implement/AgriProductService.svc /ser:DataContractSerializer

/tcv:Version35


关于svcutil.exe的详细介绍,可以参看微软的MSDN。

ServiceModel 元数据实用工具 (Svcutil.exe)

如果生成的代理类是要给silverlight用,还需要手动修改一下。因为silverlight不支持同步操作,所有需要删除代理类中的同步操作的代码,只保留异步的代码,还有就是需要添加下面的代码,用于open和close客户端的代码。

要是有只生成异步代码的参数就更好了,好像目前还没有发现,默认生成同步代码,加上/async参数就同时生成异步代码。

下面的代码添加到client类中,public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1,这个类中。

private BeginOperationDelegate onBeginOpenDelegate;

private EndOperationDelegate onEndOpenDelegate;

private System.Threading.SendOrPostCallback onOpenCompletedDelegate;

private BeginOperationDelegate onBeginCloseDelegate;

private EndOperationDelegate onEndCloseDelegate;

private System.Threading.SendOrPostCallback onCloseCompletedDelegate;

public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted;

public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted;

private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState)
{
return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState);
}

private object[] OnEndOpen(System.IAsyncResult result)
{
((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result); return null;
}

private void OnOpenCompleted(object state)
{
if ((this.OpenCompleted != null))
{
InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
}

}

public void OpenAsync()
{
this.OpenAsync(null);
}

public void OpenAsync(object userState)
{
if ((this.onBeginOpenDelegate == null))
{
this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen);
}
if ((this.onEndOpenDelegate == null))
{
this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen);
}
if ((this.onOpenCompletedDelegate == null))
{
this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted);
}
base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState);
}

private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState)
{
return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState);
}

private object[] OnEndClose(System.IAsyncResult result)
{
((System.ServiceModel.ICommunicationObject)(this)).EndClose(result); return null;
}

private void OnCloseCompleted(object state)
{
if ((this.CloseCompleted != null))
{
InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
}
}

public void CloseAsync()
{
this.CloseAsync(null);
}

public void CloseAsync(object userState)
{
if ((this.onBeginCloseDelegate == null))
{
this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose);
}
if ((this.onEndCloseDelegate == null))
{
this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose);
}
if ((this.onCloseCompletedDelegate == null))
{
this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted);
}
base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState);
}


还要提醒大家的是,用svcutil生成的异步访问代码,直接给silverlight使用,还有有点问题的。和使用VS2010添加服务生成的代码相比,有一些出入,比如没有实现ChannelBase,没有在Client中override CreateChannel方法,还有一些局部的差异,导致使用起来会有一些不同。

出现上面的问题,不知道是我没有用对svcutil的参数,还是它们本身就是有一些不同的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  wcf svcutil