.NET 4.5中的WCF / svcutil默认情况下会生成不可用的代码 [英] WCF / svcutil in .NET 4.5 generates unusable code by default
问题描述
在.NET 4.5中,使用 svcutil
创建的WCF似乎突然中断了(直到最近我一直仅使用.NET 4.0)...。
With .NET 4.5, my WCF creation using svcutil
suddenly seems to break (I've been using only .NET 4.0 until very recently) ....
使用默认设置,将默认的WSDL转换为C#WCF代理类:
With the default settings I'm using to convert a pre-existing WSDL to my C# WCF proxy class:
c:> svcutil.exe /n:*,MyNamespace /out:WebService MyService.wsdl
我得到了这个C#文件创建:
I get this C# file created:
[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();
这不起作用-如果我尝试在<$ c中实例化此接口的实现$ c> ServiceHost
This doesn't work - if I try to instantiate an implemention of this interface in a ServiceHost
using (ServiceHost svcHost = new ServiceHost(typeof(MyService01Impl)))
{
svcHost.Open();
Console.WriteLine("Host running ...");
Console.ReadLine();
svcHost.Close();
}
我收到此错误:
在同一个合同中不能有两个具有相同名称的操作,类型 MyService01Impl的方法 IsAlive和 IsAliveAsync违反了此规则。您可以通过更改方法名称或使用OperationContractAttribute的Name属性来更改其中一项操作的名称。
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
突然生成不起作用的代码?
Why does svcutil
suddenly generate code that doesn't work??
如果我在 svcutil
中使用 / async
关键字,然后使用 BeginIsAlive
和 EndIsAlive
得到旧式异步模式,事情又恢复了,但是如何工作我告诉WCF / svcutil
生成没有异步的东西吗?
If I use the /async
keyword with svcutil
, then I get the "old-style" async pattern with BeginIsAlive
and EndIsAlive
and things work again - but how can I tell WCF / svcutil
to generate no async stuff at all?
推荐答案
svcutil
默认情况下会生成包含同步和任务的代理类基于方法,例如:
svcutil
by default generates a proxy class with both synchronous and Task-based methods, eg:
public interface IService1
{
...
string GetData(int value);
...
System.Threading.Tasks.Task<string> GetDataAsync(int value);
...
}
要仅使用同步方法生成代理,您需要使用 / syncOnly
。这将省略基于任务的版本:
To generate a proxy with only synchronous methods, you need to use /syncOnly
. This will omit the Task-based version:
public interface IService1
{
...
string GetData(int value);
...
}
在两种情况下,代理类本身都继承来自ClientBase:
In both cases, the proxy class itself inherits from ClientBase:
public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1
最后, / serviceContract
开关仅生成生成请求的接口和DTO。虚拟服务
Finally, the /serviceContract
switch generates only the interface and DTOs necessary for generating a dummy service
这篇关于.NET 4.5中的WCF / svcutil默认情况下会生成不可用的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!