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

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

问题描述

我的客户机/服务器应用程序使用WCF进行通信,这一直是很大的。然而,当前的体系结构的一个缺点是,我必须用已知类型的配置对于某些传输类型。我使用了一个内部​​的Pub / Sub机制,这个要求是不可避免的。

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?

修改请理解,我想在配置动态设置已知类型在运行时,而不是声明或使用属性源$ C ​​$ C。

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.

推荐答案

添加<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.serviceknowntypeattribute.aspx"><$c$c>[ServiceKnownType]你的 [的ServiceContract] 接口:

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

然后创建一个类名为 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天全站免登陆