【发布时间】:2011-06-28 19:56:41
【问题描述】:
基本上如标题所说:
[DataContract(Name = "{0}Item")] //This will format properly
public class GenericItem<T>
{
[DataMember(Name = "The{0}")] //This will NOT format properly
public T TheItem { get; set; }
}
[CollectionDataContract(Name = "{0}Items")] //This will format properly
public SpecialCollection<T> : Collection<T> { }
[ServiceContract(Name = "{0}Service")] //This will NOT format properly
public interface IGenericService<T>
{
[OperationContract(Name = "Get{0}")] //This will NOT format properly
GenericItem<T> Get<T>();
}
那么,你有它...什么有效,什么无效...但问题是...为什么?显然.NET能够创建一个具体的类型和格式使用DataContract 和CollectionDataContract 并说明类型时的名称(即GenericItem<Foo> 或SpecialCollection<Foo>。那么为什么不让DataMember 也能够格式化呢?
ServiceContract/OperationContract 我可以理解为上面留下的方式(排序),但我不明白的是,当你给它一个具体的类型时,操作仍然无法正常工作:
[ServiceContract(Name = "FooService")]
public interface FooService : IGenericService<Foo> { }
public interface IGenericService<T>
{
[OperationContract(Name = "Get{0}")] //This will NOT format properly
T Get<T>();
}
再次,为什么?显然,我在这里声明了一个具体的 Foo 类型,这意味着 IGenericService 是一个 IGenericService
更新:
我只记得为什么我对无法使用通用格式的 ServiceContract 感到不安......当我实现服务时,我正在给它一个具体的类型......
//See! I gave it a concrete type to go by!
[ServiceBehavior(...)]
public class MyService : IGenericService<Foo> { ... }
我为此创建了一个Microsoft Connect 请求。如果您希望将此功能用于其他属性,请对其进行投票。 http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2327048-enable-generics-for-datamemberattribute-serviceco
【问题讨论】:
-
+1 只是因为我不知道你在任何情况下都能做到这一点。如果我不得不猜测,我会说 MS 在定义泛型类型并且需要区分的两个地方实现了它。例如,您不能让
List<string>和List<DateTime>的数据合同名称都为“List”,因此他们必须这样做才能使 WCF 在内部工作。浏览 Reflector 或任何工具中的代码,看看他们在哪里实现它会很有趣。读取DataContract和DataCollectionContract属性的任何类都必须不同于读取其他属性的类。
标签: wcf generics wsdl xsd operationcontract