在 WCF 中命名通用数据契约
我使用通用类作为响应数据契约。 一切都很好,这显着简化了我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的找到了 答案
您可以使用以下语法指定序列化版本:
因此,如果我有一个名为 Response 的类,它采用通用 T 参数
我会用
Ok found the Answer
You can specify the Serialised version using the following syntax:
So if I had a Class called Response which takes a Generic T parameter
I would use
谢谢你哈利
Thank you Harry