问题描述
我目前正在测试几种方法,以使一个应用程序从另一个应用程序中获取一个对象.两者都是用C#编写的,而且如果我使用IPC通道或WCF服务合同,最终都会遇到相同的问题.如果我使用一个简单的类,其属性和方法仅返回内置类型(例如int,string或ArrayList),则它可以正常工作,但是如果我添加自定义类型的另一个属性,则会出现错误. IPC给我一个InvalidCastException,WCF将通道置于Faulted状态并中断.显然我做错了,希望有人可以帮我.服务器看起来像这样:
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());
最后一行给出了InvalidCastException,或者在WCF的原因中是有故障的管道.请帮忙.
The last line gives me the InvalidCastException or, in the cae of WCF, the Faulted pipe. Please help.
推荐答案
这篇关于如何在.NET远程处理和WCF中使复杂的类型对客户端可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!