user1233802
user1233802

Reputation: 573

Migration from ASP.NET web service with SOAP Header and Mtom, WSE-3 to WCF

I need to do migration from ASP.NET web service to WCF. Current code is like this:

Server Side:

    public class MyType
    {
    public int x;
    public string s;
    }

    public class MyService : WebService
    {
    public MyType myObj;

    [WebMethod]
    [SoapHeader("myObj", Direction=SoapHeaderDirection.InOut)]
    public string SomeMethod() // NO Parameter!!!
    {
            if (myObj.x > 5)
               return myObj.s;
            else
               return "Less than 5";    
        }
    }
</code>

Client Side:

    MyService service = new MyService();
    MyType m = new MyType();
    m.x = 10;
    m.s = "That's it!!!";
    service.myObj = m;
    string s = service.SomeMethod();

This works smoothly. Now I need to change this to WCF. Found many topics about how to sign soap header with WCF, but all are about how to use MessageContract as an attribute of a class which will be given to the operation as a parameter. Like this:

<code>
    [MessageContract]
    public class MyType
    {
       public int x;
       public string s;
    }

    [OperationContract]
    public string SomeMethod(MyType myType)
    {

    } </code>

But this is not what I need, my method must remain the same (with no parameter).

Few blogs talks about using WCFExtra lib or implement Client Dispatcher interface to add SOAP Header to every outgoing request. But again, I want migration of web service to WCF shouldn't affect my client, and they are not required to do any code changes, they should be able to consume the web service with .asmx extension. Also, the current web service uses MTOM and WSE-3. Is there any easy way.

Please help me with this.

I have already read following articles, but none of them talk about how to handle SOAP Header, WSE-3:

http://megakemp.com/2008/11/27/migrating-aspnet-web-services-to-wcf/
http://megakemp.com/2008/11/27/migrating-aspnet-web-services-to-wcf/
http://vikasbhardwaj15.blogspot.in/2012/05/convert-asmx-web-services-to-wcf.html
http://blogs.msdn.com/b/trobbins/archive/2006/12/02/integrating-wcf-with-your-existing-asmx-services.aspx
http://blogs.msdn.com/b/kaevans/archive/2006/10/05/phased-migration-from-asmx-to-wcf.aspx
http://www.manas.com.ar/waj/2007/05/31/asmx-to-wcf-migration/

Upvotes: 0

Views: 753

Answers (1)

bloudraak
bloudraak

Reputation: 6002

In the past I would extract the WSDL from the asmx. Then I'll run it against svcutil with argument /mc, which generates the service and message contracts. I'll then refactor the code to behave the same. For most part it works, but there are some gotchas.

From the code you posted, you want to encapsulate the "myObj" as a header in a request and response message, similar to the following:

[DataContract]
public class MyType
{
    [DataMember]
    public int x;

    [DataMember]
    public string s;
}

[MessageContract]
public class SomeMethodRequestMessage
{
    [MessageHeader] public MyType myObj;
}

[MessageContract]
public class SomeMethodResponseMessage
{
    [MessageHeader] public MyType myObj;
    [MessageBodyMember] public string result;
}

// Inside your service contract

[OperationContract]
public SomeMethodResponseMessage SomeMethod(SomeMethodRequestMessage message)
{
   // do stuff
}

See Using Message Contracts.

Upvotes: 1

Related Questions