[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.remoting

System.InvalidCastException: Return argument has an invalid type

Julien Barbé

9/1/2004 6:17:00 PM

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();
}
}

}



5 Answers

Sam Santiago

9/1/2004 8:33:00 PM

0

Could it be this line:

> Console.WriteLine("objB.getA().i"+ objB.getA().i);

objB.geatA().i returns an int and you are trying to use it as a string.

Thanks,

Sam

--
_______________________________
Sam Santiago
ssantiago@n0spam-SoftiTechture.com
http://www.SoftiTe...
_______________________________
"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();
> }
> }
>
> }
>
>
>


Julien Barbé

9/1/2004 9:28:00 PM

0

No, I already tryed...

But I have changed the code : the channel is now HTTP (soap), it was TCP
(binary). I have now another error message :
System.Runtime.Serialization.SerializationException: Parse Error, no
assembly associated with Xml key

But still don't know how to handle the problem...

"Sam Santiago" <ssantiago@n0spam-SoftiTechture.com> wrote in message
news:%23G%23xOLGkEHA.140@TK2MSFTNGP12.phx.gbl...
> Could it be this line:
>
> > Console.WriteLine("objB.getA().i"+ objB.getA().i);
>
> objB.geatA().i returns an int and you are trying to use it as a string.
>
> Thanks,
>
> Sam
>
> --
> _______________________________
> Sam Santiago
> ssantiago@n0spam-SoftiTechture.com
> http://www.SoftiTe...
> _______________________________
> "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();
> > }
> > }
> >
> > }
> >
> >
> >
>
>


Jon Z.

9/2/2004 2:11:00 AM

0

I'm having this same problem with my remoting app. I was able to make it work
by doing away with the interfaces. Of course, this means you have to install
the server DLL on every client. Yuk! Hopefully, somebody can tell us both how
to make this work with interfaces.

Jon Z.

"Julien Barbé" wrote:

> 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();
> }
> }
>
> }
>
>
>
>

retoro

9/2/2004 2:33:00 PM

0

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();
> }
> }
>
> }
>
>
>


Julien Barbé

9/3/2004 2:38:00 PM

0

You're right !

It worked when putting it in another assembly !

thanks for your explaination...

Julien.


"Ken Kolda" <nospam@spam.com> wrote in message
news:uAeaumPkEHA.2500@TK2MSFTNGP09.phx.gbl...
> 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();
> > }
> > }
> >
> > }
> >
> >
> >
>
>