Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming Generic DataContracts in WCF

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.

like image 946
Andrew Harry Avatar asked Feb 04 '09 01:02

Andrew Harry


People also ask

What is DataContract and DataMember in WCF?

A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.

What is known type attribute in WCF?

According to MSDN the KnownTypeAttribute class allows you to specify, in advance, the types that should be included for consideration during deserialization. The WCF service generally accepts and returns the base type. If you expect the service to accept and return an inherited type then we use the knowntype attribute.

What is DataContract attribute?

[DataContract] attribute specifies the data, which is to serialize (in short conversion of structured data into some format like Binary, XML etc.) and deserialize(opposite of serialization) in order to exchange between the client and the Service.

Which attribute is used for defining data contracts in WCF?

The DataContract attribute is used to mention the custom class as data contract and the DataMember attribute is used to mention the data member as a member of the data contract. We can use this for both the data member and properties of the class.


1 Answers

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

like image 195
Andrew Harry Avatar answered Dec 08 '22 05:12

Andrew Harry