c# - 无法使我的服务调用 ServiceKnownType 帮助程序方法

标签 c# wcf serviceknowntype

我在运行 ServiceKnownType 属性中指定的帮助程序方法时遇到问题。为了简单起见,我有两个程序集:一个包含我的服务接口(interface)和数据协定接口(interface),另一个包含我的服务实现和具体数据协定。

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

MyService.Interface.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类(Class)和具体List<IMyData>泛型类,因为服务定义使用接口(interface)类型而不是具体类型。

简单的答案是用以下内容装饰 IMyService:

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

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

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

[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>程序集中没有完全限定的类型名称。

最佳答案

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

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

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

// 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 类都扩展了一个公共(public)基类。在我的例子中,可以重新设计以从具有 DataContract 属性的程序集中加载所有类型,这将导致相同的集合。

关于c# - 无法使我的服务调用 ServiceKnownType 帮助程序方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14987998/

相关文章:

c# - 如何取消选择按钮 `Select all` 而不是所有项目?

json - WCF Json GET服务: Check that the sender and receiver's EndpointAddresses agree

c# - 使用 svcutil.exe 测试此服务

jquery - 在不同项目中通过 AJAX 调用使用 jQuery 中的 WCF 服务(跨域)

wcf - 了解WCF中的ServiceKnownType

c# - 检测注册的 HttpListeners

c# - 是否可以通过 WMI 获取 Cluster -> Availability Group Instance Node Map?

c# - 是否可以区分具有相同完全限定名称的类型?