WCF Generics - Why do DataContract and CollectionDataContract allow formatting of naming, but not DataMember, OperationContract or ServiceContract?

Basically, as the title says:

[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>(); } 

So, I have this ... that works and doesn't work ... but the question is ... why? Obviously, .NET can create a specific type and format for the name by using DataContract and CollectionDataContract and specifying the type (i.e. GenericItem<Foo> or SpecialCollection<Foo> ). So why not be able to format the DataMember ?

ServiceContract/OperationContract I can figure out how it stayed above (sorta), but I don’t understand when you give it a specific type, the operations will not work properly anyway:

 [ServiceContract(Name = "FooService")] public interface FooService : IGenericService<Foo> { } public interface IGenericService<T> { [OperationContract(Name = "Get{0}")] //This will NOT format properly T Get<T>(); } 

Again, why ? Obviously, I am declaring a specific Foo type here, which means that IGenericService is an IGenericService <Foo> so you should not format the name OperationContract, since it KNOWS the type?


Update:

I just remembered why I was upset that I could not use the fully formatted ServiceContract ... when I have a service implementation that I give it a specific type ...

 //See! I gave it a concrete type to go by! [ServiceBehavior(...)] public class MyService : IGenericService<Foo> { ... } 

I created a Microsoft Connect request. Promote it if you want this feature to be used for other attributes. http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2327048-enable-generics-for-datamemberattribute-serviceco

+2
source share
2 answers

This seems to be the choice of MS implementation. In System.Runtime.Serialization.DataContract it creates a name by doing:

 private static XmlQualifiedName GetDCTypeStableName(Type type, DataContractAttribute dataContractAttribute) { string localName; if (dataContractAttribute.IsNameSetExplicit) { string str = dataContractAttribute.Name; if (str == null || str.Length == 0) { ... } else { if (type.IsGenericType && !type.IsGenericTypeDefinition) str = DataContract.ExpandGenericParameters(str, type); localName = DataContract.EncodeLocalName(str); } } 

Thus, it explicitly generates a common name. In the case of the ServiceContract product, which is processed in System.ServiceModel.Description.TypeLoader and System.ServiceModel.Description.NamingHelper , and does nothing with generic types (finally, I do not see).

So, I assume that since these contracts are based on different assemblies and namespaces, they may have been implemented by different teams to begin with.

+1
source

Read this: http://msdn.microsoft.com/en-us/library/ms731045.aspx#Y1254

In principle, it seems that the naming system was originally developed without the possibility of formatting names, but in the end the DataContract naming system annoyed enough people (because of the hash) that they added the ability to format names from them.

EDIT:

"However, there may be reasons to change this default name. One reason is to allow an existing type to process data that must match an existing data contract. For example, there is a type called Person, but a data contract implemented in an XML schema, requires the name to be a client.The contract can be satisfied by setting the property value for the Client.

The second reason is to allow the creation of names that are invalid as type names. For example, if a data contract requires a name that is not valid as a type name, set the property value for this forbidden name. For example, the string "$ value" is not allowed as a type name, but allowed as a value of the Name property. "

Source: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.name.aspx

My guess, again, is that no one is needed to change the default name of others (including OperationContract, ServiceContract, etc.).

0
source

All Articles