Question

I wanted to know what is the use of MessageParameterAttribute in wcf.

In my function:

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

I dont use fromCity or toCity anywhere in the implementation or even while using a service. Then whats the point in giving it a name?

Was it helpful?

Solution

This attribute is used to control serialization. It 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. It can also be a useful attribute for standardizing on XML element naming conventions, separate from CLR naming conventions. For example, you may prefer to use camel case for parameter names and Pascal case for XML.

If we were to use your provided code snippet as an example, the request would look like:

<s:Body>
    <GetAirFare xmlns="yournamespacehere">
        <fromCity>Chicago</fromCity>
        <toCity>Las Vegas</toCity>
    </GetAirFare>
</s:Body>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top