Re: passing structs (setializable) object in web service

From: Dino Chiesa [Microsoft] (dinoch_at_online.microsoft.com)
Date: 01/03/05


Date: Mon, 3 Jan 2005 13:57:29 -0500

The philosophy of webservices is different than that of remoting .

Webservices is a message-passing model, where a sender sends a message to a
receiver. In the common case, the sender is a client, the receiver a
service, and the messages are passed in request+reply pairs. In your case
the wsPerson is an instance of a Data Transfer Object, which is a common
pattern in these sorts of apps. The DTO is documented in Martin Fowler's
PEAA, #401.
Remoting is a different model. The metaphor is invocation of methods on
remote objects, along with the ability to transport objects (including data
and behavior) between apps. Of course these differences can be either
hidden or encapsulated, so that you can treat both as simple communications
mechanisms. But in my view, they lend themselves to different solutions.

Just with the info you gave in your posts, If I were designing your app, I
would use shared classes to implement the behavior that is shared in both
clients and servers. And I would use a DTO for transmission of state
between clients and servers (in either direction). In .NET these two
things can be the same - in other words a shared domain class (something
that exposes data and methods) can also act implicitly as a DTO (which
exposes data only), via XML Serialization and Webservices. If you want to
be explicit about it, you could define a simple subclass, eg public class
PersonDTO: Person {}. This would be more of a documentation aid than
anything else, because the PersonDTO would still expose all the public
methods from Person. If you want to be more explicit , you could define a
completely separate DTO type with support in Person that would translate
between the two types. Something like what you have done in your example.

Also, for today, to share classes across sender and receiver, you have to
follow Seely's advice as Christoph pointed out earlier - you have to modify
the code generated by wsdl.exe.

By the way #1. I don't agree that modifying generated code is taboo. Any
tool that generates code, including wsdl.exe, is just a tool. If you like
what it generates, use it as is. If you don't like it, you can hand-author
your own code, or you can start with the generated code and customize it.
There's nothing wrong with this. Automatically modifying the generated code
is also ok in my book - using perl or sed or awk or xslt or whatever.

By the way #2: there is a related pattern in Fowler's PEAA, #388, Remote
Facades.

Finally, Web services and Xml Serialization is not affected by the
[Serializable] attribute.

-D

"Stacy Meir" <StacyMeir@discussions.microsoft.com> wrote in message
news:B85BEC15-79AC-40EC-B518-55C5828FA642@microsoft.com...
> christoph and erymuzuan,
>
> thanks you for replying so quickly.
>
> i did try erymuzuan's approach before but was reluctant for the same
> reasons
> christoph pointed out. during development process i do change my server
> frequently and very likely to forget to make manual edit.
>
> christoph, thank you for pointing me in the right direction. to complecate
> things further, i do like erymuzuan's point about .net remoting and that
> having total control over both client and server types. i am not an expert
> in
> web services yet (may be some day) and would like to know if serialized
> types
> exposed through web services can also export or map methods on the client
> side? in other words, is there a way to provide methods available to the
> client? i think it will be clear in the example below what i am asking.
>
> on the server:
>
> [Serializable]
> public class Person
> {
> public string Name;
> public DateTime DOB;
> public TimeSpan Age { get { DateTime.Now-DOS; } }
> }
>
> on client when web referenced:
>
> [Serializable]
> public class Person
> {
> public string Name;
> public DateTime DOB;
> }
>
> now i have lost all of my support methods that really make this object
> useful. so this is what i am doing:
>
> [Serializable]
> public class wsPerson // transport "flying" object
> {
> public string Name;
> public DateTime Age;
> }
>
> is used in server and web referenced in client (which looks/feels the
> same... no methods). now i have common.dll that implements Person uses
> wsPerson:
>
> public class Person
> {
> public Person(wsPerson netflyer) { ... copy values ... }
> public static explicit operator wsPerson(Person local) { ... copy
> attrs .. }
>
> public TimeSpan Age { get { DateTime.Now - Age; } } // me likey
> }
>
> i am using transport object to create my "useful" object on the client as
> well as server. i am doing this for 50 such objects. is there a better
> way?
> my guess that there should be. thanks again.
>
> regards
>
>
>
> "Christoph Schittko [MVP]" wrote:
>
>> Scott's article does describe editing the code generated from the web
>> reference, but generally I strongly discourage people from editing
>> auto-generated code.
>>
>> The use of remoting is strongly discouraged [0] at this point with
>> respect to future interoperability issues and a programming model that
>> tempts developers to develop strongly coupled systems based on the
>> distributed object metaphor. Personally, I like the .NET Remoting
>> framework, and I am somewhat disappointed to see such a fine framework
>> go away, but knowing the issues ahead I can no longer recommend building
>> new distributed systems on .NET Remoting. At this point .NET Remoting
>> should strictly be used for intra-process/cross app domain scenarios.
>>
>> HTH,
>> Christoph Schittko
>> MVP XML
>> http://weblogs.asp.net/cschittko
>>
>> [0] http://weblogs.asp.net/cschittko/archive/2004/05/27/143388.aspx
>>
>>
>> > -----Original Message-----
>> > From: erymuzuan [mailto:erymuzuan@yahoo.com]
>> > Posted At: Sunday, January 02, 2005 7:04 PM
>> > Posted To: microsoft.public.dotnet.framework.aspnet.webservices
>> > Conversation: passing structs (setializable) object in web service
>> > Subject: Re: passing structs (setializable) object in web service
>> >
>> > It's true, but you can always manually edit your webreference file,
>> and
>> > replace the object from the WSDL.exe generated with one from your
>> common
>> > project, just make sure you have the XMLNamespace is correct. BTW i'll
>> > always suggest that you use remoting instead. as you have the control
>> of
>> > both the client and the server.
>> >
>> >
>> > Regards
>> > Erymuzuan Mustapa
>> > Christoph Schittko [MVP] wrote:
>> > > Stacy,
>> > >
>> > > The behavior you're describing is a known "weakness" in v1.x of
>> ..NET.
>> > > Add Web Reference has no way to recognize types that are in the
>> project
>> > > where you are adding the reference to. Furthermore, it doesn't even
>> > > re-use proxy classes for XML Schema types referenced from multiple
>> web
>> > > services.
>> > >
>> > > In .NET 2.0 you will be able to customize the generation of proxy
>> > > classes much better. For now you have to follow the approach
>> outlined
>> > > Scott Sealy's article [0].
>> > >
>> > > HTH,
>> > > Christoph Schittko
>> > > MVP XML
>> > > http://weblogs.asp.net/cschittko
>> > >
>> > > [0]
>> > >
>> http://msdn.microsoft.com/library/en-us/dnservice/html/service07162002.a
>> > > sp?frame=true
>> > >
>> > >
>> > >>-----Original Message-----
>> > >>From: Stacy Meir [mailto:Stacy Meir@discussions.microsoft.com]
>> > >>Posted At: Saturday, January 01, 2005 2:41 AM
>> > >>Posted To: microsoft.public.dotnet.framework.aspnet.webservices
>> > >>Conversation: passing structs (setializable) object in web service
>> > >>Subject: passing structs (setializable) object in web service
>> > >>
>> > >>i have tried all possible ways and have failed to pass a
>> serializable
>> > >>struct.
>> > >>i have three projects: server, client and common. common project
>> > >
>> > > contains
>> > >
>> > >>struct mydatapacket marked serializable. i use common project
>> > >
>> > > reference in
>> > >
>> > >>other two projects (client and server).
>> > >>
>> > >>in client project when i create web reference to server project it
>> > >
>> > > imports
>> > >
>> > >>a
>> > >>new type from server project something like
>> > >>server.mywebreference.mydatapacket, which is different from
>> > >>common.mydatapacket. how do stop this from happening. or, am i
>> > >
>> > > completly
>> > >
>> > >>off
>> > >>track.
>> > >>
>> > >>all examples i see pass basic data types and most "complex" pass
>> data
>> > >>sets.
>> > >>can someone explain or point me to a resource that discusses in
>> detail
>> > >>passing of compound data structures in web services (hopefully
>> without
>> > >>much
>> > >>soaping and more c# attributing)
>> > >>
>> > >>thanks
>> > >
>> > >
>>
>>



Relevant Pages

  • Re: Events in .Net Remoting
    ... I am writing with respect to the Events in .Net Remoting that I had ... another channel for the callbacks..both on client side. ... Then I force a method on the server end (through a GUI control on the ... >> the regular client side requests still work fine. ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Delegate Failure after Migration to .NET 2.0 - Vista
    ... remoting to work in .NET 2.0 for you? ... ActivatedClientTypeEntry entry = new ... I make the single below call in order to register my server type. ... the client. ...
    (microsoft.public.dotnet.languages.csharp)
  • Reuse of Remoting Channels...
    ... makes it possible for the server to know the identity of the caller. ... If my client is on the other side of a Windows 'realm' (as in the ... RemotingConfiguration options) to reject any clients whose credentials ... "Remoting server cannot be reached. ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: soapsuds
    ... pre-instantiated exe running in a singleton mode, for remoting, even if you ... Revised framework implementations will probably not break your application ... We can then compile the client using this code which allows us ... Both client and server are ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: passing structs (setializable) object in web service
    ... christoph pointed out. ... having total control over both client and server types. ... >> Erymuzuan Mustapa ...
    (microsoft.public.dotnet.framework.aspnet.webservices)