Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange-looking type in client side when using generics in WCF

Tags:

c#

wcf

I'm rather new to WCF, and have created created a service with the following two methods:

[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:

[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:

[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:

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:

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.

like image 417
William Avatar asked Dec 13 '25 10:12

William


1 Answers

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 = "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 e.g. DbResponseOfArrayOfStudent, but at least there is no ugly hash, so I guess I will have to live with that.

like image 123
William Avatar answered Dec 16 '25 06:12

William



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!