Keep Your Identity

Use RPC-style messaging when passing reference types to Web methods ... or suffer the consequences.

Hot Tip

LANGUAGES: C#

TECHNOLOGIES: Web Services | RPC Messaging

 

Keep Your Identity

Use RPC-style messaging when passing reference types to Web methods ... or suffer the consequences.

 

By Jeff Prosise

 

Many developers don't realize that ASP.NET Web services support two distinctly different messaging styles: document messaging and RPC messaging. Document-style messaging is the default, despite the fact that it's not intended to support request/response message patterns specifically as is RPC-style messaging. Nevertheless, document-style messaging works well for the vast majority of Web methods.

One drawback to document-style messaging is,   however, that it is powerless to preserve the identities of objects passed as method parameters. Consider this Web service:

 

<%@ WebService Language="C#" Class="IdentiService" %>

 

using System;

using System.Web.Services;

 

[WebService]

public class IdentiService

{

     [WebMethod]

    public bool IsDuplicate (ref Widget w1, ref Widget w2)

    {

        return (w1 == w2);

    }

}

 

public class Widget

{

    public string Description;

}

 

This Web service's one and only Web method, IsDuplicate, compares the two input parameters; it returns true if they represent the same Widget object, false if they do not. At least that's how it's intended to work. In reality, IsDuplicate always returns false because document-style messaging doesn't preserve the identity of the input parameters, even if the parameters refer to the same Widget object. In other words, this call to IsDuplicate returns false even though it should return true:

 

Widget w = new Widget ();

w.Description = "This is a widget";

IdentiService ident = new IdentiService ();

bool IsDuplicate = ident.IsDuplicate (ref w, ref w); // Returns false (!)

 

The solution to this problem is RPC-style messaging, which is enacted by attributing a Web service with the [SoapRpcService] attribute or individual Web methods with [SoapRpcMethod]. Here's a modified version of the Web service whose IsDuplicate method detects duplicate input parameters accurately (changes are highlighted in bold):

 

<%@ WebService Language="C#" Class="IdentiService" %>

 

using System;

using System.Web.Services;

using System.Web.Services.Protocols;

 

[WebService]

public class IdentiService

{

     [WebMethod]

     [SoapRpcMethod]

    public bool IsDuplicate (ref Widget w1, ref Widget w2)

    {

        return (w1 == w2);

    }

}

 

public class Widget

{

    public string Description;

}

 

When you pass reference types by reference to Web methods and wish to preserve the parameters' identities, be sure to use RPC-style messaging. Otherwise, the results you get probably won't be the results you expect.

 

Jeff Prosise is author of several books, including Programming Microsoft .NET (Microsoft Press). He also is a co-founder of Wintellect (http://www.wintellect.com), a software consulting and education firm that specializes in .NET. Got a question for this column? Submit queries to [email protected].

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish