views:

151

answers:

5

I will be interfacing with a third-party - the exchange of information is done as follows.

If my application is the client and the third party is the server, then:

  • Server-to-client: I give them a pre-defined URL and they push data with HTTP POST. The POST parameters contain a variable "xml" with the request in a proprietory XML format.
  • Client-to-server: this is done in a similar way only this time I originate the request (irrelevant to the question I guess)

Is this possible with WCF? If so, how? If not, ASP? MVC? Whatever the case, please give some pointers as to the specific steps.

Thank you

Edit: The response given is a HTTP 200 along with a proprietory XML response. Typically, the flow of data will be:

Server pushes request to client service => Generate response (usually will involve the client requesting the server to do something) => Give HTTP 200 back with response XML

A: 

I don't believe you can do this with WCF, though I'm no expert at WCF... If it were me I would create an ASP.Net ashx file for receiving the data and then create an aspx page where you can do a file upload/send using WebClient, or something along those lines.

Patricker
@Patricker you can send and receive data through WCF
msarchet
@msarchet of course you can send and receive data through WCF, I just wasn't sure if you could specify that it should be POST and the parameter name. Looks like Coding Gorilla has a good post though.
Patricker
+1  A: 

I would take a look at WCF, without knowing detailed specifics of what you are doing I can tell you this.

Windows Communication Foundation (WCF) is created for communicating between a hosted service and a client, so it should do exactly what you want.

More information here MSDN WCF

msarchet
+4  A: 

I would suggest something along the lines of a WCF REST service. You could set it up to accept a POST request with a single string parameter. The string would then contain your XML, which you could load into an XDocument or something similar to parse your XML.

This might look something like this:

[OperationContract]
[WebInvoke(Method="POST")]
public string AcceptRequest(string Xml)
{
  // Do something here
}

You didn't really specify what you have to do with the post, or how you have to respond, so this might not work depending on those requirements.

Rick Strahl has a series of blog posts about WCF REST services, you might start here: http://west-wind.com/weblog/posts/310747.aspx.

Coding Gorilla
+1 I like the idea of making it RESTful. It would both enforce a certain discipline in design and keep the door open for extensibility in the future.
Daniel Dyson
@Coding Gorilla good answer, I think that REST is the way to go here
msarchet
I agree with RESTful approach but I don't agree with the return value of type string. It will return <string>Encoded XML</string> Use DataContract, Xml serializable type or XElement.
Ladislav Mrnka
The return type of string was simply for the demonstration purpose, as I mentioned I wasn't sure what his requirements were as regards to responding.
Coding Gorilla
Thanks, your answer is a lot more specific. The response will also be proprietory XML, with a HTTP 200 status necessary to acknowledge it. Can I achieve this with WCF REST also?
everwicked
It should be, you might need to play with some variations of the WebInvokeAttribute (http://msdn.microsoft.com/en-us/library/bb515593.aspx) to get it to work out properly. But if you set the ResponseFormat to XML, and the body format to Bare, you should be able to spit out just about anything you want.
Coding Gorilla
There's an added complication which I've just noticed. Sometimes the data comes in as text/xml and the XML request from the server is as-is and sometimes it's application/x-www-form-urlencoded in which case the XML request is escaped etc. Can it handle both things? If so, could you also give me a pointer? Thank you
everwicked
If you're simply accepting the incoming POST as a string value, then you can transform, manipulate, un-encode/re-encode it as you see fit.
Coding Gorilla
Great. And how does one know what the encoding is via the WCF API? Also, is there any benefit to doing it with WCF versus a plain IHttpHandler as suggested below? I see the comment about extensibility but given that this communication is with a third-party whose API we have no influence over, it's doubtful it will ever change.
everwicked
WCF isn't going to tell you anything about the contents of the POST (ie. the body of the POST) you're going to have to look at the string (particularly because you said it can be different at different times) and determine for yourself what format it is. The advantage WCF will give over a Handler (.ashx) is that WCF will do a lot of the plumbing for you and give you the contents of the post; with a handler you're going to have to read the entire request and strip out the headers and such you don't need.
Coding Gorilla
@Coding Gorilla: IHttpHandler is pretty easy--how tricky is `Xml.LoadString(Request.Form["xml"])`?
Wyatt Barnett
@WyattBarnett: My understanding is this is going to be a direct POST request, not a form post. There won't be any form fields, just a raw body.
Coding Gorilla
Request.Form is a misnomer carried from the old days of ASP.old -- it access the raw post key/value pairs, which the question indicates they are using.
Wyatt Barnett
@WyattBarnett: I understand about Request.Form, I missed the part of his question which indicated it was a key/value pair. I was thinking it was just posting with the XML in the body.
Coding Gorilla
A: 

imho you are mixing different technologies here.

Sure you could use classic asp.net, asp.net mvc and WCF to do so, but it begs the question: how do you need to run this? specially the request that you initiate, when its initiated?

WCF is designed precisely for communication scenarios, so it might be the most flexible option you have. What is not mentioned, is in response to what you need to initiate the request on your side.

It might be part of a page you are presenting to someone, in that case you also want asp.net (mvc) to initiate the operation. It might be needed to be run as a service, in which case you can stick to a WCF only solution. It might be needed to be initiated from a client application, in which case you also need said application.

Above said, if you already need to do asp.net (mvc) and the exchange is very simple, you could do it without introducing WCF. Less learning pieces.

Finally if HTTP POST + xml is not just a custom thing, but a web service call, you might want to stick to WCF to receive the requests. Specially as its possible you'll need other WS pieces, like WS-Security.

eglasius
+1  A: 

I'd really wonder why you are doing old-school web services (custom XML over custom HTTP endpoint) in 2010. That said, I think in this scenario you probably want either:

a) Ye olde plain IHttpHandler taking raw input and returning a response.
b) An ASP.NET MVC App doing the same thing on a dedicated controller.

Decision really would get driven by what else you need to deploy with this app but either would work. Kickers here are responses are synchronous and the domain is relatively focused.

I think you could get there with WCF, but that adds so much configuration complexity to what is really a simple operation that it is probably best avoided.

Wyatt Barnett
Thanks Wyatt - I agree on the added complexity of WCF, especially since I'm not trained on it just yet. I'm not sure what you mean about "what else you need to deploy with this app" - it will receive a request and probably immediately acknowledge it/respond and add it to a processing queue which will in turn do what needs to be done and send a client-server request. I hope that makes sense. Which of the two options would you recommend and what kind of book should I get for learning about it?
everwicked
By "what else you need to deploy with this app" I meant "are there any other web components, such as documentation, a client site, a management tool?" Mainly because service-wise, it is a wash, but other components could push it towards doing one of several things . . .
Wyatt Barnett
Oh, missed the book part. Not sure what would be good, but I'd just read the documentation on IHttpHandlers and go from there. They are pretty simple until you get too fancy for your own good . . .
Wyatt Barnett