zoukankan      html  css  js  c++  java
  • How and Why to use the System.servicemodel.MessageParameterAttribute in WCF

    MessageParameterAttribute

    MSDN note:

    Controls the name of the request and response parameter names. Cannot be used with Message or message contracts.


    The MessageParameterAttribute, part of System.ServiceModel, is used to control the
    name of parameter and return values in the service description. This attribute has
    only one property,the Name property. In the operation signature,parameters become
    part of the request message body. Each parameter is serialized to an element named
    for the parameter name. The response is serialized to an element named for the operation
    name with “Response” appended. Consider this operation signature:

    [OperationContract]
    string NewOperation(string s);

    what the resulting message body looks like for the request and response message.

    request message:

    <s:Body>
    <NewOperation xmlns="http://www.thatindigogirl.com/samples/2006/06">
    <s>hello</s>
    </NewOperation>
    </s:Body>

    reponse message:

    <s:Body>
    <NewOperationResponse xmlns="http://www.thatindigogirl.com/samples/2006/06">
    <NewOperationResult>IServiceA.NewOperation( ) invoked with </NewOperationResult>
    </NewOperationResponse>
    </s:Body>

    You can apply the MessageParameterAttribute to control serialization. The following code applies this attribute to parameters and return values:

    [OperationContract]
    [return: MessageParameter(Name = "responseString")]
    string NewOperation([MessageParameter(Name = "string")]string s);

    This attribute can be particularly useful when you want to use a keyword or type name in the resulting XSD schema that describes an incoming message. Likewise,
    you can control the XML element name for the return value in a response message.what the resulting messages look like for NewOperation( ) after applying the attribute.

    request message:

    <s:Body>
    <NewOperation xmlns="http://www.thatindigogirl.com/samples/2006/06">
    <string>hello</string>
    </NewOperation>
    </s:Body>

    response message:

    <s:Body>
    <NewOperationResponse xmlns="http://www.thatindigogirl.com/samples/2006/06">
    <responseString>IServiceA.NewOperation( ) invoked with hello</responseString>
    </NewOperationResponse>
    </s:Body>

    As with keywords and type names,you may also find MessageParameterAttribute useful for standardizing on XML element naming conventions separate from CLR
    naming conventions. For example,you may use camel case for parameter names and prefer Pascal case for XML.

  • 相关阅读:
    Java 获取本机IP
    IDEA2017.3.1破解激活
    java访问https绕过证书信任
    windows版nginx+ftp实现图片服务器的搭建
    json转字符串,json转list,json转pojo的工具类
    文件上传到ftp服务工具类
    一个servlet处理多个功能
    一二级栏目的查询
    后台接收URL地址的参数
    SSH邮箱验证与激活
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2563831.html
Copyright © 2011-2022 走看看