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.

你可能感兴趣的:(parameter)