.NET webservice returns a class that can not be cast

  • Thread starter Thread starter LT_Hassan
  • Start date Start date
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?
 
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.
 
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).
 
Back
Top