Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm currently testing several ways to have one application get hold of an object from another application. Both are written in C# and wether I use IPC channels or WCF Service Contracts I end up with the same problem. If I use a simple class whose properties and methods only return the built in types (such as int, string or ArrayList) it works fine, but if I add another property of a custom made type I get an error. IPC gives me a InvalidCastException and WCF puts the channel in a Faulted state and it breaks. Obviously I'm doing something wrong and I hope someone can give me a hand. The server looks like this:

public interface IBase {<br /><br />   int IntTest { get; }<br />   String StringTest { get; }<br />   IOther OtherTest { get; }<br /><br />   String Test(String txt);<br />   IOther Test3();<br />}<br /><br />public interface IOther {<br />   String StringTest { get; }<br />   String Test(String txt);<br />}<br /><br />public class Base : MarshalByRefObject, IBase {<br /><br />   public int IntTest {<br />     get { return 4; }<br />   }<br /><br />   public string StringTest {<br />     get { return "A string from Base"; }<br />   }<br /><br />   public IOther OtherTest {<br />     get { return new Other(); }<br />   }<br /><br />   public string Test(string txt) {<br />     return "Base called with: " + txt;<br />   }<br /><br />   public IOther Test3() {<br />     return new Other();<br />   }<br /><br />}<br /><br />public class Other : MarshalByRefObject, IOther {<br /><br />   public string StringTest {<br />     get { return "A string from Other"; }<br />   }<br /><br />   public string Test(string txt) {<br />     return "Other method called with: " + txt;<br />   }<br /><br />}


The server is registered like this:
public MainWindow() {<br />   InitializeComponent();<br />   IpcChannel ipcCh = new IpcChannel("IPChannelName");<br />   ChannelServices.RegisterChannel(ipcCh, false);<br />  RemotingConfiguration.RegisterWellKnownServiceType(typeof(Base), "CName", WellKnownObjectMode.Singleton);<br />}


The client looks something like this:

IpcChannel ipcCh = new IpcChannel("myClient");<br />ChannelServices.RegisterChannel(ipcCh, false);<br />obj = (IBase)Activator.GetObject(typeof(IBase), "ipc://IPChannelName/CName");<br />Console.WriteLine("Returns: " + obj.Test("a text"));<br />Console.WriteLine("Returns: " + obj.StringTest + " " + obj.StringTest.Length);<br />Console.WriteLine("Returns: " + obj.IntTest);<br />Console.WriteLine(obj.OtherTest.ToString());


The last line gives me the InvalidCastException or, in the cae of WCF, the Faulted pipe. Please help.
Posted

1 solution

Hi,

this may seem silly but it looks as if you've put the interfaces in the *server* application. This is certainly possible, but would require the client to have a reference to the server. Normally one doesn't want to deploy the server on the client machine :) so one would put the interfaces in a common assembly to be deployed on both client and server.

That said, if you have duplicated the interface declarations so that the code exists in both client and server assemblies then that is your problem - and it should indeed result in InvalidTypeCastException.
 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900