.NETGURU
System.InvalidCastException: Return argument has an invalid type
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.remoting.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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

}

Reply to this message...
 
    
Sam Santiago
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
Click here to reveal e-mail address
http://www.SoftiTechture.com
_______________________________
"Julien Barbé" <Click here to reveal e-mail address> wrote in message
news:41361234$0$3552$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Julien Barbé
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" <Click here to reveal e-mail address> wrote in message
news:%23G%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Jon Z. (VIP)
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:

[Original message clipped]

Reply to this message...
 
    
Ken Kolda
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é" <Click here to reveal e-mail address> wrote in message
news:41361234$0$3552$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Julien Barbé
You're right !

It worked when putting it in another assembly !

thanks for your explaination...

Julien.

"Ken Kolda" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.Activator
System.Console
System.InvalidCastException
System.MarshalByRefObject
System.Runtime.Remoting.Channels.ChannelServices
System.Runtime.Remoting.Channels.Tcp.TcpClientChannel
System.Runtime.Remoting.Channels.Tcp.TcpServerChannel
System.Runtime.Remoting.RemotingConfiguration
System.Runtime.Remoting.WellKnownObjectMode
System.Runtime.Serialization.SerializationException




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification