Re: Passing Inherited types to a web service method
From: Jeffrey Hasan (jeff_at_noreply.com)
Date: 08/12/04
- Next message: Jeffrey Hasan: "Re: web service architecture question"
- Previous message: Jeffrey Hasan: "Re: Web Service Design"
- In reply to: Paul Michaud: "Re: Passing Inherited types to a web service method"
- Next in thread: Paul Michaud: "Re: Passing Inherited types to a web service method"
- Reply: Paul Michaud: "Re: Passing Inherited types to a web service method"
- Messages sorted by: [ date ] [ thread ]
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
>
>
- Next message: Jeffrey Hasan: "Re: web service architecture question"
- Previous message: Jeffrey Hasan: "Re: Web Service Design"
- In reply to: Paul Michaud: "Re: Passing Inherited types to a web service method"
- Next in thread: Paul Michaud: "Re: Passing Inherited types to a web service method"
- Reply: Paul Michaud: "Re: Passing Inherited types to a web service method"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|