Raj Kumar
Raj Kumar

Reputation: 7136

WCF Interface as parameter

I am using interface as input parameter in OperationContract. But when i generate proxy class at client side. I am not able to access the members of interface or class implemeting the ITransaction interface. I am only geeting is object

  1. Service Interface

    [ServiceContract]
    public interface IServiceInterface
    {
    [OperationContract]
    string SyncDatabase(ITransaction TransactionObject);
    }
    
  2. Service class

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

    public interface ITransaction
    {
        ExpenseData ExpData { get; set; }
        void Add(ITransaction transactionObject);
    }
    
  4. Data Contract

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

In above case should i also copy the iTransaction class and interface on client

Upvotes: 7

Views: 5505

Answers (3)

Hasan Salameh
Hasan Salameh

Reputation: 326

You actually need to make your ServiceContract aware of the implementation of the interface you pass 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);
}

Upvotes: 6

Vivek
Vivek

Reputation: 570

Use [KnownType(typeof(testClass))].

Refer these links:

Upvotes: 2

CodingWithSpike
CodingWithSpike

Reputation: 43748

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

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

Upvotes: 0

Related Questions