I suggest you ...

Enable Generics for DataMemberAttribute, ServiceContractAttribute, and OperationContractAttribute

Currently DataContractAttribute and CollectionDataContractAttribute both allow the use of generics and formatting the Name to account for the generics proplery. Example:

[DataContract(Name = "{0}Item")]
public class GenericItem<T> { }

[CollectionDataContract(Name = "{0}Items")]
public SpecialCollection<T> : Collection<T> { }

However, this does not work for the DataMemberAttribute, ServiceContractAttribute, OperationContractAttribute. I understand the need to have a concrete type so that a service can be exposed properly and is compatible, but even if you have a concrete type with these 3 attributes, it still does not work... the problem is that the code that generates the naming does not account for Generics.

In the System.Runtime.Serialization.DataContract we have the following:

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);
}
}
...
}

So, why can't the other attributes also take into account generics in their naming and generate proper names? This would allow us to do:

//The IGenericService is concrete now!
[ServiceContract(Name = "FooService")]
public interface FooService : IGenericService<Foo> { }

public interface IGenericService<T>
{
[OperationContract(Name = "Get{0}"]
T Get<T>();
}

25 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Anonymous shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    1 comment

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • leanord suyileanord suyi commented  ·   ·  Flag as inappropriate

        This sounds like a great idea... it would allow me to create multiple WCF services that are exactly alike without having to copy/paste everything.

      Feedback and Knowledge Base