wcf rest service xml structure - wcf

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.

Related

WCF receiving data over querystring

I am looking to integrate SignalR (.net 4.0) and Sharepoint 2010 (.net 3.5 together).
For this to work, I need a WCF server, which can recieve data posted over a querystring. What would be my best WCF option here in terms of binding? I need to post data over a querystring.
I've actually never head of anyone doing that. Is there a reason you have to use WCF? I would think something like ASP.NET MVC 4's Web API would work better.
If you are using the query string, that means it isn't SOAP, and so you can't use the basicHttpBinding or wsHttpBinding.
webHttpBinding might work for you. And depending on how it is hosted, you might see if there is a current web Request object you can get at.
Edit:
Actually, this might help too: Making a WCF Web Service work with GET requests
And with webHttpBinding, you can specify a URL Template like this:
[OperationContract]
[WebGet(UriTemplate = "/url/{one}?two={two}&three={three}")]
object GetStuff(string one, int two, string three);
assuming you know all the query string parameters.

WCF to consume other REST Services?

I'm trying to figure out if the best way to consume and use a 3rd party API that is REST is to use WCF or just use System.Runtime.Serialization.Json Namespace or the WebClient object in .NET and create my own methods to send and receive json objects to and from the REST service I'm consuming.
So far I've only seen consuming REST json of an existing WCF service. Can you use WCF to consume and work with (request/response) any json based REST service outside of .NET?
Yes, you can consume Flickr services using WCF as described here. You just need to change the ResponseFormat in the WebGet (and WebInvoke) attributes to be Json.
However, my experience is that it is rather painful when you deal with stuff like error handling or complex authentication schemes. I found it simpler to manually write the client using the WebRequest class.

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

Best way to support "application/x-www-form-urlencoded" post data with WCF?

I'm building a WCF service based on a W3C specification which defines a RESTful web service endpoint that accepts "application/x-www-form-urlencoded" post data. WCF doesn't support this type of message encoding by default and I have found a number of different examples of creating a contract that looks like this:
XElement Query_Post(Stream postData);
And then within the implementation decoding the postData stream using the HttpUtility.ParseQueryString method.
Does anyone know of a more strongly typed way of supporting "application/x-www-form-urlencoded" in WCF?
I would like my operation contract to be:
XElement Query_Post(string query, string [] params);
The best way is to use Stream like Raw HTTP POST with WCF or what you are saying.
The reason is because WCF abstracts all the communication-level physical layout stuff out from the service code. Ideally, you would want to make a service that could turn into SOAP or REST just by flipping the switch.
To support it natively, you probably have to extend WebHttpBinding or make your own binding and implement custom encoder. This is symmetric to the output like the linked post says. You have to twist its arms to get WCF to output non-XML/JSON stuff.
The WCF REST Contrib library enables this functionality:
https://github.com/mikeobrien/WcfRestContrib
It includes a POX formatter and form url encoded formatter and allows you to easily create your own. Formatters are mapped to mime types and automatically selected to serialize/deserialize the entity body based on the content type and accept headers.

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

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.