views:

438

answers:

3

I currently have a WCF service to consume a remote REST service with the following:

[ServiceContract]
[XmlSerializerFormat]
public interface IMyApi
{
 [OperationContract]
 [WebGet(
  ResponseFormat = WebMessage.Xml,
  UriTemplate = "RemoteServicePage.jsp")]
 MyNewClass Send();
}

The nice part about this is the XmlSerializerFormat attribute. Automatically deserializes a response into the return type of the method(ie, POX response => MyNewClass).

I've been unsuccessful, however, in finding any samples of the reverse. I'd like to post a POX request to a given service. I'm curious if there's a similar way to pass an object to a WCF service which in turn makes the post request to the target.

Any thoughts are greatly appreciated.

UPDATE

Just a clarification of the question:

Is it possible to post an object to a web service via WCF(which handles the serialization)?

UPDATE

I believe Steve touched on what I believe is the right direction below with using the WebInvoke method and attribute RequestFormat to achieve what I'm looking for. I guess I want to point out I'm not hosting a web service that allows for posting, but rather trying to post to an external web service(ie, a remote *.jsp) using WCF.

WCF allows for easy consumption and access of external web services and this is something I'm familiar with. I've never attempted to post a stream or object to an external source however(posting via UriTemplate is straightforward).

+1  A: 

I don't think you understand the meaning of XmlSerializerFormat. It means that the XML Serializer should be used instead of the Data Contract Serializer. Both will serialize to XML.

John Saunders
A: 

Use the RequestFormat attribute. For JSON, this would be

[WebInvoke(Method = "POST",
                 BodyStyle = WebMessageBodyStyle.WrappedRequest,
                 ResponseFormat = WebMessageFormat.Json,
                 RequestFormat = WebMessageFormat.Json)]

Good luck

Steve
+1  A: 

I don't understand. Why not just use HttpWebRequest? One of the benefits of doing REST over HTTP is that you get to use standard HTTP libraries. Doing a POST with HttpWebRequest is relatively trivial. Why do you need WCF? If you want to serialize an object into your POST body, then you can do that with either the DataContractSerializer or with the XmlSerializer.

If you really don't like that option, then look at the new Microsoft.Http.HttpClient class that is in the WCF Rest Starter Kit Preview 2. It is a very nice client library and despite its name and packaging, it does not even have a dependency on WCF!

This answer is based on your second update. I only say this because your initial statement of "I currently have a WCF service to consume a remote REST service" makes no sense. You cannot declaratively define a remote interface that you want to consume. The term WebInvoke is not what you are thinking, it is simply a catch all attribute for handing non GET requests. WebGet and WebInvoke both handing incoming requests.

If you want to consume a remote "REST" interface then you need to use HttpWebRequest or the new HttpClient class.

Darrel Miller
Hey Darrell, I appreciate the response here. Mostly for slapping me in the face for over complicating something ;) We do currently use WCF to make a GET request for a third party service, which handles serialization for us nicely and are looking to post something to the same service. I realize I don't have to use WCF to accomplish this, but since the dependency was already there, I wasn't sure if this was possible.
Alexis Abril
Glad that you appreciate that it was only a friendly slap. :)
Darrel Miller