Jeff Maier
Jeff Maier

Reputation: 141

A simple WCF Service (POX) without complex serialization

I'm a complete WCF novice. I'm trying to build a deploy a very, very simple IIS 7.0 hosted web service. For reasons outside of my control it must be WCF and not ASMX. It's a wrapper service for a pre-existing web application that simply does the following:

1) Receives a POST request with the request body XML-encapsulated form elements. Something like valuevalue. This is untyped XML and the XML is atomic (a form) and not a list of records/objects.

2) Add a couple of tags to the request XML and the invoke another HTTP-based service with a simple POST + bare XML -- this will actually be added by some internal SQL ops but that isn't the issue.

3) Receive the XML response from the 3rd party service and relay it as the response to the original calling client in Step 1.

The clients (step 1) will be some sort of web-based scripting but could be anything .aspx, python, php, etc.

I can't have SOAP and the usual WCF-based REST examples with their contracts and serialization have me confused. This seems like a very common and very simple problem conceptually. It would be easy to implement in code but IIS-hosted WCF is a requirement.

Any pointers?

Upvotes: 4

Views: 3116

Answers (5)

Torben Philippsen
Torben Philippsen

Reputation: 11

I think the video that you are referring to, is available from here

Upvotes: 1

SteveC
SteveC

Reputation: 16743

There is an example in NET 4.0 here

Upvotes: 0

Aaronaught
Aaronaught

Reputation: 122624

Building a POX service is usually as simple as adding a few attributes and changing a few settings in your web.config.

First, add [WebInvoke] to the POX methods of the service contract:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
    MyResult MyOperation(MyClass c);
}

Sometimes you also have to specify an XML namespace for your data contracts:

[DataContract(Namespace = "http://example.com/myclass")]
public class MyClass { ... }

Then set up your web.config to use webHttpBinding and the webHttp behaviour:

<system.serviceModel>
    <services>
        <service behaviorConfiguration="MyApp.MyBehavior" name="MyApp.MyService">
            <endpoint address="" binding="webHttpBinding"
                      contract="MyApp.IMyService" behaviorConfiguration="POX"
                      bindingNamespace="http://example.com/">
                <--! More endpoint configuration here -->
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding"
                      contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <!-- Behavior config here -->
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="POX">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

That's about it. It seems harder than it really is.

Note: If you actually want to receive untyped XML instead of a serialized class, then change your method to receive an XElement, as in:

[WebInvoke(...)]
MyResult MyOperation(XElement element);

Upvotes: 4

marc_s
marc_s

Reputation: 754458

Ben has the right video - unfortunately, that video is "For Subscribers Only" on the Pluralsight site by now :-( :-( :-(

Luckily, almost all those intro videos are also available from the MSDN WCF REST Developer Center - except the one Ben mentioned ....

But the WCF REST developer center should still have plenty of good intro material to get up to speed with WCF REST easily and quickly!

Upvotes: 0

Ben.Vineyard
Ben.Vineyard

Reputation: 1169

I would take a look at the WCF REST Starter Kit @ http://www.asp.net/downloads/starter-kits/wcf-rest/ and the PluralSite HowTo @ http://www.pluralsight-training.net/microsoft/olt/howtovideo.aspx?a=aaron-skonnard&n=http-plain-xml-services. Those should get you started and pointed in the right direction.

Upvotes: 1

Related Questions