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

标签 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 中实例化此接口(interface)的实现
using (ServiceHost svcHost = new ServiceHost(typeof(MyService01Impl)))
{
    svcHost.Open();
    Console.WriteLine("Host running ...");

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

我收到此错误:

Cannot have two operations in the same contract with the same name, methods 'IsAlive' and 'IsAliveAsync' in type 'MyService01Impl' violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.



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

如果我使用 /async svcutil 的关键字,然后我得到了带有 BeginIsAlive 的“旧式”异步模式和 EndIsAlive事情又开始了——但我怎么能告诉 WCF/svcutil生成 没有异步 东西吗?

最佳答案

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 switch 仅生成生成虚拟服务所需的接口(interface)和 DTO

关于.NET 4.5 中的 WCF/svcutil 默认生成不可用的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31914303/

相关文章:

c# - 实体对象的 XML 序列化

c# - Web API 异步,我做对了吗?

deployment - .NET4.5 应用程序的安装程序和部署建议

c# - 使用图像+文本样式化 WPF 按钮

ios - 使用 dispatch_group_notify 的快速异步请求不起作用

c# winforms - 刷新时调用 InitializeComponent() 函数吗?

.net - WCF 连接的带宽和吞吐量

wcf - 在双工系统WCF中如何区分不同的 channel 实例?

c# - 简单的 WCF 服务,不是从客户端到服务的所有参数

c# - 如何从多个源异步加载 ASP.NET GridView?