Accepting form fields via HTTP message in WCF

I need to accept form data into a WCF based service. Here's the interface:

[OperationContract] [WebInvoke(UriTemplate = "lead/inff", BodyStyle = WebMessageBodyStyle.WrappedRequest)] int Inff(Stream input); 

Here's the implementation (sampling - error handling and other precautions):

 public int Inff(Stream input) { StreamReader sr = new StreamReader(input); string s = sr.ReadToEnd(); sr.Dispose(); NameValueCollection qs = HttpUtility.ParseQueryString(s); Debug.WriteLine(qs["field1"]); Debug.WriteLine(qs["field2"]); return 0; } 

Assuming WCF, is there a better way to accomplish this other than parsing the incoming stream?

+7
wcf webinvoke
source share
2 answers

I remember how you talked about this in DevLink.

Since you must maintain the form fields, the mechanics of obtaining them (what you are doing now) do not change.

Something that can be useful, especially if you want to reuse your service for new applications that do not require form fields, is to create a channel that deconstructs your stream and repackages it in XML / JSON / SOAP / regardless of your form clients will contact the service through this, while clients who do not use forms can use a different channel stack. Just an idea ...

Hope this helps. If you need help with the channel, feel free to let me know.

+5
source

You can serialize form fields using jquery and pack them as a json request to the wcf service.

0
source

All Articles