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?