WCF中具有接口类型params的通用返回类型

JMD*_*JMD 9 generics wcf serialization json interface

如何从OperationContracts我的WCF REST服务中返回泛型类型参数中的接口类型?更具体地说,它适用于一个操作,但是当我使用T作为接口的泛型添加第二个操作时,它不起作用.

我正在使用JSON作为请求和响应格式,为非WCF客户端提供解析所需数据的JSON响应.我没有使用SOAP,也没有使用服务生成的WSDL.

我的服务界面:

[ServiceContract]
[ServiceKnownType("GetServiceKnownTypes", typeof(ServiceKnownTypesHelper))]
public interface IMyService
{
    [WebGet(UriTemplate="count")]
    [OperationContract]
    IServiceResult<int> GetCount();

    [WebGet(UriTemplate="desc")]
    [OperationContract]
    IServiceResult<string> GetDescription();

    [WebGet(UriTemplate="foo")]
    [OperationContract]
    IServiceResult<IFooData> GetFooData();

    // Fails when I invoke either method if I uncomment this operation.
    //[WebGet(UriTemplate="bar")]
    //[OperationContract]
    //IServiceResult<IBarData> GetBarData();
}
Run Code Online (Sandbox Code Playgroud)

我离开GetCount()GetDescription()在示例中指出这两个通用结果工作正常,但显然它们是具体类型.甚至GetFooData()可以正常工作,直到我添加第二种方法,IServiceResult<T>T接口.

返回类型GetFooData()GetBarData()不同,也不是实现它们的具体类.

你可以想象我已经将实现简化为骨架,因为我不认为实现是问题的核心:

#region My service implementation
public class MyService : IMyService
{
    public IServiceResult<int> GetCount()
    {
        return new ServiceResult<int>(42);
    }
    public IServiceResult<string> GetDescription()
    {
        return new ServiceResult<string>("Muffins");
    }
    public IServiceResult<IFooData> GetFooData()
    {
        return new ServiceResult<IFooData>(new FooData() { Foo = 99 });
    }
    public IServiceResult<IBarData> GetBarData()
    {
        return new ServiceResult<IBarData>(new BarData() { Bar = "Elvis was here" });
    }
}
#endregion

#region ServiceKnownTypesHelper.GetServiceKnownTypes():
public static class ServiceKnownTypesHelper
{
    private static IList<Type> serviceKnownTypes = new List<Type>()
        {
            typeof(FooData),
            typeof(BarData),
            typeof(ServiceResult<int>),
            typeof(ServiceResult<string>),
            typeof(ServiceResult<IFooData>),
            typeof(ServiceResult<IBarData>),
        };

    public static IEnumerable<Type> GetServiceKnownTypes(ICustomAttributeProvider paramIgnored)
    {
        return serviceKnownTypes;
    }
}
#endregion

#region IServiceResult<T> and its concrete implementation:
public interface IServiceResult<T>
{
    IList<string> Errors { get; }
    T Value { get; set; }
}

[DataContract]
public class ServiceResult<T> : IServiceResult<T>
{
    public ServiceResult(T value)
    {
        this.Value = value;
    }

    private IList<string> errors = new List<string>();

    [DataMember]
    public IList<string> Errors
    {
        get
        {
            return this.errors;
        }
    }

    [DataMember]
    public T Value { get; set; }
}
#endregion

#region IFooData and its concrete implementation:
public interface IFooData
{
    int Foo { get; set; }
}

[DataContract]
public class FooData: IFooData
{
    [DataMember]
    public int Foo { get; set; }
}
#endregion

#region IBarData and its concrete implementation:
public interface IBarData
{
    string Bar { get; set; }
}

[DataContract]
public class BarData: IBarData
{
    [DataMember]
    public string Bar { get; set; }
}
#endregion
Run Code Online (Sandbox Code Playgroud)

GetBarData()从浏览器调用时出现错误消息:

Type 'ServiceResult`1[IBarData]' cannot be added to list of known types since
another type 'ServiceResult`1[IFooData]' with the same data contract name
'http://schemas.datacontract.org/2004/07/ServiceResultOfanyType' is
already present. 

错误消息的其余部分是关于碰撞集合类型红鲱鱼List<Test>Test[],这是不是这里的情况.

显然,IFooData并且IBarData不一样,也没有实现它们的类.

那么,为什么ServiceResult<IFooData>ServiceResult<IBarData>这两个决心ServiceResultOfanyType

我错过了什么,或者没有办法解决这个问题?

JMD*_*JMD 5

经过多次试验和错误,我终于在最小的变化下完成了这项工作:

  • 我的服务操作现在返回ServiceResult<T>而不是IServiceResult<T>.事实上,IServiceResult<T>现在完全消失了.
  • GetServiceKnownTypes()不再返回所有变体ServiceResult<T>.我只返回DataContract用作的T.

    #region My service interface
    [ServiceContract]
    [ServiceKnownType("GetServiceKnownTypes", typeof(ServiceKnownTypesHelper))]
    public interface IMyService
    {
        [WebGet(UriTemplate="count")]
        [OperationContract]
        ServiceResult<int> GetCount();
    
        [WebGet(UriTemplate="desc")]
        [OperationContract]
        ServiceResult<string> GetDescription();
    
        [WebGet(UriTemplate="foo")]
        [OperationContract]
        ServiceResult<IFooData> GetFooData();
    
        [WebGet(UriTemplate="bar")]
        [OperationContract]
        ServiceResult<IBarData> GetBarData();
    }
    #endregion
    #region My service implementation (minus bodies)
    public class MyService : IMyService
    {
        public ServiceResult<int> GetCount() {}
        public ServiceResult<string> GetDescription() {}
        public ServiceResult<IFooData> GetFooData() {}
        public ServiceResult<IBarData> GetBarData() {}
    }
    #endregion
    #region ServiceKnownTypes
    // My list of ServiceKnownTypes is now much shorter and simpler. I was feeding the service too much information
    public static class ServiceKnownTypesHelper
    {
        private static IList<Type> serviceKnownTypes = new List<Type>()
            {
                typeof(FooData),
                typeof(BarData),
                //typeof(ServiceResult<int>),
                //typeof(ServiceResult<string>),
                //typeof(ServiceResult<IFooData>),
                //typeof(ServiceResult<IBarData>),
            };
    
        // Remaining implementation is the same as before
    }
    #endregion
    #region ServiceResult<T> with no interface (it's not used or needed)
    [DataContract]
    public class ServiceResult<T> //: IServiceResult<T>
    {
        // implementation is the same as before
    }
    #endregion
    
    Run Code Online (Sandbox Code Playgroud)

我现在可以调用所有这些方法并获取其接口所引用的泛型类型列表ServiceContract.

  • 我发布了自己的答案,因为至少有一个人认为这个问题值得投票.因此,希望同样的人可能会发现答案有用. (3认同)