Re: How to return a user-defined data type object from a webservice?
- From: Bradley Plett <plettb@xxxxxxxxxxxxxxxx>
- Date: Wed, 05 Mar 2008 15:46:05 -0700
What John is saying is all true, but it's a bit of an ivory tower
approach. Sometimes we developers have to bend the rules because it's
the easiest way to get the job done!
Anyway, I'm guessing that you have shared code between your server and
your client (probably using the same class code, shared in VSS). If
that's the case, what you're up against is simply the fact that the
object coming from the server via the web service is in a different
namespace from the object you want to use in your client. There are a
variety of ways of getting around this problem. For example, you can
use reflection and generics to change the namespace of the object in
question. Here's a code sample that will simply copy everything from
one object to another, assuming all their public properties are
identical:
// the idea for this came from
http://www.eggheadcafe.com/tutorials/aspnet/a4264125-fcb0-4757-9d78-ff541dfbcb56/net-reflection--copy-cl.aspx
private static T SetProperties<T, U>(U fromRecord, T toRecord)
{
foreach (PropertyInfo fromField in
fromRecord.GetType().GetProperties())
{
if (fromField.Name != "Id")
{
foreach (PropertyInfo toField in
toRecord.GetType().GetProperties())
{
if (fromField.Name == toField.Name)
{
toField.SetValue(toRecord, fromField.GetValue(fromRecord,
null), null);
break;
}
}
}
}
return toRecord;
}
Hope that helps!
Brad.
On Wed, 5 Mar 2008 07:19:31 -0800 (PST), nano2k
<adrian.rotaru@xxxxxxxxxxx> wrote:
On 5 Mar, 00:46, "John Saunders [MVP]" <john.saunders at trizetto.com>.
wrote:
"nano2k" <adrian.rot...@xxxxxxxxxxx> wrote in message
news:35fd1af1-4792-47a3-a0af-a03f77292227@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On 3 Mar, 14:34, "John Saunders [MVP]" <john.saunders at trizetto.com>
wrote:
"nano2k" <adrian.rot...@xxxxxxxxxxx> wrote in message
news:e6603399-aff7-4e0e-b6aa-d66db798e4b6@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Anyway,
Passing DataSet or XmlNode objects works by itself.
How can I enable my Person class to act like the above ones?
=========
You don't want to. Really.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer
Sorry, but why?
Just for the record.
=
I pretty much told you why before. It's just not how Web Services works.
The paradigm of Web Services is all about interoperability between client
and server by using industry-standard protocols (HTTP) and data formats
(XML). You are asking how you can use the above to pass data specific to a
particular platform (Microsoft .NET). There is no provision for that in the
Web Services standards. If you read those standards, you'll find mention of
the use of standards (XML Schema) to describe the structure of the data to
be exchanged. Nowhere in any of those standards is there an ability for you
to specify a particular data type from a particular platform.
This is in contrast to .NET Remoting, which is platform-specific, and which
actually transmits the full typename and assembly name of the types of the
data being transferred. This permits the client to receive instances of the
precise type sent by the server. There is no provision for anything like
this in Web Services.
Now, I'm only just recently getting up to speed on WCF, but it looks like
when you create a Service Reference (the WCF equivalent of a Web Reference),
you have the ability to tell WCF to try to reuse datatypes if the types are
declared in particular assemblies. This may give you what you're looking
for, as long as you are in a controlled environment where you can be certain
that the type that WCF will choose for you is compatible with the type being
sent by the server. An example of such an environment is when the server and
all of its clients are in the same .NET solution. Tihs may be acceptable for
"internal" web services, which are only ever meant to be used by well-known
clients. It cannot work in the general case.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer
Thanks John.
I understand and aggree entirely.
But my app is ment to work ONLY on .NET platforms. It is not a public
web site or smtg like that.
It's an "internal" application for a company that will use the app
inside its own VPN.
More than that, I'm being asked to secure the entire system so that
the webservice will only work with the designated client (also
developed by me).
So I find myself in this particular case, not in the general case. I
assure you I understand how interoperatibility works and its demands.
Anyway, your input was very good for me, but what do you say about my
particular situation? Creating standard XML schemas for each type I
want to "transport" would be a waiste of time, as I am 100% SURE my
webservice will only be accessible from a .NET client.
PS: I should have been more specific on this on my first post, my bad.
- Follow-Ups:
- Re: How to return a user-defined data type object from a webservice?
- From: John Saunders [MVP]
- Re: How to return a user-defined data type object from a webservice?
- References:
- Re: How to return a user-defined data type object from a webservice?
- From: John Saunders [MVP]
- Re: How to return a user-defined data type object from a webservice?
- From: nano2k
- Re: How to return a user-defined data type object from a webservice?
- From: nano2k
- Re: How to return a user-defined data type object from a webservice?
- From: John Saunders [MVP]
- Re: How to return a user-defined data type object from a webservice?
- From: nano2k
- Re: How to return a user-defined data type object from a webservice?
- From: John Saunders [MVP]
- Re: How to return a user-defined data type object from a webservice?
- From: nano2k
- Re: How to return a user-defined data type object from a webservice?
- Prev by Date: WSE Multi-Threading?
- Next by Date: Re: How to return a user-defined data type object from a webservice?
- Previous by thread: Re: How to return a user-defined data type object from a webservice?
- Next by thread: Re: How to return a user-defined data type object from a webservice?
- Index(es):
Relevant Pages
|