【问题标题】:how to use MessageParameterAttribute in wcf如何在 wcf 中使用 MessageParameterAttribute
【发布时间】:2010-05-12 06:44:35
【问题描述】:

想知道wcf中MessageParameterAttribute有什么用。

在我的功能中:

[OperationContract]
public float GetAirfare(
[MessageParameter(Name=”fromCity”)] string originCity,
[MessageParameter(Name=”toCity”)] string destinationCity);

我不使用 fromCity 或 toCity 在实施中的任何地方,甚至在使用服务时。那么给它起个名字有什么意义呢?

【问题讨论】:

    标签: wcf data-transfer servicecontract


    【解决方案1】:

    此属性用于控制序列化。当您想在生成的 XSD 模式中使用关键字或类型名称来描述传入消息时,它会特别有用。同样,您可以控制响应消息中返回值的 XML 元素名称。它也可以是用于标准化 XML 元素命名约定的有用属性,与 CLR 命名约定分开。例如,您可能更喜欢对参数名称使用驼峰式大小写,而对 XML 使用 Pascal 大小写。

    如果我们以您提供的代码 sn-p 为例,请求将如下所示:

    <s:Body>
        <GetAirFare xmlns="yournamespacehere">
            <fromCity>Chicago</fromCity>
            <toCity>Las Vegas</toCity>
        </GetAirFare>
    </s:Body>
    

    【讨论】:

    • 谢谢。但是你什么时候会像这样明确地指定 XSD?你能举个例子吗?
    • XSD 会自动为您生成,假设您使用的是 Visual Studio IDE。如果您或其他客户向 WCF 服务添加服务引用,您会注意到自动生成的 Reference.cs 类如下所示: public float GetAirfare(string fromCity, string toCity) { return base.Channel.GetAirfare(fromCity , 到城市);如果有人使用非 .NET 环境来使用服务并且没有像我们这样自动生成服务代理的工具,他们可能需要手动创建代理类,并且需要 XSD 和 WSDL 来完成。
    猜你喜欢
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多