views:

55

answers:

1

hello,

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?

+1  A: 

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>
Ben.Vineyard
Thanks. But when would you specify XSD explicitely like this? Can you give any example?
Archie
The XSD is automatically generated for you, assuming you are using the Visual Studio IDE. If you or another client adds a service reference to the WCF service, you will notice that the Reference.cs class automatically generated will look like the following:public float GetAirfare(string fromCity, string toCity) { return base.Channel.GetAirfare(fromCity, toCity);If someone was using a non-.NET environment to consume the service and didn't have tools to auto-generate service proxies like us, they may need to create a proxy class by hand and would need the XSD and WSDL to do it.
Ben.Vineyard