WCF / svcutil in .NET 4.5 generates unused code by default

In .NET 4.5, my WCF creation using svcutil suddenly seems broken (I only use .NET 4.0 until recently) ....

With the default settings that I use to convert a pre-existing WSDL to my C # WCF proxy class:

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

I create this C # file:

 [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(); 

This will not work - if I try to instantiate the implementation of this interface in ServiceHost

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

I get this error:

There cannot be two operations in the same contract with the same name, the IsAlive and IsAliveAsync methods of 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.

Why svcutil unexpectedly generate code that doesn't work?

If I use the /async with svcutil , then I get an โ€œold styleโ€ asynchronous template with BeginIsAlive and EndIsAlive and everything works again, but how can I tell WCF / svcutil generate things without async at all?

+5
source share
1 answer

svcutil by default generates a proxy class with synchronous and task-based methods, for example:

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

To create proxies with only synchronous methods, you need to use /syncOnly . This will lower the task-based version:

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

In both cases, the proxy class itself is inherited from ClientBase:

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

Finally, the /serviceContract switch only generates the interface and DTO needed to create the dummy service

+4
source

All Articles