WCF Web Method that Accepts Different Message Types

北战南征 提交于 2019-12-11 07:09:02

问题


Microsofts's WCF is easy to work with when you create Web services where each message has it's own Web method. WCF generates all of the WSDL and everything is easy.

What I want to do is have one Web method that accepts multiple different messages (I don't want to add a mew method every time I add a new message type). The messages themselves will have header information that identify the message type. Once I know the message type, I'll know the structure of the rest of the message.

The only way I've found to do this with WCF is to have the method accept a string, which I parse in as XML and and use. However, I can see no clear way to publish the various message types in the WSDL; so, the whole service is essentially undocumented.

Anyone know of a technique to use in WCF?


回答1:


You can write an operation contract that accepts any message by setting the Action to * and having it take in a Message object:

[ServiceContract]
public interface IMessageContract
{
    [OperationContract(Action = "*", ReplyAction = "*")]
    Message ProcessRequest(Message request);
}

The Message object gives you access to the headers and has methods to deserialize the body.

To export your own WSDL, you will need to implement IWsdlExportExtension on a contract behavior or operation behavior and attach it to your service. This will give you access to a WsdlExporter, and you can create a ContractDescription yourself and call ExportContract to have it appear in the generated WSDL.



来源:https://stackoverflow.com/questions/3166313/wcf-web-method-that-accepts-different-message-types

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