How can I control the name of a generic WCF Message Contract

风格不统一 提交于 2021-02-07 11:19:15

问题


I'm generating a WCF service using the message contract model.

I have created a generic request message contract like so:

[MessageContract]
public Request<T>
{
    [MessageBodyMember]
    public T Details { get; set; }
}

I'm used to using [DataContract(Name="Contract{0}")] to produce readable names for generic data contracts, but this approach does not seem to work for me using message contracts.

Is there a way to achieve the same behaviour using the message contract model?


回答1:


It seems like a lot of work for what you want to accomplish, but I believe you can create a MessageInspector which will allow you to interact directly with the XML.

Client message inspectors implement the IClientMessageInspector interface and service message inspectors implement the IDispatchMessageInspector interface.

http://msdn.microsoft.com/en-us/library/aa717047.aspx

Any service (dispatcher) message inspector must implement the two IDispatchMessageInspector methods AfterReceiveRequest and BeforeSendReply.

The link goes into much more detail, but once you have these implemented, you should be able to add the inspector to your web.config and you should be all set.




回答2:


There's a WrapperName and WrapperNamespace property on the MessageContract attribute that I think does the same thing. E.g.,

[MessageContract(WrapperName = "FooMessage", IsWrapped = true)]
public class Request<T>
{ ... }

Note the addition of the IsWrapped property to indicate that the message should be serialized into the wrapper element.



来源:https://stackoverflow.com/questions/3961654/how-can-i-control-the-name-of-a-generic-wcf-message-contract

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!