473,562 Members | 2,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing Inherited types to a web service method

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=doSometh ing(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
Nov 21 '05 #1
3 3147
To elaborate I also tried the following:

[XmlInclude(type of(B))]
[XmlInclude(type of(C))]
Class A
{
...
}

and

[XmlInclude(type of(B))]
[XmlInclude(type of(C))]
[WebMethod]
public int doSomething(ref A theAInstance)
{
...
}

Paul

"Paul Michaud" <pm******@earth link.net> wrote in message
news:5c******** *************** ***@posting.goo gle.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=doSometh ing(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

Nov 21 '05 #2
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_pmicha ud@NO_SPAM_eart hlink.net> wrote in message
news:ef******** ******@TK2MSFTN GP10.phx.gbl...
To elaborate I also tried the following:

[XmlInclude(type of(B))]
[XmlInclude(type of(C))]
Class A
{
...
}

and

[XmlInclude(type of(B))]
[XmlInclude(type of(C))]
[WebMethod]
public int doSomething(ref A theAInstance)
{
...
}

Paul

"Paul Michaud" <pm******@earth link.net> wrote in message
news:5c******** *************** ***@posting.goo gle.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=doSometh ing(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


Nov 21 '05 #3
Hi Jeffrey,
Thanks for the reply. The reason I don't want to pass the variable back
through the return type, is that I always use the return for error codes. I
think I have figured out the issue. Consider the following:

A,B, etc defined as below.

Now call
status=doSometh ing(ref instanceOfB);

This doesn't work because of the issue you mentioned below.

so create a new variable called tempA and point it at instanceOfB (or so we
might think)
A tempA=(A)instan ceOfB;

now call the function with tempA.

status=doSometh ing(ref instanceOfB);
Here is what I have found to have happened.
1) tempA comes back knowing its of type B (or at least when you do a watch
it has the additional attributes present in B and not in A in a substructure
, which by the way manifests differently on either side of the web service
call which is odd, but it does appear to have worked)
2) if I now cast tempA to type B and access the B specific properties it
works fine. All the data is there correctly.
3) Even though tempA was set to be equal to instanceOfB when the function
returns, tempA and instanceOfB are no longer the same insance, they are two
different instances after the de-serialization of tempA. Thus the original
variable instanceOfB is not filled unless you set
instanceOfB=tem pA after the call.

The real issue is that according to the web service spec, I should be able
to pass instanceOfB directly and it should work. The fact that I can't
seems to be specifically a .Net bug. The other possibility is that there
may be some settings to export the class hierarchy through the web service
definition so that .Net knows B is a type of A and thus doesn't throw the
compile error (I do have a using statement with an interface dll which
com=ntains the class hierarchy but that doesn't do it). But if there is
something like that I haven't found it yet. I have tried the settings I
mentioned before which I thought was supposed to work but no go.

If anyone knows how to accomplish this I would appreciate it. I have spoken
to a number of others about this and they have had the same problem and
haven't found a decent work around yet.
Paul Michaud
"Jeffrey Hasan" <je**@noreply.c om> wrote in message
news:OR******** ******@TK2MSFTN GP10.phx.gbl...
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_pmicha ud@NO_SPAM_eart hlink.net> wrote in message
news:ef******** ******@TK2MSFTN GP10.phx.gbl...
To elaborate I also tried the following:

[XmlInclude(type of(B))]
[XmlInclude(type of(C))]
Class A
{
...
}

and

[XmlInclude(type of(B))]
[XmlInclude(type of(C))]
[WebMethod]
public int doSomething(ref A theAInstance)
{
...
}

Paul

"Paul Michaud" <pm******@earth link.net> wrote in message
news:5c******** *************** ***@posting.goo gle.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=doSometh ing(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



Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
25555
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to compile. <WebMethod()> _ Public Function VerifySku(ByVal skus As XmlDataDocument) As DataSet Test program : Dim cartSet As DataSet cartSet =...
0
1099
by: brian.mills | last post by:
I have a web service in a technology called Jade. It is returning a StudentObject, which has an array of Application Objects on it. The Application objects never are actually Application Instances, they are always inherited types. eg. public class Student { public Application allApplications; } public class Application
12
5307
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded the custom employee class and have built it as a separate library (employee.dll). This employee.dll is being referenced by both the web service and...
6
5522
by: Kevin Yu | last post by:
hi all is there a way to pass an interface as input parameter for a web method? since interface is not serializable, does that mean it's impossible to do that? Kevin
0
1849
by: cnSoftware | last post by:
I come across this problem during my recent project and even now I am still be puzzled by it. So I paste the procedure here. I wish you can figure out where I am wrong or missing. The sample application is developed with VS 2005 Beta 2. 1. Create a base class named ¡°BaseEntity¡±
6
13815
by: nickname | last post by:
I want to pass some xml to a web service method, the xml will confirm to a defined schema. Lets say the schema is defined as: <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="books"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded"...
7
3292
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class...
3
7295
by: andreas.baus | last post by:
Hello. I'm trying to exchange data between a webservice based on Apache Axis and a client written in C#, and so far it has been working with few problems (none that I couldn't solve myself with a bit of web research). However, now I've tried passing complex data types between the two, and could not get that to work. What I did was to...
8
8890
by: =?Utf-8?B?UmF2aQ==?= | last post by:
Hi, I'm trying to pass values of different data-types to a web-service. I thought it would be easier to box these values and pass them as a System.object parameter, like public void webmethod1(object a) where a can be of type string, DateTime, float, etc..
0
7577
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7627
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7935
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6221
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5477
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3608
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1191
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
903
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.