How to upload a file to a WCF Service? - wcf

I've build a WCF Service to accept a file and write it to disk. The front-end consists of a page with SWFUpload which is handling the upload on the client side. Apparently, SWFUpload posts the data with a Content Type of: multipart/form-data.
I would think this ok but on the Service side I get an error. The error is "ProtocolException" expecting text/xml. I've tried different message encodings in the bindings but nothing seems to work.
How can I get this file uploaded using multipart/form-data?

#jdiaz,
#JasonS is right, to upload file you need to transfer it as a byte stream. You need to use WCF streaming. For example on how to upload file via WCF see an article from http://kjellsj.blogspot.com

What you want to use is probably MTOM, if you want it to be standard. Using this, you can have MIME multiparts messages.
You then have to read the file as a stream and stuff it into one of the parameters of the request.

It might be that your WCF service targets .NET Framework 3.5 and your IIS is running on .NET Framework 4.0. In this case (framework mismatch) you need to modify your service.

I believe you are going to have to tranfer the file as a byte array to WCF. You will need to handle the post from SWFUpload and convert to a byte array before sending to your service.

Related

WCF REST Service save text file while hosting using Windows services

I wrote a small test WCF REST service using C#, within the WCF service, if it receive POST info from client, the WCF will collect the HTTP Request message from client and write it to a text file. I am using Stream to collect HTTP request and using StreamWriter to write it to a text file. I can specify any folder/path for the text file.
At this point, everything is working when I host the WCF REST service using a local console app.
However, I want to host the WCF service by using Windows service, the Windows service self is fine, it can be started/stoped no issue, my issue is, when client send HTTP POST request, the WCF suppose to create the text file but I can't see the text file. I tried to search the file on the PC, I could not find it, the text file actually never generated (in my view, Windows Service is not responsible to write the file, it is still WCF responsibility)
btw, the HTTP GET works from client (means the WCF Windows service works at least for GET).
any hints are much appreciated

Odata compression - Is there ANY support? (WCF Data Services 5.0 for WinRT)

Context:Currently using WCF Data Services 5.0 with Odata for a Windows 8 Store application.
Have been trying to compress the OData coming over the wire from my service today and running into plenty of issues surrounding support.
binary serialization isn't supported.
json isn't supported (library throws 'application/json' is currently not supported in Content-Type header)
json lite isn't released yet
adding gzip requests in the http response works (server returns compressed) - but client side lib doesn't seem to want to deserialize it. (xml parsing exceptions)
My final attempt today was to try and get a hold of the http response, and to manually deserialize the stream myself (GZipStream isn't available but i know there are 3rd party alternatives) - but I couldn't find ANY Microsoft.Data.Services.Client.WindowsStore events/overrides that would expose the stream to me.
I wanted to implement OData to leverage the performance and efficiency gains over standard WCF Web services - but with no way to compress the stream over the wire i don't get any.
Does anyone have any experience with this, or advice of how to approach?
Have you followed the same steps here to get json response.The client has to send MaxDataServiceVersion header:
http://blogs.msdn.com/b/astoriateam/archive/2012/04/11/what-happened-to-application-json-in-wcf-ds-5-0.aspx
I also checked that Odata 5.1.0 has been released on nuget. This has the better json support:
https://nuget.org/packages/Microsoft.Data.Services/5.1.0
Your other option is to use asp.net web api and try the odata support:
http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData

MTOM, Axis2, WS-Security and signed attachements

I am working on a bug we have encountered in one of our web services. We are using Axis2 1.4.1 running on OC4J. The issues is that when sending multiple signed binary attachments to the service with MTOM, the service only gets one of the attachments. If I disable WS-Security and send the same request to the web service, all the attachments get through. This seems to only happen when MTOM "determines" the attachments are large enough to warrant breaking them out into separate MIME sections. If they are quite small, then MTOM just base64encodes them and leaves them inline and all data gets through.
All the request SOAP Xml is produced correctly. That is, all the multiple attachments are being sent to the service and all the MIME boundaries are present and all the XOP includes are in place, as I have snooped the requests with Eclipses TCP/IP Monitor and also with the output provided in SOAP Sonar.
Has anyone else run into an issues when trying to send signed content to an Axis2 web service?
I found the answer, sort of, on my own question after spelunking through many JIRA tickets on the AXIS2 JIRA. The fix, or work around actually, is described in AXIS2-4973. Using the method described, Axis2 seems to generate the code the parses multiple MTOM attachments correctly.

IIS6 cannot handle WCF json response

I wonder if you could help me with this. I have a .NET 3.5 WCF RESTful service that returns json. Service works fine on my local machine but when I deploy it on IIS6 I get this error: The server encountered an error processing the request. See server logs for more details.
The WebInvoke method is GET and when I try to access the service method in browser on the IIS6 machine I get a prompt that asks me to download a file (with the response of the GET request).
I'm really baffled by this as when I choose to download and open the file I see the json that is suppossed to be returned....Strange behavior by IIS.
Any clues on this?
The answer was rather trivial...
Because my wcf operation was returning a List of objects for some reason IIS insists in that case the BodyStyle to be WebMessageBodyStyle.Wrapped , unlike local development service where the response was returned correctly both with Bare and Wrapped formats.
Thanks for all suggestions guys.

WCF - Response Format

For some reason, I have the idea that a client can ask a WCF service for the request to be of a certain format. Is this possible or do I need to create the same method with different signatures for each different response type?
For instance, if a Silverlight client accesses my WCF service, I want to using binary encoding. However, if my WCF service is called via JQuery or ASP.NET AJAX, I want to return JSON. Is this possible?
Thank you!
You are talking about the binding used, and yes, it is possible to host using multiple bindings. If you're talking about hosting in IIS, and you want binary serialised output, you'll need to be running on Windows Server 2008 and use Windows Process Activation Services (WAS).
You'd be accessing a different endpoint address in each case (e.g. net.tcp://... versus http://...).