2

我在运行我的 ServiceKnownType 属性中指定的辅助方法时遇到问题。为简单起见,我有两个程序集:一个带有我的服务接口和数据合约的接口,另一个带有我的服务实现和具体的数据合约。

这是我的服务及其实现的简化/精简版本。

我的服务接口.dll

// IMyService.cs
[ServiceContract]
IMyService
{
    [OperationContract]
    IList<IMyData> GetData();
}

// IMyData.cs
public interface IMyData
{
    int Foo { get; set; }
}

MyService.dll(参考 MyService.Interface.dll)

// MyService.svc.cs
public class MyService: IMyService
{
    public IList<IMyData> GetData()
    {
        // Instantiate List<IMyData>, add a new MyData to the list
        // return the List<IMyData>.
    }
}

// MyData.cs
[DataContract]
public class MyData : IMyData
{
    [DataMember]
    public int Foo { get; set; }
}

问题的根源在于,要序列化 ​​的结果GetData(),必须告知服务具体MyData类和具体List<IMyData>泛型类,因为服务定义使用接口类型而不是具体类型。

简单的答案是用以下方式装饰 IMyService:

[ServiceKnownType(typeof(MyData))]
[ServiceKnownType(typeof(List<IMyData>))]

但是,MyData在 MyService.Interface.dll 中未引用的程序集中定义(并且不能由于循环引用。)

我的下一个想法是使用ServiceKnownTypeMyService 本身的“辅助方法”形式:

[ServiceKnownType("GetDataContracts", MyService)]
public class MyService: IMyService
{
    public static IEnumerable<Type> GetDataContracts(ICustomeAttributeProvider provider)
    {
        // create a List<Type>, add MyData to it, and return it.
    }
    //...
}

GetDataContracts据我所知,除了从未调用过之外,它应该可以工作。我尝试将它移动到一个单独的静态类中(与 MyService 平行并嵌套在其中),但在任何情况下我都无法获得一个断点来停在那里。

编辑:我还想说通过 web.config 添加已知类型也不起作用,因为我无法以这种方式添加泛型类型。您只能通过 web.config 添加简单、具体的类型:

<knownType type="TypeName, Assembly" />

我的混凝土List<IMyData>在程序集中没有完全限定的类型名称。

4

1 回答 1

2

固定的。答案是使用辅助方法形式将 ServiceKnownType 添加到服务接口,而不是服务实现,并添加一个反映我需要的类型的辅助类,而不是通过引用代码中的具体类型来添加它们。(回想一下,我不能这样做,因为我不能添加对该程序集的引用。)

[ServiceContract]
[ServiceKnownType("GetDataContractTypes", typeof(MyServiceHelper))]
public interface IMyService
{ ... }

我在 Nightingale.Interface 中添加了一个新的 MyServiceHelper 类,但它不是公开的,所以我不担心从我只想成为“纯”接口的程序集中不必要地公开一个类。

// Not public outside of this assembly.
static class MyServiceHelper
{
    public static IEnumerable<Type> GetDataContractTypes(ICustomAttributeProvider paramIgnored)
    {
        // Get the assembly with the concrete DataContracts.
        Assembly ass = Assembly.Load("MyService");  // MyService.dll
        // Get all of the types in the MyService assembly.
        var assTypes = ass.GetTypes();
        // Create a blank list of Types.
        IList<Type> types = new List<Type>();
        // Get the base class for all MyService data contracts
        Type myDataContractBase = ass.GetType("MyDataContractBase", true, true);
        // Add MyService's data contract Types.
        types.Add(assTypes.Where(t => t.IsSubclassOf(myDataContractBase)));
        // Add all the MyService data contracts to the service's known types so they can be serialized.
        return types;
    }
}

这个特殊的解决方案对我有用,因为我所有的 DataContract 类都扩展了一个公共基类。在我的情况下,可以对其进行重新设计以从具有 DataContract 属性的程序集中加载所有类型,这将导致相同的集合。

于 2013-02-20T21:07:11.670 回答