简体   繁体   English

带有Interface参数的WCF调用服务方法导致SocketException

[英]WCF calling service method with an Interface argument results in SocketException

I have a WCF Service where I have implemented a service method with an out argument of a type which is an Interface like this 我有一个WCF服务,我已经使用类型的out参数实现了一个服务方法,这是一个像这样的接口

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

Here IFoo is the interface & AFoo is the concrete type that inherits. 这里IFoo是接口, AFoo是继承的具体类型。

Then on the Client Side I am calling this method using the service reference & receving the following error 然后在客户端我使用服务引用调用此方法并接收以下错误

System.ServiceModel.CommunicationException: 'An error occurred while receiving the HTTP response to http://localhost:4504/MyService . System.ServiceModel.CommunicationException:'接收到http:// localhost:4504 / MyService的HTTP响应时发生错误。 This could be due to the service endpoint binding not using the HTTP protocol. 这可能是由于服务端点绑定不使用HTTP协议。 This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). 这也可能是由于服务器中止HTTP请求上下文(可能是由于服务关闭)。 See server logs for more details.' 有关详细信息,请参阅服务器日志。

What is Interesting is that when I remove the interface argument from the service method, everything works just fine. 有趣的是,当我从服务方法中删除接口参数时,一切正常。 For example 例如

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

The AFoo type is already a known type on the Client side and I can use it normally. AFoo类型已经是客户端的已知类型,我可以正常使用它。


Update 1 更新1

Adding a Base Class Foo such that Afoo inherits from Foo & Foo inherits from IFoo ex: AFoo : Foo : IFoo (logically) has the same error when the service method is modified as 添加基类Foo ,使得Afoo继承自Foo&Foo继承自IFoo ex:AFoo:Foo:IFoo(逻辑上)在修改服务方法时具有相同的错误

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

Again I kept all Classes & Interfaces empty (meaning they have nothing inside them) 我再次将所有类和接口保持为空(意味着它们内部没有任何内容)


Update 2 更新2

The following seems to work perfectly fine 以下似乎工作得非常好

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

Why did not the Base Class Foo worked? 为什么Base Class Foo没有工作? Any Ideas? 有任何想法吗?

Take a look at this other question: DataContract and inheritance? 看一下这个问题: DataContract和继承?

It seems like you need to decorate the base class with KnownType of the sub-class to do this kind of thing, and have them both under DataContract of course. 看起来你需要用子类的KnownType来装饰基类来做这种事情,当然在DataContract下都有它们。

I have seen this because the type contained a reference to an enum type that was not marked with the DataContract attribute. 我已经看到了这个,因为该类型包含对未使用DataContract属性标记的enum类型的引用。 The error you're seeing is most likely hiding an inner error like that. 您看到的错误很可能隐藏了这样的内部错误。 Sometimes you can catch these by going to Debug -> Exceptions and turning on 'break on all thrown exceptions'. 有时您可以通过调试 - >异常并打开'中断所有抛出异常'来捕获这些内容。

Workaround : 解决方法

Note: this is not an answer, since it forces to create a Base Class. 注意:这不是答案,因为它强制创建基类。

I got it working finally and I am gonna share what I did. 我终于开始工作,我将分享我的所作所为。 In short, I was missing the KnownType in the DataContract . 简而言之,我错过了DataContract中KnownType

Firstly, I had to Add a Base Class Foo as described in the question section. 首先,我必须按问题部分所述添加基类Foo Next I had to Add the KnownType attributes with all the Child(inherited) classes, in my case it was only AFoo . 接下来我必须使用所有Child(继承)类添加KnownType属性,在我的例子中它只是AFoo

Here is what the code looks like 这是代码的样子

public interface IFoo
{
}

[DataContract]
[KnownType(typeof(AFoo))]
public class Foo : IFoo
{ // this is the Base Class
}

public class AFoo : Foo
{
}

Now the service method looks like this 现在服务方法看起来像这样

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

All Good!! 都好!!

It would be nice if instead it was possible to have the service method like thie following (without needing a Base Class) 如果相反可能有像下面这样的服务方法(不需要基类)会很好

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM