c# - WCF 使用接口(interface)参数调用服务方法导致 SocketException

标签 c# wcf

我有一个 WCF 服务,我在其中实现了一个服务方法,其中的输出参数类型是这样的接口(interface)

bool GetFoo(out IFoo foo)
{
    foo = new AFoo();
    return true;
}

这里的IFoo是接口(interface),AFoo是继承的具体类型。

然后在客户端我使用服务引用调用此方法并收到以下错误

System.ServiceModel.CommunicationException: 'An error occurred while receiving the HTTP response to http://localhost:4504/MyService. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.'

有趣的是,当我从服务方法中删除接口(interface)参数时,一切正常。例如

bool GetFoo()
{
    IFoo foo = new AFoo();
    return true;
}

AFoo类型在客户端已经是已知类型,我可以正常使用。


更新 1

添加一个基类 Foo 使得 Afoo 继承自 Foo & Foo 继承自 IFoo ex: AFoo : Foo : IFoo (logically) has same error 当服务方法被修改为

bool GetFoo(out Foo foo)
{
    foo = new AFoo();
    return true;
}

我再次将所有类和接口(interface)都保留为空(这意味着它们内部没有任何内容)


更新 2

以下似乎工作得很好

bool GetFoo(out AFoo foo)
{
    foo = new AFoo();
    return true;
}

为什么基类 Foo 不起作用?有什么想法吗?

最佳答案

看看另一个问题:DataContract and inheritance?

看起来你需要用子类的 KnownType 来装饰基类来做这种事情,当然把它们都放在 DataContract 下。

关于c# - WCF 使用接口(interface)参数调用服务方法导致 SocketException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51388598/

相关文章:

c# - 我怎样才能得到我的应用程序的父进程的PID

未应用 WCF Web 服务绑定(bind)配置

.net - MVVM Light 如何判断哪些记录发生了变化

c# - 没有扩展名时 Application_BeginRequest 不触发

c# - ASP.NET Core 6 : Users are logged out after 30 minutes, 需要 6 小时

c# - 以负边距滚动内容

c# - 在 WIndow 服务中捕获 KeyPress 事件

c# - 如何捕获本地主机上生成的 SOAP 请求?

wcf - 如何提高中间件开发技能?

.net - WCF 400 大数据错误请求