5

I have the following however I am not sure this is the correct way of doing it.

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 the code that uses the 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?

Tyler Buchanan
  • 311
  • 1
  • 4
  • 18
  • What exactly are you trying to achieve? Parameter that does what? – Jerrington Oct 28 '14 at 15:14
  • The WCF service takes in 4 paratmers. One happens to be an interface type of IDataItem. I need to tell the service what kind of objects take that interface correct? – Tyler Buchanan Oct 28 '14 at 15:16
  • The only interfaces involved here are `IEnumerable, `IDataItem` and `IService`. What exactly do you mean? – Codor Oct 28 '14 at 15:22
  • found the answer on this post... http://stackoverflow.com/questions/7213083/using-class-interface-as-a-parameter-in-wcf-service – Tyler Buchanan Mar 26 '15 at 21:20

3 Answers3

2

You need to use the KnownTypeAttribute instead of the ServiceKnownTypeAttribute.

papadi
  • 985
  • 2
  • 9
  • 22
1

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

[KnownTypeAttribute(typeof(IDataItem))]

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

Edit: Should be KnownTypeAttribute not ServiceKnownTypeAttribute as papadi pointed out correctly.

Edit 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
    {
        ...
    }
}
Jerrington
  • 113
  • 4
0

Do you mean like 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 still looking 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; }
    }
}
Tyler Buchanan
  • 311
  • 1
  • 4
  • 18
  • 1
    Sorry, I take it back, your answer is not entirely correct. In your service declaration you need `ServiceKnownTypeAttribute` not `KnownTypeAttribute` (`DataItem` class is marked correctly). – Jerrington Oct 28 '14 at 16:19
  • Filled out a SoapUI request for the service and I am getting everything except the data parameter. The list is empty. – Tyler Buchanan Oct 28 '14 at 17:30