Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
918 views
in Technique[技术] by (71.8m points)

dynamic - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...