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.

  • 相关阅读:
    Nginx入门(三)——正向代理
    Nginx入门(二)——双机热备
    Socket
    TCP和UDP
    主线程等待子线程结束后再运行
    H5s播放rtsp和rtmp视频
    Thread.sleep()和Thread.currentThread().sleep()区别
    OpenLayer3入门——[一]
    事件绑定
    cmake和json安装
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2563831.html
Copyright © 2011-2022 走看看