0
Answer

How to make complex types visible to the client

Christian

Christian

15y
3.5k
1
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 {

   int IntTest { get; }
   String StringTest { get; }
   IOther OtherTest { get; }

   String Test(String txt);
   IOther Test3();
}

public interface IOther {
   String StringTest { get; }
   String Test(String txt);
}

public class Base : MarshalByRefObject, IBase {

   public int IntTest {
     get { return 4; }
   }

   public string StringTest {
     get { return "A string from Base"; }
   }

   public IOther OtherTest {
     get { return new Other(); }
   }

   public string Test(string txt) {
     return "Base called with: " + txt;
   }

   public IOther Test3() {
     return new Other();
   }

}

public class Other : MarshalByRefObject, IOther {

   public string StringTest {
     get { return "A string from Other"; }
   }

   public string Test(string txt) {
     return "Other method called with: " + txt;
   }

}

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

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

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