retoro
9/2/2004 2:33:00 PM
The problem is that you haven't really "shared" the interfaces. It is not
sufficient to define the same interface in both the client and server
assemblies. The assembly in which an interface lives is considered part of
its identity. Thus, when your client calls getA(), it is receiving an object
which implements "IA, Server", and the client is attempting to cast it to
"IA, Client". Since the class doesn't implement this interface, you get the
exception.
To solve this, put your shared interfaces in a seperate assembly and
reference that assembly from both client and server. Your Server assembly
would still hold the definition for class A, but IA would now be in a
separate assembly.
Ken
"Julien Barbé" <barbezz@hotmail.com> wrote in message
news:41361234$0$3552$ba620e4c@news.skynet.be...
> Hi,
>
> Does anyone know where is the problem ???
>
> I receive a Unhandled Exception: System.InvalidCastException: Return
> argument has an invalid type. on the getA() function !
>
> getA() is a function from object B and returns a new object A.
> object A has one property i that is a int = 1.
>
> I try to to display property i of the A on the client.
> From the server, only object B is remotely available.
> From the client I call getA() from a objB
> I use "shared" interfaces between client and server.
>
> Thanks,
> Julien.
>
> //CLIENT CODE *****************
> using System;
> using System.Runtime.Remoting;
> using System.Runtime.Remoting.Channels.Tcp;
> using System.Runtime.Remoting.Channels;
>
> namespace Test
> {
> //SHARED INFO ***********
> public interface IA
> {
> int i {get;}
> }
> public interface IB
> {
> IA getA();
> }
> //************************
>
> class RemotingExampleClient
> {
> static void Main(string[] args)
> {
> ChannelServices.RegisterChannel(new TcpClientChannel());
> IB objB =(IB)Activator.GetObject(typeof(IB),"tcp://localhost:9988/B");
> Console.WriteLine("B created " + objB.ToString());
> Console.WriteLine("objB.getA().i"+ objB.getA().i);
> }
> }
> }
>
>
>
> //SERVER CODE *****************
> using System;
> using System.Runtime.Remoting;
> using System.Runtime.Remoting.Channels;
> using System.Runtime.Remoting.Channels.Tcp;
> using System.Runtime.Serialization;
>
> namespace Test
> {
> public class EntryPoint
> {
> public static void Main(string[] args)
> {
> TcpServerChannel channel = new TcpServerChannel(9988);
> ChannelServices.RegisterChannel(channel);
> RemotingConfiguration.RegisterWellKnownServiceType(typeof(B),"B",
> WellKnownObjectMode.SingleCall);
> System.Console.WriteLine("Press Any Key");
> System.Console.ReadLine();
> }
> }
>
> //SHARED INFO ***********
> public interface IA
> {
> int i {get;}
> }
> public interface IB
> {
> IA getA();
> }
> //************************
>
> public class A : MarshalByRefObject, IA
> {
> private int i_i = 1;
> public int i
> {
> get {return this.i_i;}
> }
> }
>
> public class B : MarshalByRefObject, IB
> {
>
> public IA getA()
> {
> return new A();
> }
> }
>
> }
>
>
>