0

I have this Model class on c#

public class Enumerator
{
int ID {get;set;}
string Description {get;set;}
}

I also have nearly 20 classes that implement a method to convert them to an Enumerator class.

My question is simple: Is there a way to write a single method on a WCF service to let the client choose wich class to Enumerate? The returned type is always and Enumerator class. Or do I have to write 20 Operation Contract, one for each class? That doesn't seems so logical.

This data is used mostly to fill ComboBoxes on client side.

ericpap
  • 2,917
  • 5
  • 33
  • 52
  • Using `WebOperationContext.Current.CreateTextResponse` you can return `System.ServiceModel.Channels.Message` (Meaning that, you will make the serialization manually) – EZI Jul 31 '15 at 13:01
  • Thanks @EZI. I'm not sure i follow you. Why do I need to serialize manual if I know the returned type? Can you elaborate? – ericpap Jul 31 '15 at 13:05
  • ericpap. You will return `Message` not the real class. (BTW: You may return *Stream* too. Fill all return value (Serialized form of your object) to a MemoryStream and return it,) – EZI Jul 31 '15 at 13:13

1 Answers1

2

if i understand you in a correct way you want to perform a operation in WCF that will convert any type that implement some IEnumerate (for instance) to Enumerate object. If is's true here is a code

[DataContract]
[KnownType(typeof(Enumerate1))]
[KnownType(typeof(Enumerate2))]
[KnownType(typeof(EnumerateN))]
// up yo 20
public interface IEnumerate 
{
    [DataMember]
    string Description{ get; set; }
    [DataMember]
    int ID {get; set;}
    //  whatever you need 
}

[ServiceContract]
public interface IService
{
    [OperationContract]
    Enumerator GetEnumerator(IEnumerate obj);
}

public Service: IService
{
      Enumerator GetEnumerator(IEnumerate obj)
      {
         // convert
      }
}

Hope that's helped you