使用 .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/