.NET webservice returns a class that can not be cast

L

LT_Hassan

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?
 
G

Guest

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.
 
L

LT_Hassan

thank you for your reply.
Could you give me any information about those work-arounds?
 
G

Guest

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/archive/2004/08/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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top