0

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/
4

1 回答 1

1

过去我会从 asmx.xml 中提取 WSDL。然后我将使用参数针对svcutil/mc运行它,它会生成服务和消息合同。然后,我将重构代码以使其行为相同。在大多数情况下它有效,但有一些陷阱。

从您发布的代码中,您希望将“myObj”封装为请求和响应消息中的标头,类似于以下内容:

[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
}

请参阅使用消息协定

于 2013-10-07T07:03:55.437 回答