3

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

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

Here IFoo is the interface & AFoo is the concrete type that inherits.

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. 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.'

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.


Update 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

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

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? Any Ideas?

Gagan
  • 305
  • 1
  • 13
  • Did you try removing the `out` ? Is AFoo class decorated with DataContract attribute? – Chetan Jul 17 '18 at 19:08
  • out is not a problem. why, because all my service methods have out arguments and it works fine. Also as I said AFoo is already a known type. – Gagan Jul 17 '18 at 19:15

3 Answers3

1

Take a look at this other question: DataContract and inheritance?

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.

Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54
  • Thanks @Kevin , this is what has worked for me so far, It is not a proper solution since this requires me to have a Base Class for the inheritance though I wanted to use the Interface implementations. I am gonna add an answer on how I made it work. – Gagan Jul 19 '18 at 13:49
0

I have seen this because the type contained a reference to an enum type that was not marked with the DataContract attribute. 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'.

Matt Whitfield
  • 6,436
  • 3
  • 29
  • 44
  • I even tried removing all the Properties from the Class AFoo & the interface IFoo , same result – Gagan Jul 17 '18 at 22:19
0

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.

Firstly, I had to Add a Base Class Foo as described in the question section. Next I had to Add the KnownType attributes with all the Child(inherited) classes, in my case it was only 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;
}
Gagan
  • 305
  • 1
  • 13