如何在wcf中使用MessageParameterAttribute

Arc*_*hie 3 wcf data-transfer servicecontract

我想知道wcf中MessageParameterAttribute的用途是什么.

在我的功能:

[OperationContract]
public float GetAirfare(
[MessageParameter(Name=”fromCity”)] string originCity,
[MessageParameter(Name=”toCity”)] string destinationCity);
Run Code Online (Sandbox Code Playgroud)

我在实现中的任何地方甚至在使用服务时都不使用fromCity或toCity.那是什么意思给它一个名字?

Ben*_*ard 5

此属性用于控制序列化.当您想在描述传入消息的结果XSD架构中使用关键字或类型名称时,它会特别有用.同样,您可以在响应消息中控制返回值的XML元素名称.它也可以是标准化XML元素命名约定的有用属性,与CLR命名约定分开.例如,您可能更喜欢使用camel case作为参数名称,使用Pascal case作为XML.

如果我们使用您提供的代码段作为示例,请求将如下所示:

<s:Body>
    <GetAirFare xmlns="yournamespacehere">
        <fromCity>Chicago</fromCity>
        <toCity>Las Vegas</toCity>
    </GetAirFare>
</s:Body>
Run Code Online (Sandbox Code Playgroud)