19

I am using a Generic Class as a Response Data Contract. All is good and this is streamlining the design of my WCF service significantly.

Each request is given a standard response object with the following signature:

  • Status (Enum)
  • Message (String)
  • Result (T)

Below is the Response Class:

[DataContract]
    public class Response<T>
    {
        public Response() {}

        public Response(T result)
        {
            this.result = result;
            if (result != null)
            {
                this.status = Status.StatusEnum.Success;
            }
            else
            {
                this.status = Status.StatusEnum.Warning;
            }
        }

        public Response(T result, Status.StatusEnum status)
        {
            this.status = status;
            this.message = message;
        }

        public Response(T result, Status.StatusEnum status, string message)
        {
            this.status = status;
            this.message = message;
            this.result = result;
        }

        [DataMember]
        public Status.StatusEnum status { get; set; }

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

        [DataMember]
        public T result { get; set; }
    }

And this works brillantly. Only problem I have is that the WCF Client is given a really crappy name for this object "ResponseOfAccountnT9LOUZL"

Is there a way to get around this issue?

Should I be using this class as just a Abstract class which is inherited? I'd rather not have multiple classes cluttering my code.

Andrew Harry
  • 13,773
  • 18
  • 67
  • 102
  • 4
    Here is the value of stackoverflow for me... I knew that I had asked this question before. and had even answered! And there it is - thanks – Andrew Harry May 22 '09 at 03:56
  • what is your return type of your main service method. ie could you should how you use this. PLZ – Seabizkit Jan 14 '16 at 11:41

2 Answers2

27

Ok found the Answer

You can specify the Serialised version using the following syntax:

[DataContract(Name = "MyClassOf{0}{1}")]

class MyClass { }

So if I had a Class called Response which takes a Generic T parameter I would use

[DataContract(Name = "ResponseOfType{0}")]

class Response { }

Andrew Harry
  • 13,773
  • 18
  • 67
  • 102
1
[DataContract(Name = "ReturnObjectOfType{0}")]
    public class ReturnObject<T>
    {....

//Iservice
[OperationContract]
        ReturnObject<AdresKisiBilgi> BeldeAdresKisiBilgiSorgula(string tcKimlikNo);


//Service
public ReturnObject<HbysBusiness.MernisGuvenService.AdresKisiBilgi> BeldeAdresKisiBilgiSorgula(string tcKimlikNo)
        {
            return new MernisBiz().BeldeAdresKisiBilgiSorgula(tcKimlikNo);
        }


client:
 public ReturnObjectOfAdresKisiBilgi BeldeAdresKisiBilgiSorgula(string tcKimlikNo)
        {....

Thank you Harry

badp
  • 11,409
  • 3
  • 61
  • 89