.NET Forum / ASP.NET / Web Services / March 2007
returning strongly typed dataset from web service
|
|
Thread rating:  |
N. Shehzad - 02 Mar 2007 23:42 GMT All, I have a webservice webmethod which does returns a strongly typed dataset called MyStronglyTypedDataSet correctly.
On the client side, when I cast the dataset to my strongly typed dataset, it throws the following error "Cannot convert type ClientConsumer.localhost.MyStronglyTypedDataSet" to "MyNameSpace.WebServices.MyStronglyTypedDataSet'
If I cast it as a regular dataset on client side, it works fine, but that defeats the whole purpose of having strongly typed DS in first place.
I am using a web reference from the client application to the web service, and also reference to the StronglyTpedDataSet dll that I created using SDK Command Prompt
What am I doing wrong?
John Saunders - 03 Mar 2007 18:43 GMT > All, > I have a webservice webmethod which does returns a strongly typed dataset [quoted text clipped - 14 lines] > > What am I doing wrong? Why are you referencing the dll from the server? The client and server classes have no direct relation to each other.
What happens if you create a new project and use Add Web Reference to add a reference to your web service? Isn't that enough to get you your strongly-typed dataset on the client side?
John
P.S. I just tried it myself, and it seems to work. Just keep in mind that you've got a server-side DataSet and a client-side proxy DataSet, and that the two types are unrelated.
Scott M. - 04 Mar 2007 00:44 GMT Why are you trying to cast an object into the type that it already is? You don't need to cast at all here.
Your client code should simply be:
Dim receivedDataSet As MyStronglyTypedDataSet = webService.webMethodCall()
If your web method is returing a type of "MyStronglyTypedDataSet", all you need to do is set up a pointer (not a new instance) to that returned object. That pionter needs to be declared as being the same type (or a type that your returned object derives from).
Now, having said all of this. IMHO you shouldn't be returning a platform specific object from a web service in the first place. That defeats the purpose of a web service. I would recommend returning the XML representation of the strongly typed DataSet from your web service, rather than the object itself. This keeps your application open to the possibility of needing a heterogenius architecture in the future.
> All, > I have a webservice webmethod which does returns a strongly typed dataset [quoted text clipped - 14 lines] > > What am I doing wrong? Sundar Narasiman - 05 Mar 2007 02:57 GMT N. Shehzad,
The main reason for the problem is that you are bundling the TypedDataset into an class library (dll). Please do not package the TypedDataset into an assembly. If you do that, you will end-up in the namespace conflicts. Try using the .xsd for TypedDataset at the Webservice side and the Client Side. I've used .xsd (instead of bundling .xsd inside assembly), it works. I could able to successfully pass the TypedDataset back-and-forth between webservice and client.
Please let me know if this helps
 Signature Thanks & Regards, Sundar Narasiman
> All, > I have a webservice webmethod which does returns a strongly typed dataset [quoted text clipped - 13 lines] > > What am I doing wrong? N. Shehzad - 05 Mar 2007 15:21 GMT Hi Sundar, I did create an .xsd schema first, and created a class file for that xsd schema using SDK Command prompt. How would I reference that class on the client side if I do not create a .dll library. Do I need to include that class on the client project as well?
Thanks
> N. Shehzad, > [quoted text clipped - 26 lines] > > > > What am I doing wrong? John Saunders - 05 Mar 2007 18:39 GMT > Hi Sundar, > I did create an .xsd schema first, and created a class file for that xsd [quoted text clipped - 4 lines] > client > project as well? You don't need to do _anything_ to reference the class on the client side. Simply define your web service to return the strongly-typed dataset. Then use Add Web Reference in your client application to cause VS.NET to create the necessary proxy classes. One of those classes will be a client-side version of the server-side dataset. You will use that class on the client side.
You will _never_ reference server-side classes on the client side. NEVER.
John
N. Shehzad - 05 Mar 2007 19:25 GMT John, The problem is it does not create those client proxy classes for the dataset. When I add the web reference, it only creates the localhost reference that's all
> > Hi Sundar, > > I did create an .xsd schema first, and created a class file for that xsd [quoted text clipped - 15 lines] > > John John Saunders - 05 Mar 2007 21:08 GMT > John, > The problem is it does not create those client proxy classes for the > dataset. > When I add the web reference, it only creates the localhost reference > that's > all Select the "localhost" reference in Solution Explorer. Turn on "Show All Files", either by clicking the icon at the top of the Solution Explorer, or else using Project->Show All Files. This will add a "+" sign in front of "localhost". Click the plus signs a few times, and you'll find a Reference.cs (or .vb) file. Open that, and you should see all of the proxy classes.
OTOH, which version of VS.NET are you using? This may be a VS2005 feature.
John
Scott M. - 06 Mar 2007 02:23 GMT No John, only the web service class gets a proxy created for it. Not the return types of the web method calls.
>> John, >> The problem is it does not create those client proxy classes for the [quoted text clipped - 13 lines] > > John John Saunders - 06 Mar 2007 02:45 GMT > No John, only the web service class gets a proxy created for it. Not the > return types of the web method calls. Sorry, man, you need to go check your facts. Create an example and see what happens. I did, and it behaves as I posted.
My only question (which I'll check when I get a chance) is what happens when there's more than one typed dataset involved. One of the posters said that only one proxy is generated, and since my example only used one typed dataset, I can't tell if he's correct or not.
John
N. Shehzad - 05 Mar 2007 21:10 GMT Actually, I got it to work with one dataset, but if my webservice has multiple typed datasets, they do not show up as proxy xsd when I create or update the web reference.
> > Hi Sundar, > > I did create an .xsd schema first, and created a class file for that xsd [quoted text clipped - 15 lines] > > John John Saunders - 05 Mar 2007 21:26 GMT > Actually, I got it to work with one dataset, but if my webservice has > multiple typed datasets, they do not show up as proxy xsd when I create or > update the web reference. See my other post, and which version of VS.NET are you using?
John
N. Shehzad - 05 Mar 2007 22:16 GMT I am using vs 2005. It seems like vs 2005 only creates proxy xsd class for only one strongly typed dataset. if the webservice is using multiple datasets, it does not create multiple classes how can I make the reference map to reflect both classes?
> > Actually, I got it to work with one dataset, but if my webservice has > > multiple typed datasets, they do not show up as proxy xsd when I create or [quoted text clipped - 3 lines] > > John John Saunders - 05 Mar 2007 23:01 GMT >I am using vs 2005. It seems like vs 2005 only creates proxy xsd class for > only one strongly typed dataset. if the webservice is using multiple > datasets, it does not create multiple classes how can I make the reference > map to reflect both classes? Are both datasets used as return values?
As an experiment, try changing the return type of the method whose dataset is being created in the proxy class with one of those that is not:
Now a proxy for class A is being created:
[WebMethod] A Method1(){return new A();}
[WebMethod] B Method2(){return new B();}
Change it to this and see what happens:
[WebMethod] B Method1(){return new B();}
[WebMethod] A Method2(){return new A();}
John
N. Shehzad - 06 Mar 2007 15:51 GMT John,
Thanks that worked. It seems you need to have the webmethod as strongly typed dataset for all typed datasets in order for the proxy classes to be created on the client side. I had only one return type method that's why only 1 proxy class for the strongly typed dataset was being created.
Thanks for your help again!
> >I am using vs 2005. It seems like vs 2005 only creates proxy xsd class for > > only one strongly typed dataset. if the webservice is using multiple [quoted text clipped - 23 lines] > > John John Saunders - 06 Mar 2007 18:27 GMT > John, > [quoted text clipped - 5 lines] > > Thanks for your help again! Yes, I've found that, in general, only the classes which are referenced in the web method signatures are generated as proxy classes on the client side. That is, the classes need to be used for return values or as parameters.
One other thing I've noticed: if you have a method that takes a parameter of type Base, then type Derived does not have a proxy class generated for it. Only if Derived is explicitly used in a method signature is the proxy generated for it.
John
Scott M. - 06 Mar 2007 02:24 GMT XSD's can't be proxies as XSD's aren't classes. You are confusing Schemas with typed dataSets.
> Actually, I got it to work with one dataset, but if my webservice has > multiple typed datasets, they do not show up as proxy xsd when I create or [quoted text clipped - 23 lines] >> >> John John Saunders - 06 Mar 2007 02:46 GMT > XSD's can't be proxies as XSD's aren't classes. You are confusing Schemas > with typed dataSets. Scott, have you noticed any .xsd files associated with strongly-typed DataSets? Do you have strongly-typed datasets in your projects which do NOT have an .XSD file nearby?
John
Scott M. - 06 Mar 2007 12:32 GMT They are related, but they are not one in the same. Have you noticed that you can delete either the typed dataset or the XSD and continue to use whichever remains?
XSD's are not a MS invention, they are the W3C standard schema mechanism. A typed DataSet is a .NET class that is modeled after your data structure, an XSD is an XML Schema that is modeled after your data.
They can work together, but they are not the same thing. An XSD is not defined in the .NET Framework. A DataSet is. An XSD is not a class (and so you can't have a proxy class for something that isn't a class in the first place), a typed DataSet is.
>> XSD's can't be proxies as XSD's aren't classes. You are confusing >> Schemas with typed dataSets. [quoted text clipped - 4 lines] > > John Scott M. - 06 Mar 2007 02:22 GMT This is not correct.
When you reference a web service, a proxy class for the web service is created on the web service consumer. BUT, proxies for all possible webMethod return types are NOT created.
If the client will be receiving a strongly-typed DataSet, then the client will need this class referenced directly. This can be accomplished by the strongly-typed DataSet existing in a separate assembly that both the web service and it's consumer can both reference separately, or a copy of the class exists in another place for the client to reference.
>> Hi Sundar, >> I did create an .xsd schema first, and created a class file for that xsd [quoted text clipped - 15 lines] > > John John Saunders - 06 Mar 2007 02:44 GMT > This is not correct. > [quoted text clipped - 7 lines] > service and it's consumer can both reference separately, or a copy of the > class exists in another place for the client to reference. Sorry, I don't know where you get this from. Maybe you're talking about .NET 1.1?
Of course proxies are created for the WebMethod return types. How do you expect a client to be able to access them?
I think you're confusing Web Services and Remoting. In the case of Remoting, you_do_ share the data types.
John
Scott M. - 06 Mar 2007 12:34 GMT I am referring to .NET 1.1 and I'm not confusing remoting and Web Services.
The proxy created by VS.NET is a proxy class to the web service class and exposes the web service class's web methods. The return types of those methods do not get proxies created for them.
I think you are confusing the web service class itself and the class(es) that may be returned from them.
>> This is not correct. >> [quoted text clipped - 18 lines] > > John John Saunders - 06 Mar 2007 13:27 GMT >I am referring to .NET 1.1 and I'm not confusing remoting and Web Services. > [quoted text clipped - 4 lines] > I think you are confusing the web service class itself and the class(es) > that may be returned from them. You are making too many assumptions.
Even in .NET 1.1, other classes were created in the proxy file. This includes the types used in return values and the types used in parameters. What's changed in 2.0 is that there appears to be some infrastructure that now permits the typed datasets to be created in the proxy.
I'm enclosing a small VS.NET 2005 solution which demonstrates the new behavior. It has two projects: a Web Service project and a Windows Form client. The service has two methods, each of which returns a different typed dataset. The Windows Forms project has a web reference to the Web Service.
If you look in the "WindowsApplication1\Web References\localhost" directory, you'll see that the XML Schema files for both typed datasets have been imported into the web reference. Further, the References.cs file includes proxy classes for both typed datasets. Note the comment:
// This type definition was generated by System.Data.Design.TypedDataSetSchemaImporterExtension schema importer extension.
This is the magic that allows VS.NET to generate proxy classes for the typed datasets. And, if you look at the WSDL file, you'll see why: none of the structure of the typed datasets is present in the WSDL. A client other than .NET would not be able to treat these return values as objects of a strong type. At best, such a client would be able to treat them as XML elements with a schema to validate against. At that, such a client would need to be able to interpret the schemaLocation attribute in the <s:import> element in the WSDL as an actual URL from which to retrieve the schemas, or else the schemas would need to be provided out-of-band.
One more note, since I've mentioned strongly-typed datasets and schemas in the same post. ;-)
I've found that the new DataSet designer in VS.NET 2005 does not preserve the structure of a schema. I had several VS2003 strongly-typed dataset schemas which, when "converted" to the VS2005 format lost their structure. Worse, the schema resulting from asking a typed dataset instance to do a WriteXmlSchema does not match the original schema. The DataSet itself is added as an outer XmlElement, for instance.
It doesn't matter if you're just using strongly-typed datasets to represent database tables, but I thought I'd mention it for the benefit of any readers using XML schemas as XML schemas.
John
Scott M. - 06 Mar 2007 17:55 GMT John,
I don't know what assumptions you think I'm making. In fact, I think you have made an assumption that I use VS.NET 2005, which I do not, so I can't open your solution and look at it.
But this is besides the point....
You have described how VS.NET uses XSD's, but your missing the point that an XSD is not a class and so, can't have a proxy created for it.
The typed-dataset is the class and the XSD is simply an XML representation of the data structure. Just as a typed-dataset is a .NET class representation of the structure.
Because a dataset can be serialized as XML, the XML that it produces is valid with the XSD, but the XSD is in no way used as a class, that's what the typed-dataset is for.
You yourself prove this when you write:
"Further, the References.cs file includes proxy classes for both typed datasets"
The proxy is created from the typed dataset (a .NET class), not the XSD (an XML file).
I may be mistaken about proxies for return types being automatically created by VS.NET in the web service consumer, but I stand by my assertion that a typed dataset is NOT an XSD and vice versa. A proxy can be made for a class, but an XSD isn't a class and so, any proxies are for (just as you wrote) the dataset class, not the XSD.
I think your confusion here is how tightly VS.NET *can* integrate the dataset with the xsd. But the XSD, is at it's core an entirely separate animal from a dataset.
You could say that a typed-dataset is just MS's proprietary .NET way of working with the W3C standard XML validation document.
> >I am referring to .NET 1.1 and I'm not confusing remoting and Web Services. > > [quoted text clipped - 51 lines] > > John Scott M. - 06 Mar 2007 02:18 GMT This is completely incorrect.
A Typed DataSet is NOT an XSD. These are two different things.
A Typed DataSet is a class that inherits from the DataSet class and has custom properties and methods created for it that model your specific data. This is a class and there is NO POSSIBLE WAY to not have it be part of an assembly (or a namespace for that matter).
An XSD is an XML Schema Document, which is simply an XML Schema model of your empty DataSet.
I have already provided the source and solution of your problem in my first reply.
> N. Shehzad, > [quoted text clipped - 32 lines] >> >> What am I doing wrong?
Free MagazinesGet 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 ...
|
|
|