473,762 Members | 8,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing complex type/class to WebService


Hi

How do I pass a complex type to a webservice??

What I have is a Class 'MyComplexClass ' which lives in it's own dll/namespace. I'd like to pass this class to my webmethod:

<WebMethod()_
Public Sub MyMethod(ByVal arg As MyComplexClass)

End Sub

When I add a webreference to my client project VS2005 creates a class similar to the 'MyComplexClass ' and put it in the proxy code.

This means that I have to give an instance of the 'MyComplexClass ' defined in the proxy, rather than an instance of the 'MyComplexClass ' defined by me.

How do I make it pass the correct class?? Do I have to make an SubClass of my proxy which converts between the two types??

TIA

Søren
Apr 25 '07 #1
7 10147
"Søren M. Olesen" <sm******@hotma il.comwrote in message news:Oz******** ******@TK2MSFTN GP04.phx.gbl...

Hi

How do I pass a complex type to a webservice??

What I have is a Class 'MyComplexClass ' which lives in it's own dll/namespace. I'd like to pass this class to my webmethod:

<WebMethod()_
Public Sub MyMethod(ByVal arg As MyComplexClass)

End Sub

When I add a webreference to my client project VS2005 creates a class similar to the 'MyComplexClass ' and put it in the proxy code.

This means that I have to give an instance of the 'MyComplexClass ' defined in the proxy, rather than an instance of the 'MyComplexClass ' defined by me.

How do I make it pass the correct class?? Do I have to make an SubClass of my proxy which converts between the two types??

TIA

Søren

Let me see if I understand you correctly. On your server, you have a class SomeNamespace.M yComplexClass. Your web service has a WebMethod which takes a parameter of that type.

In developing your client, you use Add Web Reference to add a reference to this web service. This produces a proxy class for the web service, and proxy classes for the types used as parameters or return values from the web service.

Am I correct so far?

If I understand you correctly, then you want the client to use the assembly containing the server-side class MyComplexType, and to pass that to the web service through the proxy? But since there is no relationship between the server-side class and the proxy class (which is why they call it a proxy class), the two classes are not the same thing and cannot be interchanged.

Web Services do not pass objects: they pass XML. A client-side object is serialized into XML before sending it to the server, which deserializes it _into an object of a different type_.

The two types have little in common. For instance, try creating a server-side type with non-default constructors, indexers, write-only properties and methods. Try using that in a WebMethod and take a look at the proxy class. It will not have the non-default constructors, indexers, write-only properties or methods. And it's not meant to have them.

Perhaps if you'll tell us what you need to accomplish, we'll be able to help you accomplish it.
--

John Saunders [MVP]
Apr 25 '07 #2
>Let me see if I understand you correctly. On your server, you have a class SomeNamespace.M yComplexClass. Your web service has a WebMethod which takes a parameter of that type.
>In developing your client, you use Add Web Reference to add a reference to this web service. This produces a proxy class for the web service, and proxy classes for the types used as >parameters or return values from the web service.
>Am I correct so far?
Yes, that's true.
>Perhaps if you'll tell us what you need to accomplish, we'll be able to help you accomplish it.
What I'd like is to have two different webservices MyWebService1.M yMethod and MyWebService2.M yMethod each taking a parameter of SomeNameSpace.M yComplexClass. The SomeNameSpace.M yComplexClass exists in it's own assembly, so it's very easy to link to it to both the servers and the clients.

What I'd like to do is to pass the SomeNameSpace.M yComplexClass between the two services without having to convert the class to and from the two proxyclasses for the two webservices.

Of course the SomeNameSpace.M yComplexClass has to be XML serializeable and everything, but when that's the case, there shouldn't be any problems passing it around.

Regards,

Søren
Apr 26 '07 #3
"Søren M. Olesen" <sm******@hotma il.comwrote in message news:uz******** *****@TK2MSFTNG P06.phx.gbl...
>Let me see if I understand you correctly. On your server, you have a class SomeNamespace.M yComplexClass. Your web service has a WebMethod which takes a parameter of that type.
>In developing your client, you use Add Web Reference to add a reference to this web service. This produces a proxy class for the web service, and proxy classes for the types used as >parameters or return values from the web service.
>Am I correct so far?
Yes, that's true.
>Perhaps if you'll tell us what you need to accomplish, we'll be able to help you accomplish it.
What I'd like is to have two different webservices MyWebService1.M yMethod and MyWebService2.M yMethod each taking a parameter of SomeNameSpace.M yComplexClass. The SomeNameSpace.M yComplexClass exists in it's own assembly, so it's very easy to link to it to both the servers and the clients.

What I'd like to do is to pass the SomeNameSpace.M yComplexClass between the two services without having to convert the class to and from the two proxyclasses for the two webservices.

Of course the SomeNameSpace.M yComplexClass has to be XML serializeable and everything, but when that's the case, there shouldn't be any problems passing it around.

Sorry, I guess I'm not being clear enough. You do not "pass objects" between the client and server using Web Services. The server takes an object and serializes it into some XML. The client deserializes it into a proxy object. The server object and proxy object are completely unrelated. Period.

The client should absolutely not have a reference to the assembly which the server uses to define the type that the server sends on the wire.

Any attempt to hack around this fundamental restriction will very likely lead to failure and frustration. If you really need the client and server objects to be "the same", then you need to use .NET Remoting or WCF.
--

John Saunders [MVP]

Apr 26 '07 #4
Søren,

You should modify the proxy class (either directly or via a partial class declaration) and add a public constructor to Proxy.MyComplex Class that takes an instance of SomeNamespace.M yComplexClass. In the constructor you can initialize the properties based on the values of SomeNamespace.M yComplexClass. This will deal with web service methods that use MyComplexClass as an input parameter. If MyComplexClass is returned from the web service then you should add a public method on Proxy.MyComplex Class that will return SomeNamespace.M yComplexClass.
"Søren M. Olesen" <sm******@hotma il.comwrote in message news:uz******** *****@TK2MSFTNG P06.phx.gbl...
>Let me see if I understand you correctly. On your server, you have a class SomeNamespace.M yComplexClass. Your web service has a WebMethod which takes a parameter of that type.
>In developing your client, you use Add Web Reference to add a reference to this web service. This produces a proxy class for the web service, and proxy classes for the types used as >parameters or return values from the web service.
>Am I correct so far?
Yes, that's true.
>Perhaps if you'll tell us what you need to accomplish, we'll be able to help you accomplish it.
What I'd like is to have two different webservices MyWebService1.M yMethod and MyWebService2.M yMethod each taking a parameter of SomeNameSpace.M yComplexClass. The SomeNameSpace.M yComplexClass exists in it's own assembly, so it's very easy to link to it to both the servers and the clients.

What I'd like to do is to pass the SomeNameSpace.M yComplexClass between the two services without having to convert the class to and from the two proxyclasses for the two webservices.

Of course the SomeNameSpace.M yComplexClass has to be XML serializeable and everything, but when that's the case, there shouldn't be any problems passing it around.

Regards,

Søren
Apr 26 '07 #5
"Scott Holman" <sh*****@micros .comwrote in message news:OZ******** ******@TK2MSFTN GP03.phx.gbl...
Søren,

You should modify the proxy class (either directly or via a partial class declaration) and add a public constructor to Proxy.MyComplex Class that takes an instance of SomeNamespace.M yComplexClass. In the constructor you can initialize the properties based on the values of SomeNamespace.M yComplexClass. This will deal with web service methods that use MyComplexClass as an input parameter. If MyComplexClass is returned from the web service then you should add a public method on Proxy.MyComplex Class that will return SomeNamespace.M yComplexClass.
Scott, have you tried this before? I would be concerned about maintainability in terms of keeping the client in sync with the server. By giving the client knowledge of the server (i.e., the client now knows the actual class the server is using, as opposed to knowing the WSDL and proxy class), the two are now tightly coupled.

Again, .NET Remoting and WCF both address this scenario. "Standard" Web Services do not.
--

John Saunders [MVP]

Apr 26 '07 #6
As I interpreted the OP questions, the OP has a client side implementation of MyComplexClass. This implementation may or maynot be the same implementation on the server-side...it doesn't really matter. The OP needs to marshall the data in SomeNameSpace.M yComplexClass to the proxy class so that the proxy can marshall to the server. The OP could
1 - write a helper class such as
class foo
{
public static Proxy.MyComplex Class Bar(MyNameSpace .MyComplexClass value)
{
//Code to Initialize and return Proxy.MyComplex Class
}
}

2 - or...assuming that the code generated for Proxy.MyComplex Class is declared as a partial class...the OP could extend the proxy class in another file and implement the logic in 1 above in a constructor and leave the generated proxy class code unmodified. (This would seem to be exactly the situation called for by a partial class).

And to answer your question, yes I have done this with proxies generated by svcutil. In fact, I have even replaced a specific class within the proxy with my own implementation.

In reading the orignial post, it is likely that the OP is using the same implementation of MyComplexClass on both the client and server. This however does not result in a tightly coupled client and server. The client application still has to marshal data from the Proxy.MyComplex Class to the SomeNameSpace.M yComplexClass. The only coupling that exists is the coupling to the data contract as represented by Proxy.MyComplex Class and this coupling exists in all clients that consume the web service. However, the client and server implementation do have a physical coupling due to the shared assembly. The OP should realize that bugs in SomeNameSpace.M yComplexClass will require that both client and server be rebuilt and redistributed. The OP can minimize this by refactoring the class and 1) separating common logic and 2) separating the data representation from the business logic (for example: MyComplexClassD ata and MyComplexClassC ommonLogic, MyComplexClassS erverLogic, MyComplexClassC lientLogic). Refactoring along these lines is more consistent with the Patterns and Practices guidance in the Web Service Software Factory.
"John Saunders [MVP]" <john.saunder s at trizetto.comwro te in message news:uk******** ******@TK2MSFTN GP03.phx.gbl...
"Scott Holman" <sh*****@micros .comwrote in message news:OZ******** ******@TK2MSFTN GP03.phx.gbl...
Søren,

You should modify the proxy class (either directly or via a partial class declaration) and add a public constructor to Proxy.MyComplex Class that takes an instance of SomeNamespace.M yComplexClass. In the constructor you can initialize the properties based on the values of SomeNamespace.M yComplexClass. This will deal with web service methods that use MyComplexClass as an input parameter. If MyComplexClass is returned from the web service then you should add a public method on Proxy.MyComplex Class that will return SomeNamespace.M yComplexClass.
Scott, have you tried this before? I would be concerned about maintainability in terms of keeping the client in sync with the server. By giving the client knowledge of the server (i.e., the client now knows the actual class the server is using, as opposed to knowing the WSDL and proxy class), the two are now tightly coupled.

Again, .NET Remoting and WCF both address this scenario. "Standard" Web Services do not.
--

John Saunders [MVP]

Apr 27 '07 #7
"Scott Holman" <sh*****@micros .comwrote in message news:ud******** ******@TK2MSFTN GP06.phx.gbl...
As I interpreted the OP questions, the OP has a client side implementation of MyComplexClass. This implementation may or maynot be the same implementation on the server-side...it doesn't really matter. The OP needs to marshall the data in SomeNameSpace.M yComplexClass to the proxy class so that the proxy can marshall to the server. The OP could
1 - write a helper class such as
class foo
{
public static Proxy.MyComplex Class Bar(MyNameSpace .MyComplexClass value)
{
//Code to Initialize and return Proxy.MyComplex Class
}
}

2 - or...assuming that the code generated for Proxy.MyComplex Class is declared as a partial class...the OP could extend the proxy class in another file and implement the logic in 1 above in a constructor and leave the generated proxy class code unmodified. (This would seem to be exactly the situation called for by a partial class).

And to answer your question, yes I have done this with proxies generated by svcutil. In fact, I have even replaced a specific class within the proxy with my own implementation.

In reading the orignial post, it is likely that the OP is using the same implementation of MyComplexClass on both the client and server. This however does not result in a tightly coupled client and server. The client application still has to marshal data from the Proxy.MyComplex Class to the SomeNameSpace.M yComplexClass. The only coupling that exists is the coupling to the data contract as represented by Proxy.MyComplex Class and this coupling exists in all clients that consume the web service. However, the client and server implementation do have a physical coupling due to the shared assembly. The OP should realize that bugs in SomeNameSpace.M yComplexClass will require that both client and server be rebuilt and redistributed. The OP can minimize this by refactoring the class and 1) separating common logic and 2) separating the data representation from the business logic (for example: MyComplexClassD ata and MyComplexClassC ommonLogic, MyComplexClassS erverLogic, MyComplexClassC lientLogic). Refactoring along these lines is more consistent with the Patterns and Practices guidance in the Web Service Software Factory.

Thanks, Scott, this puts your suggestion into perspective.

My concern was that the OP, like many, wanted the client and server to use the same exact class. You have correctly stated the difficulties this presents.

In addition, there will be a temptation to use other features of this class, so that the clients will come not only to depend on the data representation of the class, but on the methods etc. This is the coupling I warned about.
--
John Saunders [MVP]

Apr 27 '07 #8

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

Similar topics

7
11929
by: BS | last post by:
Hello everybody I'm calling a webservice that returns complex data. The goal is to populate a datagrid with it. Using a loop for each record found ( such as For i = 0 To oResponse.historicalData.Length - 1 ) no problem to load a datagrid. However, when I try to bind directly the datasource to the web service
0
2122
by: saish | last post by:
Hello I am new to vb.net webservice. My requirement is to design a VB.net web service which will send and receive xml documents. The xml document send and received would be a very complex .xsd file which contains multiple formats of send and receive mesages. On reading books on webservice it was says to design an interface (ie design the xsd , wsdl, then run wsdl.exe to generate interface.vb class) Since my xsd is very complex if I have...
12
5342
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 the windows application. I face the following problem when I send this class to the webservice.
7
2928
by: Nalaka | last post by:
Hi, I created a sinple web service that returns a dataSet. Then I created a client program that uses this web service (that returns the Dataset). My question is, how did the client figure out to create a "DataSet" as the return type from the webservice?
1
2862
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive types and of more elaborate classes implementing IXmlSerializable. The resulting WSDL file for the webservice has two separate schemas in its <types> sections, and the client proxy (generated with wsdl.exe) is missing the definitions of the...
9
3800
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting techniques are not feasible) The question is; Given that I have a Person object with a private set for id. What is the recommended approac in passing that object to the web service
6
13824
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" name="book"> <xs:complexType>
0
3578
by: shaily | last post by:
hi I have a java web service called "Registration service" and C# client which is a consumer of that service java web service running under Tomcat has following interface API exposed public RegistrationResponse registerDevice(RegistrationRequest regReq) throws RemoteException
8
8903
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
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9378
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10137
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9812
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3914
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.