在WCF中命名Generic DataContracts [英] Naming Generic DataContracts in WCF
问题描述
我正在使用通用类作为响应数据合约。一切都很好,这样可以显着地简化我的WCF服务的设计。
每个请求都被赋予一个标准的响应对象,具有以下签名:
- 状态(枚举)
- 消息(字符串)
- 结果(T) / li>
以下是响应类:
[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;组; }
[DataMember]
public string message {get;组; }
[DataMember]
public T result {get;组; }
}
这样做很出色。只有我遇到的问题是WCF客户端给这个对象ResponseOfAccountnT9LOUZL一个非常糟糕的名字
有没有办法解决这个问题?
我应该使用这个类作为继承的抽象类吗?我宁愿没有多个类混乱我的代码。
确定发现答案
您可以指定序列化版本使用以下语法:
[DataContract(Name =MyClassOf {0} {1})]
class MyClass {}
所以如果我有一个叫Response的类,它接受一个Generic T参数
我将使用
[DataContract(Name =ResponseOfType {0})]
class Response {}
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.
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 { }
这篇关于在WCF中命名Generic DataContracts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!