问题描述
限时送ChatGPT账号..在使用 WCF 和 OperationContracts 时,我定义了以下方法:
While using WCF and OperationContracts I have the following method defined:
[OperationContract]
[FaultContract(typeof(ValidationFault))]
[FaultContract(typeof(FaultException<ExceptionDetail>))]
int DoSomething(int someId, MyComplexType messageData);
当 WCF 运行时将其转换为 WSDL 时,它最终会为参数 someId 和 messageData 列出 minoccurs="0"(如果缺少这些参数,随后会引发运行时错误).
When this gets translated to a WSDL by the WCF runtime, it ends up with with minoccurs="0" listed for the parameters someId and messageData (and subsequently throws a runtime error if these parameters are missing).
如果我使用 SoapUI 生成代理,我会得到如下所示的内容:
If I generate a proxy using SoapUI I get something that looks like this:
<com:DoSomething>
<!--Optional-->
<com:EventId>1</com:EventId>
<!--Optional-->
<com:myComplexType >
<com:id>1</com:id>
</com:myComplexType >
</com:DoSomething>
MyComplexType 中的 id 字段使用 IsRequired="true" 使用 DataMemeber 属性进行标记,因此公开为必填项.
The id field in MyComplexType is marked up with DataMemeber attribute using IsRequired="true" and thus is exposed as mandatory.
如果 WSDL 指定一个参数是可选的,而它不是可选的,这显然是一种误导,但我看不到任何明显的方法来标记 OperationContract 以强制 WCF 根据需要识别和公开这些参数.
It's obviously quite misleading for the WSDL to specify that a parameter is optional when it isn't, but I can't see any obvious way to markup the OperationContract to force WCF to recognise and expose these parameters as required.
我有点困惑,似乎没有明显的方法可以做到这一点(阅读智能感知/msdn/google).或者我会失明并忽略一些明显的东西.
I'm slightly baffled there doesn't seem an obvious way to do this (reading intellisense / msdn / google). Or I'm going blind and overlooking something obvious.
有什么线索吗?
推荐答案
我刚刚写了一个 博文 关于这个主题,上周我自己也遇到了这个问题.它解释了如何修改 WCF 在运行时生成的元数据.
I've just written a Blog post about this subject, as I ran into the problem myself last week. It explains how you can modify the metadata that WCF generates at runtime.
除了下载源文件外,您只需要在合同定义中添加一个属性.像这样:
Aside from downloading the source file, you only need to add an attribute to your contract definition. Like so:
[ServiceContract]
[RequiredParametersBehavior]
public interface ICalculatorService
{
[OperationContract]
int Add(int firstValue, int secondValue);
}
这是更详细解释它的博客文章:使用 WCF 控制 WSDL minOccurs
Here's the Blog post that explains it in more detail: Controlling WSDL minOccurs with WCF
这篇关于如何强制 WCF 使用所需的方法参数(minoccurs=“1")自动生成 WSDL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论