.NET 4.5 中的 WCF/svcutil 默认生成不可用代码 [英] WCF / svcutil in .NET 4.5 generates unusable code by default

查看:24
本文介绍了.NET 4.5 中的 WCF/svcutil 默认生成不可用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 .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();

这不起作用 - 如果我尝试在 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??

如果我将 /async 关键字与 svcutil 一起使用,那么我将使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆