在 WCF 中命名通用数据契约

发布于 2024-07-13 21:22:05 字数 1285 浏览 4 评论 0原文

我使用通用类作为响应数据契约。 一切都很好,这显着简化了我的 WCF 服务的设计。

每个请求都会得到一个带有以下签名的标准响应对象:

  • 状态(枚举)
  • 消息(字符串)
  • 结果(T)

下面是响应类:

[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; }
    }

这非常有效。 我遇到的唯一问题是 WCF 客户端为该对象“ResponseOfAccountnT9LOUZL”指定了一个非常蹩脚的名称,

有没有办法解决这个问题?

我应该将此类用作继承的抽象类吗? 我不想让多个类弄乱我的代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ懵逼小萝莉 2024-07-20 21:22:05

好的找到了 答案

您可以使用以下语法指定序列化版本:

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

类我的类{}

因此,如果我有一个名为 Response 的类,它采用通用 T 参数
我会用

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

类响应{}

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

血之狂魔 2024-07-20 21:22:05
[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)
        {....

谢谢你哈利

[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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文