开发者

A simple WCF Service (POX) without complex serialization

开发者 https://www.devze.com 2022-12-29 07:41 出处:网络
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-

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?


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);


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.


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


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!


There is an example in NET 4.0 here

0

精彩评论

暂无评论...
验证码 换一张
取 消