Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Interface as parameter

Tags:

wcf

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

like image 245
Raj Kumar Avatar asked Jul 05 '12 12:07

Raj Kumar


2 Answers

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);
}
like image 82
Hasan Salameh Avatar answered Sep 20 '22 07:09

Hasan Salameh


Use [KnownType(typeof(testClass))].

Refer these links:

  • msdn.microsoft.com/en-us/library/ms730167.aspx
  • www.codeproject.com/Tips/108807/Implementing-KnownType-Attribute
like image 26
Vivek Avatar answered Sep 21 '22 07:09

Vivek