如何以编程方式配置 WCF 已知类型? [英] How do you configure WCF known types programmatically?

查看:24
本文介绍了如何以编程方式配置 WCF 已知类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户端/服务器应用程序使用 WCF 进行通信,这很棒.然而,当前架构的一个缺点是我必须对某些传输类型使用已知类型配置.我使用的是内部发布/订阅机制,这个要求是不可避免的.

My client/server application is using WCF for communication, which has been great. However one shortcoming of the current architecture is that I must use known type configuration for certain transmitted types. I'm using an in-house Pub/Sub mechanism and this requirement is unavoidable.

问题是很容易忘记添加已知类型,如果你这样做了,WCF 会默默地失败,几乎不知道出了什么问题.

The problem is that it's easy to forget to add the known type, and if you do, WCF fails silently with few clues as to what's going wrong.

在我的应用程序中,我知道将要发送的类型集.我想以编程方式执行配置,而不是通过 App.config 文件以声明方式执行,该文件当前包含如下内容:

In my application, I know the set of types that are going to be sent. I would like to perform the configuration programmatically, rather than declaratively through the App.config file which currently contains something like this:

<system.runtime.serialization>
  <dataContractSerializer>
    <declaredTypes>
      <add type="MyProject.MyParent, MyProjectAssembly">
        <knownType type="MyProject.MyChild1, MyProjectAssembly"/>
        <knownType type="MyProject.MyChild2, MyProjectAssembly"/>
        <knownType type="MyProject.MyChild3, MyProjectAssembly"/>
        <knownType type="MyProject.MyChild4, MyProjectAssembly"/>
        <knownType type="MyProject.MyChild5, MyProjectAssembly"/>
      </add>
    </declaredTypes>
  </dataContractSerializer>
</system.runtime.serialization>

相反,我想做这样的事情:

Instead, I'd like to do something like this:

foreach (Type type in _transmittedTypes)
{
    // How would I write this method?
    AddKnownType(typeof(MyParent), type);
}

有人可以解释一下我该怎么做吗?

Can someone please explain how I might do this?

编辑请理解,我试图在运行时动态设置已知类型,而不是在配置中声明性地设置或在源代码中使用属性.

EDIT Please understand that I'm trying to set the known types dynamically at run time rather than declaratively in config or using attributes in the source code.

这基本上是一个关于 WCF API 的问题,而不是一个样式问题.

This is basically a question about the WCF API, not a style question.

编辑 2 此 MSDN 页面页面状态:

您还可以向 ReadOnlyCollection 添加类型,通过 DataContractSerializer 的KnownTypes 属性访问.

You can also add types to the ReadOnlyCollection, accessed through the KnownTypes property of the DataContractSerializer.

不幸的是,这就是它所说的全部内容,鉴于KnownTypes 是一个只读属性,并且该属性的值是一个ReadOnlyCollection,这并没有多大意义.

Unfortunately that's all it says and it doesn't make terribly much sense given that KnownTypes is a readonly property, and the property's value is a ReadOnlyCollection.

推荐答案

添加 [ServiceKnownType] 到您的 [ServiceContract] 接口:

[ServiceKnownType("GetKnownTypes", typeof(KnownTypesProvider))]

然后创建一个名为KnownTypesProvider的类:

then create a class called KnownTypesProvider:

internal static class KnownTypesProvider
{
    public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
    {
         // collect and pass back the list of known types
    }
}

然后你可以传回任何你需要的类型.

and then you can pass back whatever types you need.

这篇关于如何以编程方式配置 WCF 已知类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆