问题WCF服务暴露的自定义复杂的接口类型
-
09-09-2019 - |
题
我试图使用WCF其他应用程序获取对象的保持。随着内置类它工作正常,但我试图从WCF操作返回自定义的接口类型,当运行到发生问题的。
我是否包括在这两个应用程序的界面分开,或特异性它作为一个共享组件,我得到相同的结果:与所述消息的CommunicationException“时发生错误,从管道读取:无法识别错误109”。
在界面看起来像这样:
[ServiceContract]
public interface IBase {
int IntTest {
[OperationContract]
get;
}
String StringTest {
[OperationContract]
get;
}
IOther OtherTest {
[OperationContract]
get;
}
}
[ServiceContract]
public interface IOther {
String StringTest {
[OperationContract]
get;
}
}
我的服务器看起来是这样的:
public partial class MainWindow : Window {
private Base fb;
private ServiceHost host;
public MainWindow() {
InitializeComponent();
fb = new Base();
host = new ServiceHost(fb, new Uri[] { new Uri("net.pipe://localhost") });
host.AddServiceEndpoint(typeof(IBase), new NetNamedPipeBinding(),
"PipeReverse");
host.Open();
}
private void Window_Closing(object sender, CancelEventArgs e) {
host.Close();
}
}
这是我的接口的实现:
[Serializable]
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
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(); }
}
}
[Serializable]
[DataContract]
public class Other : MarshalByRefObject, IOther {
[DataMember]
public string StringTest {
get { return "A string from Other"; }
}
}
在客户端看起来像这样:
public partial class Form1 : Form {
IBase obj;
public Form1() {
InitializeComponent();
ChannelFactory<IBase> pipeFactory = new ChannelFactory<IBase>(
new NetNamedPipeBinding(), new EndpointAddress(
"net.pipe://localhost/PipeReverse"));
obj = pipeFactory.CreateChannel();
}
private void button2_Click(object sender, EventArgs e) {
Console.WriteLine("Returns: " + obj.StringTest + " " +
obj.StringTest.Length);
Console.WriteLine("Returns: " + obj.IntTest);
Console.WriteLine(obj.OtherTest);
}
}
一切就像一个魅力除了这一行:
Console.WriteLine(obj.OtherTest);
有给我与消息的CommunicationException“时发生错误,从管道读取:无法识别错误109”。至于我可以告诉大家,是一个破裂的管道因故障状态,但我想不出为什么,或者更重要的是如何解决它。任何想法?
我没有配置文件作为寄托都在代码中完成上述所以我不知道如何打开跟踪,否则我会已经包括了。
解决方案
返回的属性的 OtherTest 需要为一个具体类型而不是接口,否则序列将不能工作。
其他提示
这典型地是序列化错误。放眼[KnownType]属性。测试了这一点,最简单的方法就是直接调用的DataContractSerializer。您可以使用它的writeObject和readObject方法来获得真正的序列化的错误。您还可以检查数据流(通常的FileStream),以确保您键入正确的序列化。
不隶属于 StackOverflow