passing objects via WebServices
-
Hello all, I got this object obj, it's an instance of CTestClass. Now I want to pass this object across my network using MyWebService. So, I've wrote a Method called GoObject:
[WebMethod] [XmlInclude(typeof(CTestClass))] public bool GoObject(object obj) { if(obj is CTestClass) { return true; } return false; }
But, whenever I call this method passing a client side instance of CTestClass, I get the following error message: The type TesteClass.CTestClass was not expected. Use theXmlInclude
orSoapInclude
attribute to specify types that are not known statically. What's the professional way to perform what I'm trying to :) Best Regards Raphael Amorim Dantas Leite VC++, Java and C# programmer. Win32 and PocketPC enviroments -
Hello all, I got this object obj, it's an instance of CTestClass. Now I want to pass this object across my network using MyWebService. So, I've wrote a Method called GoObject:
[WebMethod] [XmlInclude(typeof(CTestClass))] public bool GoObject(object obj) { if(obj is CTestClass) { return true; } return false; }
But, whenever I call this method passing a client side instance of CTestClass, I get the following error message: The type TesteClass.CTestClass was not expected. Use theXmlInclude
orSoapInclude
attribute to specify types that are not known statically. What's the professional way to perform what I'm trying to :) Best Regards Raphael Amorim Dantas Leite VC++, Java and C# programmer. Win32 and PocketPC enviromentsAs the error states, the WSDL for Web Service is missing a type declaration for the CTestClass. You either need to edit the WSDL manually, or better yet - do what the error tells you. Include the
CTestClass
type using either theXmlIncludeAttribute
(better portability) or theSoapIncludeAttribute
that take aType
as a parameter:public class Test : WebService
{
[WebMethod]
[XmlInclude(typeof(CTestClass))]
public bool GenericUpdate(object o) { /* ... */ }
}Otherwise, the web service doesn't understand know the WSDL for the
CTestClass
type and can't deserialize it. See the .NET Framework documentation forXmlIncludeAttribute
for a good example. PS: You shouldn't use the old Polish notation with classes and members in .NET, like beginning classes with a "C". Most languages have language guidelines that developers should follow when possible to make all libraries consistent and easy to use. There is a design guidelines in the .NET Framework that describes the changes (really not that bad and mostly for public/protected members), but they are good to follow.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
As the error states, the WSDL for Web Service is missing a type declaration for the CTestClass. You either need to edit the WSDL manually, or better yet - do what the error tells you. Include the
CTestClass
type using either theXmlIncludeAttribute
(better portability) or theSoapIncludeAttribute
that take aType
as a parameter:public class Test : WebService
{
[WebMethod]
[XmlInclude(typeof(CTestClass))]
public bool GenericUpdate(object o) { /* ... */ }
}Otherwise, the web service doesn't understand know the WSDL for the
CTestClass
type and can't deserialize it. See the .NET Framework documentation forXmlIncludeAttribute
for a good example. PS: You shouldn't use the old Polish notation with classes and members in .NET, like beginning classes with a "C". Most languages have language guidelines that developers should follow when possible to make all libraries consistent and easy to use. There is a design guidelines in the .NET Framework that describes the changes (really not that bad and mostly for public/protected members), but they are good to follow.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
How do I edit the WSDL manually? Raphael Amorim Dantas Leite VC++, Java and C# programmer. Win32 and PocketPC enviroments
-
How do I edit the WSDL manually? Raphael Amorim Dantas Leite VC++, Java and C# programmer. Win32 and PocketPC enviroments
Short answer...don't! There's a lot of work involved (and you really have to understand WSDL) and ever time you compile with changes you'd either invalidate the WSDL or loose it. All you'd be doing, though, is adding a definition for the
CTestClass
, which is what using the simple one-linerXmlIncludeAttribute
above the method would do. I strongly suggest using the latter route. If, for some reason, you need to edit the WSDL directly I suggest you check-out some of the MSDN articles on Web Services. Several of them do this but they have enterprise-ready solutions that sometimes require more than the framework provides. From what I can tell, theXmlIncludeAttribute
is exactly what you need (since ASP.NET will pull the type from the dependent assembly which must be there for theo is CTestClass
statement to work anyway, into the WSDL generated for WS clients).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Short answer...don't! There's a lot of work involved (and you really have to understand WSDL) and ever time you compile with changes you'd either invalidate the WSDL or loose it. All you'd be doing, though, is adding a definition for the
CTestClass
, which is what using the simple one-linerXmlIncludeAttribute
above the method would do. I strongly suggest using the latter route. If, for some reason, you need to edit the WSDL directly I suggest you check-out some of the MSDN articles on Web Services. Several of them do this but they have enterprise-ready solutions that sometimes require more than the framework provides. From what I can tell, theXmlIncludeAttribute
is exactly what you need (since ASP.NET will pull the type from the dependent assembly which must be there for theo is CTestClass
statement to work anyway, into the WSDL generated for WS clients).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
So, Is something like this? [WebMethod] [XmlInclude(typeof(CTestClass))] [XmlIncludeAttribute(typeof(CTestClass))] public bool GoObject(object obj) { if(obj is CTestClass) { return true; } return true; } Raphael Amorim Dantas Leite VC++, Java and C# programmer. Win32 and PocketPC enviroments
-
So, Is something like this? [WebMethod] [XmlInclude(typeof(CTestClass))] [XmlIncludeAttribute(typeof(CTestClass))] public bool GoObject(object obj) { if(obj is CTestClass) { return true; } return true; } Raphael Amorim Dantas Leite VC++, Java and C# programmer. Win32 and PocketPC enviroments
You actually have the
XmlIncludeAttribute
specified twice! Remember that languages like C#, VB.NET, and MC++ don't require the "Attribute" suffix, though some languages might (it depends on the compiler). Simply:[WebMethod]
[XmlInclude(typeof(CTestClass))]
public bool GetObject(object obj)
{
if (obj is CTestClass) return true;
return true;
}And make sure that the assembly in which
CTestClass
is defined is referenced as a dependent assembly (or in the Web Service assembly itself, although this doesn't make any sense since the client can't use the type (even if retyped, since a Type is defined by its namespace, class name, assembly name, and public key token).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----