Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / June 2005

Tip: Looking for answers? Try searching our database.

Exposing a private instance field in SOAP

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave A - 29 May 2005 01:10 GMT
I have a class called 'PrimaryKey' that represents the primary key of a
table.

PrimaryKeys can only be created and the class only implements .ToString().
The PrimaryKey class internally stores the data as either a int or a Guid
depending on the structure of the database.

If we simply used int to represent a primary key, rather than this
PrimaryKey class then programmers could do things like performing
mathematically operations on the primary key which should be best avoided
since this is not the nature of primary keys.

The PrimaryKey class works really well except when it comes to exposing it
through SOAP.  Because the class contains no public fields and no public
properties then SOAP/WSDL does not expose any insights into the class.

When an instance of the PrimaryKey class flows through SOAP and turns up at
the client I can't actually do anything with it since I can't get any data
out of it and cast it into the client's PrimaryKey class.

Ideally I would to put an attribute around the 'private int primaryKey'
field of the PrimaryKey class (that internally records the value of the
primary key) so that it can be exposed to SOAP and SOAP alone.

Is this possible?  I have studied the SOAP attributes and nothing seems
available.  Are there any work arounds?

One idea I had was to drop the .ToString() method and just have a .Value
property.  I would only implement the Get and not the Set but evidently
SOAP/WSDL needs both a Get and a Set before it is exposed to SOAP.

Thanks in advance
Dave A
Sami Vaaraniemi - 31 May 2005 08:46 GMT
I'm afraid there's no simple and easy answer to this issue. As you've
noticed, the XML serializer serializes only public fields and read-write
properties. The data format is customizable to some extent, but all private
state and behavior associated with your classes is lost.

That said, it is possible to hack around this issue. You can edit the
consumer-side proxy, remove the data classes generated along with the proxy
and have the proxy refer to the original class(es). This does allow you to
share the same type between the web service and the consumer and thus share
behavior, but private state is still lost.

The bottom line is that if you really want to share objects between a
service and its consumers, then you might want to consider using a
distributed object technology such as .NET Remoting. Web Services merely
pass XML documents back and forth, and trying to share types or behavior
will run into issues.

Regards,
Sami

>I have a class called 'PrimaryKey' that represents the primary key of a
>table.
[quoted text clipped - 29 lines]
> Thanks in advance
> Dave A
Dave A - 31 May 2005 10:23 GMT
Thanks.  I appreciate the effort that you put into that great answer.

There has to be a solution though. I am going to keep thinking about it and
if I come up with something then I will post back what I find.

> I'm afraid there's no simple and easy answer to this issue. As you've
> noticed, the XML serializer serializes only public fields and read-write
[quoted text clipped - 51 lines]
>> Thanks in advance
>> Dave A
Dave A - 31 May 2005 14:49 GMT
I knew there had to be an answer and there is!

It just barely works with .NET 1.x -
http://dotnetified.com/PermaLink.aspx?guid=E86B447E-AA95-49B6-909F-CDA36ACF481F

And it fully works with .NET
2.0 -http://weblogs.asp.net/cweyer/archive/2004/08/02/205798.aspx

Dave A

('Exposing private members using SOAP and XML Serialization' - that is there
for google)

> Thanks.  I appreciate the effort that you put into that great answer.
>
[quoted text clipped - 58 lines]
>>> Thanks in advance
>>> Dave A
Sami Vaaraniemi - 01 Jun 2005 07:53 GMT
Indeed, you can serialize a private field as shown in the article by
overriding the serialization for the object. You could also do this with a
wrapper class and a bit of reflection.

However, (I think) in the consumer the field now becomes public in the
generated class so the behavior (encapsulation) is lost. The consumer will
be able to access the PrimaryKey value directly which is what I believe you
wanted to avoid.

Regards,
Sami

>I knew there had to be an answer and there is!
>
[quoted text clipped - 75 lines]
>>>> Thanks in advance
>>>> Dave A

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.