如何在WCF服务中将Interface作为参数传递?

时间:2014-10-28 15:08:56

标签: c# wcf

我有以下内容,但我不确定这是否正确。

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 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);
        }
    }
}

目前我唯一的对象类型如下。

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; }
    }
}
我是朝着正确的方向前进的吗?

3 个答案:

答案 0 :(得分:2)

您需要使用KnownTypeAttribute而不是ServiceKnownTypeAttribute。

答案 1 :(得分:1)

如果有问题的界面是您在IDataItem参数中使用的IEnumerable<IDataItem>,那么您需要将界面标记为已知类型:

[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
    {
        ...
    }
}

答案 2 :(得分:0)

你的意思是以下几个?

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);
    }
}

我的对象仍然看起来像这样。

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; }
    }
}

这是我的界面

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; }
    }
}