9

如何从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();
}

我离开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

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

类型“ServiceResult`1[IBarData]”无法添加到已知类型列表,因为
具有相同数据合约名称的另一种类型 'ServiceResult`1[IFooData]'
'http://schemas.datacontract.org/2004/07/ServiceResultOfanyType' 是
已经存在。

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

显然,IFooData它们IBarData不一样,实现它们的类也不一样。

那么为什么要ServiceResult<IFooData>ServiceResult<IBarData>两者都解决ServiceResultOfanyType呢?

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

4

1 回答 1

5

经过多次试验和错误,我终于得到了这个工作与最小的变化:

  • 我的服务操作现在返回ServiceResult<T>而不是IServiceResult<T>. 事实上,IServiceResult<T>现在已经完全没有了。
  • GetServiceKnownTypes()不再返回 的所有变体ServiceResult<T>。我只返回DataContract用作 s 的 s 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
    

我现在可以调用所有这些方法,并在ServiceContract.

于 2013-04-02T22:13:00.640 回答