469,931 Members | 2,652 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,931 developers. It's quick & easy.

.NET webservice returns a class that can not be cast

This is only a model program, because it would take too long to
demonstrate the real solution. Anyway,
I have 3 projects - ClassLibrary1, ConsoleApplication1 and WebSite1
(which hosts webservice).
ClassLibrary1 looks like this:
------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary1
{
public interface IClass1
{
string Message
{
get;
set;
}
}
}
--------------------------------------------
WebService code file looks like this:
--------------------------------------------
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
public class Class1:ClassLibrary1.IClass1
{
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public Class1 HelloWorld() {
Class1 cl = new Class1();
cl.Message = "Hello World";
return cl;
}

}
-------------------------------------------------------------
And the console application looks like this:
-------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
localhost.Service sr = new
ConsoleApplication1.localhost.Service();
ClassLibrary1.IClass1 cl =
(ClassLibrary1.IClass1)sr.HelloWorld();
Console.WriteLine(cl.Message);
Console.ReadKey();
}
}
}
-----------------------------------------------------------
(localhost is the name of my web-reference to webservice)
both the webservice and consoleApplication holds references to
ClassLibrary1.

The problem is that when I try to run the solution, I get
InvalidCastException in my ConsoleApplication1 on this line:
ClassLibrary1.IClass1 cl = (ClassLibrary1.IClass1)sr.HelloWorld();
I see no reasons why upcasting should fail here, maybe anyone of you
has any ideas about my problem?

Oct 19 '06 #1
3 2007
"LT_Hassan" <tu****@gmail.comwrote in news:1161238967.029467.212270
@m7g2000cwm.googlegroups.com:
The problem is that when I try to run the solution, I get
InvalidCastException in my ConsoleApplication1 on this line:
ClassLibrary1.IClass1 cl = (ClassLibrary1.IClass1)sr.HelloWorld();
I see no reasons why upcasting should fail here, maybe anyone of you
has any ideas about my problem?
Ya unfortunately you can't just cast a web service class back to the
interface class ... There are work arounds, but it's generally not
recommend to pass typed data via web services.

If you need to pass typed data - use (IIS Hosted) Remoting instead.
Oct 19 '06 #2
thank you for your reply.
Could you give me any information about those work-arounds?

On Oct 19, 11:22 am, Spam Catcher <spamhoney...@rogers.comwrote:
"LT_Hassan" <tur...@gmail.comwrote in news:1161238967.029467.212270
@m7g2000cwm.googlegroups.com:
The problem is that when I try to run the solution, I get
InvalidCastException in my ConsoleApplication1 on this line:
ClassLibrary1.IClass1 cl = (ClassLibrary1.IClass1)sr.HelloWorld();
I see no reasons why upcasting should fail here, maybe anyone of you
has any ideas about my problem?Ya unfortunately you can't just cast a web service class back to the
interface class ... There are work arounds, but it's generally not
recommend to pass typed data via web services.

If you need to pass typed data - use (IIS Hosted) Remoting instead.
Oct 19 '06 #3
"LT_Hassan" <tu****@gmail.comwrote in
news:11**********************@f16g2000cwb.googlegr oups.com:
thank you for your reply.
Could you give me any information about those work-arounds?

You need to implement IXMLSerializble:

http://weblogs.asp.net/cweyer/archiv...02/205798.aspx

Also, when you generate your web service reference, I believe you have to
do it manually using the WSDL.exe tool (not via Visual Studios).

As you see - using web services to return custom data types adds a lot of
programming overhead... so it's better to use Remoting.

In anycase, the moment you start using custom data types - your web service
is no longer "generic" thus not compatible with other systems anyways
(other systems will need to implement custom parsing of your data types).
Oct 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michael | last post: by
4 posts views Thread by Adrian Meyer | last post: by
5 posts views Thread by mtv | last post: by
4 posts views Thread by Martin Ehrlich | last post: by
9 posts views Thread by Nick Gilbert | last post: by
1 post views Thread by Mike9900 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.