简体   繁体   English

在 WCF 中使用泛型时客户端的奇怪类型

[英]Strange-looking type in client side when using generics in WCF

I'm rather new to WCF, and have created created a service with the following two methods:我对 WCF 比较陌生,并且使用以下两种方法创建了一个服务:

[ServiceContract]
public interface IDB
{
    [OperationContract]
    DbResponse<Student> AddStudent(string pnr, string firstName, string lastName, string userName, string email, string cls);

    [OperationContract]
    DbResponse<List<Student>> FindStudent(string searchString);
}

The DbResponse is a generic class: DbResponse 是一个通用类:

[DataContract]
public class DbResponse<T>
{
    [DataMember]
    public DbStatus Status { get; set; }
    [DataMember]
    public string Message { get; set; }
    [DataMember]
    public T Data { get; set; }
}

where DbStatus is an enum:其中 DbStatus 是一个枚举:

[DataContract]
public enum DbStatus
{ 
    [EnumMember]
    Ok,
    [EnumMember]
    Warning,
    [EnumMember]
    Failed
};

This works fine, except that on the client side, since adding the generic property to DbResponse, I can no longer use the following:这工作正常,除了在客户端,因为将通用属性添加到 DbResponse,我不能再使用以下内容:

string filter = "Foo";
DBClient dbc = new DBClient();
WebInterface.Services.DbResponse response = dbc.FindStudent(filter);

Instead, according to Intellisense, I have to use the strange-looking (and ugly) type:相反,根据 Intellisense,我必须使用看起来很奇怪(和丑陋)的类型:

WebInterface.Services.DbResponseOfArrayOfStudentL1_PcKk2u response = dbc.FindStudent(filter);

I would greatly appreciate it if anyone could shed some light over why this happens and if (and how) I can use a "cleaner" type name.如果有人能解释为什么会发生这种情况以及我是否(以及如何)可以使用“更干净”的类型名称,我将不胜感激。

Ok, I found out that the hash can be overridden by using the Name property in the DataContract attribute (http://jeffbarnes.net/blog/post/2007/05/10/wcf-serialization-and-generics.aspx):好的,我发现可以通过使用 DataContract 属性中的 Name 属性来覆盖哈希(http://jeffbarnes.net/blog/post/2007/05/10/wcf-serialization-and-generics.aspx):

[DataContract(Name = "DbResponse{0}"]
public class DbResponse<T>
{
    [DataMember]
    public DbStatus Status { get; set; }
    [DataMember]
    public string Message { get; set; }
    [DataMember]
    public T Data { get; set; }
}

This will still output eg DbResponseOfArrayOfStudent, but at least there is no ugly hash, so I guess I will have to live with that.这仍然会输出例如 DbResponseOfArrayOfStudent,但至少没有丑陋的哈希值,所以我想我将不得不忍受它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM