bitsmuggler
bitsmuggler

Reputation: 1729

Interface in a WCF DTO

I try to transmit a DTO (WCF client to WCF server) which contains a suboject with an interface.

My code:

WCF Service Method:

[OperationBehavior(TransactionScopeRequired = true)]
public void SendTest(MyTestDto testDto)
{
  ...
}

MyTestDto class:

[Serializable]
[DataContract(Name = "MyTestDto")]
public class MyTestDto : ITestDto
{
   [DataMember(IsRequired = true, Order = 1, Name = "MyTestDto")]
   [DataMemberValidation(IsRequired = true)]
   public ITest Test { get; set; }

}

ITest interface:

public interface ITest
{
    int Field1 {get;set;}
    int Field2 {get;set,}
}

The problem is, that I get always an FaultException if I will transmit the MyTestDto from Server to Client. I've analysed the WSDL File and the Test Field had the Type: AnyType. I think, this is the problem. I've replaced the ITest with an abstract class and so the communication works (of course I have to set the ServiceKnownType attribute with the abstract class).

Can you help me? Why does it work with the abstract class and not with the interface?

Upvotes: 1

Views: 477

Answers (1)

daryal
daryal

Reputation: 14929

WCF works with concrete types, interfaces can not be serialized over WCF.

Making the interface an abstract class works only if you marked the service with ServiceKnownType or the data contract with KnownType attributes. Below is an example;

public abstract Test
{
    public int Field1 {get;set;}
    public int Field2 {get;set,}
}

public class SomeTest : Test
{ 
    ...
}

[ServiceKnownType(typeof(SomeTest))]
public class SomeService : ISomeService
{
     public void SendTest(Test test)
}

Upvotes: 1

Related Questions