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

mar*_*c_s 7 wcf asynchronous svcutil.exe .net-4.5

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

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

c:> svcutil.exe /n:*,MyNamespace /out:WebService MyService.wsdl
Run Code Online (Sandbox Code Playgroud)

我创建了这个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();
Run Code Online (Sandbox Code Playgroud)

这不起作用 - 如果我尝试在a中实例化该接口的实现 ServiceHost

using (ServiceHost svcHost = new ServiceHost(typeof(MyService01Impl)))
{
    svcHost.Open();
    Console.WriteLine("Host running ...");

    Console.ReadLine();
    svcHost.Close();
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

在同一个具有相同名称的合同中不能有两个操作,"MyService01Impl"类型中的方法"IsAlive"和"IsAliveAsync"违反了此规则.您可以通过更改方法名称或使用OperationContractAttribute的Name属性来更改其中一个操作的名称.

为什么svcutil突然生成不起作用的代码?

如果我使用的/async关键字有svcutil,然后我得到了"旧式"异步模式与BeginIsAliveEndIsAlive和东西再工作-但我怎么能告诉WCF/svcutil生成没有异步的所有的东西?

Pan*_*vos 5

svcutil 默认情况下,使用同步和基于任务的方法生成代理类,例如:

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

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

public interface IService1
{
   ...
   string GetData(int value);
   ...
}
Run Code Online (Sandbox Code Playgroud)

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

public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1
Run Code Online (Sandbox Code Playgroud)

最后,/serviceContract交换机仅生成生成虚拟服务所需的接口和DTO