How to pass an interface as a parameter in a WCF service?

I have the following, however, I'm not sure if this is the right way to do this.

namespace WCFServices
{
    [ServiceContract(Name = "IService")]
    [ServiceKnownTypeAttribute(typeof(DataItem))]
    public interface IService
    {
        [OperationContract]
        void InstantiateThirdParties(string name, IEnumerable<IDataItem> data, IEnumerable<string> modules, IEnumerable<string> states);
    }
}

this is code that uses an interface.

namespace WCFServices
{
    public class Service : IService
    {
        public void InstantiateThirdParties(string name, IEnumerable<IDataItem> data, IEnumerable<string> modules, IEnumerable<string> states)
        {
            Process.ExecuteAll(name, data, modules, states);
        }
    }
}

and my only object type at the moment is the following.

namespace DataObjects
{
    [Serializable]
    [DataContract]
    public class DataItem : IDataItem
    {
        public DataItem();

        [DataMember]
        public CustomerInfo customer { get; set; }

        [DataMember]
        public LoanInfo loan { get; set; }

        [DataMember]
        public DateTime loanProcessingDate { get; set; }

        [DataMember]
        public string moduleID { get; set; }

        [DataMember]
        public string processingState { get; set; }
    }
}

Am I headed in the right direction?

+4
source share
3 answers

You need to use KnownTypeAttribute instead of the ServiceKnownTypeAttribute attribute.

+2
source

If the interface in question is yours IDataItem, which is used in the parameter IEnumerable<IDataItem>, then you need to mark the interface itself as a known type:

[KnownTypeAttribute(typeof(IDataItem))]

: http://blogs.msdn.com/b/domgreen/archive/2009/04/13/wcf-using-interfaces-in-method-signatures.aspx

: KnownTypeAttribute, ServiceKnownTypeAttribute papadi, .

2:

namespace WCFServices
{
    [ServiceContract(Name = "IService")]
    [ServiceKnownTypeAttribute(typeof(DataItem))]
    public interface IService
    {
        [OperationContract]
        void InstantiateThirdParties(string name, IEnumerable<IDataItem> data, IEnumerable<string> modules, IEnumerable<string> states);
    }
}

namespace DataObjects
{
    [DataContract]
    [KnownType(typeof(IDataItem))]
    public class DataItem : IDataItem
    {
        ...
    }
}
+1

Do you mean the following?

namespace WCFServices
{
    [ServiceContract(Name = "IService")]
    [KnownTypeAttribute(typeof(DataItem))]
    public interface IService
    {
        [OperationContract]
        void InstantiateThirdParties(string name, IEnumerable<IDataItem> data, IEnumerable<string> modules, IEnumerable<string> states);
    }
}

With my object it still looks like this.

namespace DataObjects
{
    [DataContract]
    [Serializable]   <------ Not having this was my problem
    [KnownType(typeof(IDataItem))]
    public class DataItem : IDataItem
    {
        public DataItem();

        [DataMember]
        public CustomerInfo customer { get; set; }
        [DataMember]
        public LoanInfo loan { get; set; }
        [DataMember]
        public DateTime loanProcessingDate { get; set; }
        [DataMember]
        public string moduleID { get; set; }
        [DataMember]
        public string processingState { get; set; }
    }
}

here is my interface

namespace Interfaces
{
    public interface IDataItem
    {
        CustomerInfo customer { get; set; }
        LoanInfo loan { get; set; }
        DateTime loanProcessingDate { get; set; }
        string moduleID { get; set; }
        string processingState { get; set; }
    }
}
0
source

All Articles