简体   繁体   中英

Renaming WCF generic class with DataContract Name attribute

My WCF interface looks like this:

[ServiceContract]
public interface IService
{
   [OperationContract]
    Foo<String>[] Test();
}

The Foo<T> class looks like this:

[DataContract(Name = "FooOf{0}")]
public class Foo<T>
{
    public T Value { get; set; }
}

I publish the WCF service and add service reference to my client code, then I use this method like this:

var client = new ServiceClient(new BasicHttpBinding(), new EndpointAddress(@"http://myServer:port/Service.svc"));
FooOfString[] result = client.Test();

However, I cannot access the properties of the generated classes, for example, I cannot do this:

var value = result[0].Value; // cannot access property, does not compile

I am able to do the EXACT same thing by removing the [DataContract(Name = "FooOf{0}")] part of my Foo<T> class and I can access the properties, the problem is the name of the generated class which changes to FooOfStringCHtiIp13 and that looks ugly, I'm trying to rename it to something a bit more readable. This operation, however, now works:

FooOfStringCHtiIp13[] result = client.Test();
var value = result[0].Value; // can access, compiles

It feels like something does not get serialized and I am not using this correctly. Any idea how to achieve this correctly?

Solved my problem, the issue was in my Value property of Foo<T> class, I had to add DataMemberAttribute to my property:

[DataContract(Name = "FooOf{0}")]
public class Foo<T>
{
    [DataMemberAttribute(Name = "Value")]
    public T Value { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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