How to write a WCF REST service that image data can be POSTed to? - wcf

I've been looking for examples for how to write a WCF REST service that allows image data to be POSTed to. I may be missing something (I generally am), but does anyone know how to do it? Is it as simple as getting the HTTP request from inside your WCF REST service, and extracting the binary data? If so, is there an example as to how to do that? Would I be able to do that using WebOperationContext.IncomingRequest?
Or, is there a way to do something like this...
[ServiceContract]
public interface IImage
{
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "/images")]
void StoreImage(byte[] imageData);
}
Any ideas?

This post is more or less the answer I was looking for.

Related

wcf rest service xml structure

I am using Entity framework and wcf rest service for my project.
what i required is i want to change the xml structure which is generated like this
<ArrayOfBug>
<Bug>
<BugID>1</BugID>
<PageName>Home.aspx</PageName>
<BugDescription>Bug Testing</BugD`enter code here`escription>
<Priority>H</Priority>
</Bug>
</ArrayOfBug>
-------------
I need Attributes instead of element like this
<ArrayOfBug>
<Bug BugID="1" PageName="Home.aspx" BugDescription="Bug Testing" Priority="H" >
</Bug>
</ArrayOfBug>
what is the best way to do so?
My interface is like this
[OperationContract]
[WebGet(BodyStyle=WebMessageBodyStyle.Bare, UriTemplate = "/SelectAllBug", ResponseFormat = WebMessageFormat.Xml )]
List<Bug> SelectAllBug();
thanks in advance
Monish
There is no out-of-box way in WCF to do what you want.
You can extend WCF by creating a custom message formatter as shown in this good blog post. The down-side is that you'll need to invest some time in understanding how the WCF message processing pipeline works to potentially handle formatting both the request and response messages.

Sending Video Data to WCF Restful service as POST

I was having a problem sending video data to a WCF restful service using post, my contract looks like this
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat=WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "UploadMovie")]
string UploadMovie(Stream stream);
This works ok when I'm sending some text data but does not work when I attempt to send Video data, I have some exception catching in place but it seems like the request is not even being processed, since I get no response and no exceptions get logged... anyone have any input on this?
you can use the svclog app to determine exactly what is happening. You just have enable wcf logging
http://wcfsecurity.codeplex.com/Wiki/View.aspx?title=How%20to%20enable%20WCF%20message%20logging
If you are instantiating all wcf in code, you can just add an app.config with the correct information to your code directory and this will work. Just remember to take it out when you're done. Most likely you have some exception that the WCF framework is catching. That happened to me on a message that seemingly vanished into thin air.
Check out this post on streaming in Restful WCF. It's reverse of what you want to do, but using the AdapterStream class will probably help.
The problem was the buffer size, i ended up splitting the video up into chunks and sending it like that, thanks everyone for their input

Getting a WCF service to map POST'd parameters to arguments

I have a legacy service I'm looking to update to WCF and one of it's behaviours is to allow clients to POST a request that has something like:
MyService.asmx/ProcessDocument
With Post data looking like:
request=<big block of xml>
Now in the ASMX days this service accepted a single string parameter i.e:
public void ProcessDocument(string request) {
}
So far I have only gotten this to work in WCF by using a Stream as of the advice in this post here:
http://www.dennydotnet.com/post/2008/09/16/WCF-REST-and-POST-Lets-Dance!.aspx
A Stream will work, there are just more steps involved to make it work for something that seems to it should be supported out of the box.
I am pretty new to WCF - what am I missing?
OK, this sample got me to most of where I needed to go:
http://msdn.microsoft.com/en-us/library/bb943485.aspx
I now have it working as required.

WCF Chunking / Streaming

I'm using WCF and want to upload a large file from the client to the server. I have investigated and decided to follow the chunking approach outlined at http://msdn.microsoft.com/en-us/library/aa717050.aspx
However, this approach (just like streaming) restricts the contract to limited method signitures:
[OperationContract(IsOneWay=true)]
[ChunkingBehavior(ChunkingAppliesTo.InMessage)]
void UploadStream(Stream stream);
The sample uses the rather convenient example of uploading a file from a fixed path and saving it to a fixed path on the server. Therefore, my question is how do I pass additional parameters to specify things like filename, filepath etc.
eg. I would like something like:
[OperationContract(IsOneWay=true)]
[ChunkingBehavior(ChunkingAppliesTo.InMessage)]
void UploadStream(Stream stream, String filePath);
Thanks in advance,
Mark.
This article explains how to use the MessageHeader attribute to force things to be passed in the header, and therefore not count as a parameter. So, instead of passing a stream and other meta data, create a class that has the attribute MessageContract and mark all of the meta data as a MessageHeader. Then, mark the stream as a MessageBodyMember (which the article incorrect calls "MessageBody"). Have your UploadStream method take a single parameter whose type is that of the MessageContract class you've just created. I've done this successfully, but I haven't done it in tandem with chunking. Good luck.
You could make your service session-ful and have an initialization method in the contract with the IsInitiating property set to true. Something like:
[OperationContract(IsInitiating = true)]
void InitializeUploadService(string filename);
[OperationContract(IsOneWay = true, IsInitiating = false)]
[ChunkingBehavior(ChunkingAppliesTo.InMessage)]
void UploadStream(Stream stream);
I have never tried it with streaming services but it should basically make WCF enforce that InitializeUploadService is always called before UploadStream.
More documentation can be found here:
http://msdn.microsoft.com/en-us/library/system.servicemodel.description.operationdescription.isinitiating.aspx
I would look at MessageContracts and add those values as message headers to your object. This should allow you to pass the stream and any values related to the stream as message headers.
Setting up the maxItemsInObjectGraph in the Client side and Server side worked for me.
(Dont forget the client side.) http://social.msdn.microsoft.com/Forums/en/wcf/thread/0af69654-2d89-44f3-857a-583b57844ca5

Accepting form fields via HTTP Post in WCF

I need to accept form data to a WCF-based service. Here's the interface:
[OperationContract]
[WebInvoke(UriTemplate = "lead/inff",
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int Inff(Stream input);
Here's the implementation (sample - no error handling and other safeguards):
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 besides parsing the incoming stream?
I remember speaking to you about this at DevLink.
Since you have to support form fields the mechanics of getting those (what you are currently doing) don't change.
Something that might be helpful, especially if you want to reuse your service for new applications that don't require the form fields is to create a channel that deconstructs your stream and repackages it to XML/JSON/SOAP/Whatever and have your form clients communicate with the service through that while clients that don't use forms can use another channel stack. Just an idea...
Hope that helps. If you need help with the channel feel free to let me know.
You can serialize your form fields with jquery and package it as json request to wcf service.