I have a requirement to implement an API using .NET technologies. The protocol is "plain old XML" (POX) over HTTP. There are 6 API calls, but all delivered from the same URI. The API method is identified by a "method" attribute in the top-level XML element. The child XML elements in the request and response depend on which method is being invoked.
For example:
<req method="GetStuff"><id>42</id></req>
might give response
<resp method="GetStuff"><Thing name="Bob"/></resp>
Or:
<req method="Status"><verbose>false</verbose></req>
might give response
<resp method="Status"><status>OK</status></resp>
I've considered WCF - I can see that following the example for a REST API I can easily get XML serialised and deserialised but cannot see an obvious way to get the method attribute to map to separate ServiceContract
methods. I could also consider MVC but would need to mess about in the MVC pipeline somewhere to map the method to the appropriate Action.
Suggestions please?
The method dispatch technique that you want (
<req method='methodName'>
) is a non-standard technique, so there is no built-in support for it. You have a couple of options:1) Just do the dispatch yourself. E.g. if using WCF, have a single OperationContract method that looks at the attribute and then calls one of the 6 methods you need.
2) Create a custom dispatcher. For WCF, take a look at http://msdn.microsoft.com/en-us/library/ms734665%28v=vs.110%29.aspx . For ASP.NET Web API, you can probably start here - http://www.asp.net/web-api/overview/advanced/http-message-handlers - but I have less experience with it so can't tell you for sure.
Approach #1 is certainly much easier, but if you're going to create a lot of such APIs, #2 may be the way to go. Or just switch to something more standard - either SOAP, or a "true" RESTful API where all the routing/dispatch information is in the URL itself and not in the message body.