.NET 4.5中的WCF / svcutil默认生成不可用的代码

时间:2015-08-10 07:53:20

标签: wcf asynchronous .net-4.5 svcutil.exe

使用.NET 4.5,我使用svcutil创建的WCF似乎突然出现(我直到最近才使用.NET 4.0)....

使用默认设置将预先存在的WSDL转换为我的C#WCF代理类:

c:> svcutil.exe /n:*,MyNamespace /out:WebService MyService.wsdl

我创建了这个C#文件:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="MyService.IMyService1")]
public interface IMyService1
{
    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IMyService1/IsAlive", ReplyAction="http://tempuri.org/IMyService1/IsAliveResponse")]
    [System.ServiceModel.FaultContractAttribute(typeof(MyService.MyFault), Action="http://tempuri.org/IMyService1/IsAliveErrorInfoFault", Name="MyServiceErrorInfo", Namespace="http://schemas.datacontract.org/2004/07/MyService.Types")]
    string IsAlive();

    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IMyService1/IsAlive", ReplyAction="http://tempuri.org/IMyService1/IsAliveResponse")]
    System.Threading.Tasks.Task<string> IsAliveAsync();

这不起作用 - 如果我尝试在ServiceHost

中实例化此接口的实现
using (ServiceHost svcHost = new ServiceHost(typeof(MyService01Impl)))
{
    svcHost.Open();
    Console.WriteLine("Host running ...");

    Console.ReadLine();
    svcHost.Close();
}

我收到此错误:

  

同一合同中不能有两个具有相同名称,方法和IsAlive&#39;的操作。和'IsAliveAsync&#39;在类型&#39; MyService01Impl&#39;违反这条规则。您可以通过更改方法名称或使用OperationContractAttribute的Name属性来更改其中一个操作的名称。

为什么svcutil会突然生成无法运行的代码?

如果我将/async关键字与svcutil一起使用,那么我会得到&#34;旧式&#34;与BeginIsAliveEndIsAlive的异步模式再次有效 - 但我怎样才能告诉WCF / svcutil生成 no async 的内容?

1 个答案:

答案 0 :(得分:5)

默认情况下,

svcutil会生成一个包含同步和基于任务的方法的代理类,例如:

public interface IService1
{
    ...
    string GetData(int value);
    ...    
    System.Threading.Tasks.Task<string> GetDataAsync(int value);
    ...
}

要仅使用同步方法生成代理,您需要使用/syncOnly。这将省略基于任务的版本:

public interface IService1
{
   ...
   string GetData(int value);
   ...
}

在这两种情况下,代理类本身都继承自ClientBase:

public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1

最后,/serviceContract开关仅生成生成虚拟服务所需的接口和DTO

相关问题