WCF interface as parameter

I use the interface as an input parameter in OperationContract. But when I create a proxy class on the client side. I cannot access members of an interface or class implementing the ITransaction interface. I'm only geeting this object

  • Service interface

    [ServiceContract] public interface IServiceInterface { [OperationContract] string SyncDatabase(ITransaction TransactionObject); } 
  • Class of service

     class SyncService:IServiceInterface { public string SyncDatabase(ITransaction TransactionObject) { return "Hello There!!"; } } 
  • Interface

     public interface ITransaction { ExpenseData ExpData { get; set; } void Add(ITransaction transactionObject); } 
  • Data contract

     [DataContract] public class Transaction:ITransaction { [DataMember] public ExpenseData ExpData { get; set; } public void Add(ITransaction transactionObject) { } } 

In the above case, I should also copy the class and iTransaction interface to the client

+4
source share
3 answers

Try making your [DataContract] interface and use the [KnownType] attribute to tell WCF what the known implementations of this interface are.

 [DataContract] [KnownType(typeof(Transaction))] public interface ITransaction { [DataMember] ExpenseData ExpData { get; set; } void Add(ITransaction transactionObject); } 
-1
source

In fact, you need to tell ServiceContract about the implementation of the interface that you are passing as a parameter, so WCF will include it in the WSDL.

This should work:

 [ServiceContract] [ServiceKnownType(typeof(Transaction))] public interface IServiceInterface { [OperationContract] string SyncDatabase(ITransaction TransactionObject); } 
+4
source

All Articles