Re: Passing Inherited types to a web service method

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jeffrey Hasan (jeff_at_noreply.com)
Date: 08/12/04


Date: Thu, 12 Aug 2004 16:32:20 -0700

This is a very interesting issue! There are no restrictions on passing
custom types to Web services, by VALUE, including inherited types, such as
your B : A. I wrote some basic test code to verify this. In order for this
to work, the Web service and the client must have a common understanding of
the data types. You need to make sure that the type definitions are broken
out into a separate referanceable file, or type definition assembly, with a
dedicated namespace, so that you can drop it into whatever projects need it.
In your case, the Web service and the client. Then, when you set the Web
service calls, you can do so using these qualified types. (And of course, in
Web services terms what you are doing here is referencing the same
XSD-qualified type). For example:

namespace WSTypes
{
 public class A {}
 public class B : A {}
}

I think you already know this, and have something like this implemented. Now
regarding your use of "ref". This is the cause of your issue. If you define
a Web method that accepts A by VALUE, then you can pass in either A or B.
However, if your Web method accepts A by REF then you can only pass in A.
The reason appears to have to do with the way data types are deserialized on
the receiving end. Call it a bug or a known issue, but it is clearly
something that does not work as it should.

So what I would do is to write your Web methods to accept A and then return
B, as in:

[WebMethod]
public WSTypes.B doSomething(ref WSTypes.A objA)
{

}

This should be an acceptable workaround. Hope this helps -

Jeffrey Hasan, MCSD
President, Bluestone Partners, Inc.
-----------------------------------------------
Author of: Expert SOA in C# Using WSE 2.0 (APress, 2004)
http://www.bluestonepartners.com/soa.aspx

"Paul Michaud" <NO_SPAM_pmichaud@NO_SPAM_earthlink.net> wrote in message
news:efWe8L1fEHA.1656@TK2MSFTNGP10.phx.gbl...
> To elaborate I also tried the following:
>
> [XmlInclude(typeof(B))]
> [XmlInclude(typeof(C))]
> Class A
> {
> ...
> }
>
> and
>
> [XmlInclude(typeof(B))]
> [XmlInclude(typeof(C))]
> [WebMethod]
> public int doSomething(ref A theAInstance)
> {
> ...
> }
>
> Paul
>
> "Paul Michaud" <pmichaud@earthlink.net> wrote in message
> news:5c641712.0408101839.414a33f0@posting.google.com...
> > Consider the following:
> >
> > Class A
> > {
> > ...
> > }
> >
> > Class B:A
> > {
> > ...
> > }
> >
> > Class C:A
> > {
> > ...
> > }
> >
> >
> >
> > Now consider a webservice with the following method
> >
> > [WebMethod]
> > public int doSomething(ref A theAInstance)
> > {
> > ...
> > }
> >
> > When I create an application which tries to call the doSomething
> > method as follows:
> >
> > status=doSomething(ref instanceOfB);
> >
> > I get the following error:
> >
> > cannot convert from 'ref B' to 'ref A'
> >
> > According to all the docs I have on .Net and webservices, this should
> > work. I have tried to use xmlinclude to declare the inherited types
> > in both the definition of A and at the method level (I have seen
> > people suggest both) and still no go. Are we saying that it is
> > impossible to pass an inherited type by ref in a web service. If not
> > any guidance or sample code that would work is greatly appreciated.
> >
> > Paul
>
>



Relevant Pages

  • Re: Preserve whitespace
    ... > I am consuming a web service in my asp.net application. ... > web references in Visual Studio by pointing to a wsdl file. ... > System.AsyncCallback callback, object asyncState) { ... > public class mthAddINProviderResponseOutVar_0MthAddINProvider_outputStatus ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: Please Help me- Creating Custom tokens-PLEASE HELP ME!!!!!!!!!!!!
    ... So before he his given access to the Web Method, ... > WSE on the Web Service first authenticates him against Active ... > Directory(Using UsernameTokenManagers- Authenticate Method). ... > is authenticated in the Web method before the response is sent I will ...
    (microsoft.public.dotnet.framework.webservices.enhancements)
  • Re: Big-Picture Question (Web Services, RegNow)
    ... Unfortunately, the VS Web service test mechanism did not support this, which makes me think others might not support it either, so I'll avoid that. ... you can choose to initialize the variables using the constructor. ... public class Test1 ... public int Attrib2 ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Data table parameter in web services.
    ... So the webservice call is not hard-coded but for example ... which web service to use on a set of data. ... to execute and this string as a generic procdure that will execute the ... one of the web method, it does list it in IE, when i browse the asmx file, ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • RE: Web Service Identity
    ... IIS for webservice A so as to get the clientside user's windows crediential ... | I have a web service that runs under its own IIS Application Pool ... | Within my web method I require the caller to be a member of a specific ...
    (microsoft.public.dotnet.framework.aspnet.webservices)