【发布时间】:2016-03-30 14:10:17
【问题描述】:
我正在使用 svcutil 制作代理类,我注意到对于不同的服务,我得到不同的结果。
我正在使用这个 svcutil 命令:
svcutil http://server/SomeService.asmx
/l:c#
/syncOnly
/out:C:\ISomeService
/config:C:\ISomeService.config
/namespace:*,SomeServiceProxy
第一个服务的代理类生成如下代码:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="GetFirstService", Namespace="http://othernamespace.com")]
public partial class GetFirstServiceRequest : SomeServiceProxy.ResponseInfoBase
{
...
}
第二个服务的代理类生成这个:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName = "GetSecondService", WrapperNamespace = "http://somenamespace.com", IsWrapped = true)]
public partial class GetSecondServiceRequest
{
...
}
为什么一个类用 MessageContractAttribute 标记而另一个用 DataContractAttribute 标记? svcutil 如何决定使用其中一种,两种服务的命令相同?
【问题讨论】:
-
那是因为您的一项服务可能正在使用 DataContract 而其他可能正在使用 messageContract 。您可以发布这两种服务的合同定义吗?
-
@PankajKapare 不抱歉,我无法发布定义,我不拥有这两项服务,并且由于 nda 许可而无法发布。我明白你在说什么,有没有办法可以强制第二个使用 DataContractAttribute?
-
您可以尝试
/serializer:DataContractSerializer,但由于 svcutil 默认尝试生成 DataContracts,我怀疑这会改变结果。 - 问题是,你为什么还要关心? (如果你担心丑陋的代码,那么生成的代码几乎总是丑陋的。在它上面使用一个薄的包装层来隐藏废话。;) -
@nodots 我在自定义方法中使用其中一些合同,但随后出现错误,例如 - “操作 XYZ 有一个参数或返回类型,该参数或返回类型由 MessageContractAttribute 属性。为了使用消息合约来表示请求消息...”,这与标记为“DataContract”的其他合约一起工作正常。
标签: wcf svcutil.exe