本文介绍了在运行时提供ServiceKnownType吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在合同中有一个使用100多个ServiceKnownType的WCF接口,如下所示:

I've got a working WCF interface using more than 100 ServiceKnownType in the contract like this:

[ServiceKnownType(typeof(RowUser))]
[ServiceKnownType(typeof(RowRegion))]
[ServiceKnownType(typeof(RowDocument))]
[... loads more ...]
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IServiceBrowse : IDisposable
{
  [OperationContract]
  void Insert(Row satz);
}

在运行时是否可以提供这些ServiceKnownType类型?
在源代码中添加所有这些ServiceKnownType不仅容易出错,而且很繁琐,而且使我的程序集以我不喜欢的方式保持在一起(我希望能够将这些类型提取为子程序集以将它们解耦,但由于该服务需要列出所有已知类型,所以不能这样做.

Is there any way to provide these ServiceKnownTypes during runtime?
It is not only error-prone and tedious to add all these ServiceKnownTypes in the source, it keeps my assemblies tied together in a way I don't like (I'd like to be able to extract these types into subassemblies to decouple them, but can't since the Service needs to list all the known types).

推荐答案

是的.

ServiceKnownTypeAttribute允许您将方法名称指定为第一个参数,后跟一个包含实现该方法的System.Type的参数.

ServiceKnownTypeAttribute lets you specify a method name as the first parameter, followed by a parameter containing the System.Type implementing that method.

指定的方法必须同时是静态的和公共的,并且具有IEnumerable的返回类型.

The specified method must be both static and public, and have a return type of IEnumerable.

[ServiceKnownType("RegisterKnownTypes", typeof(Services))]
public class Services : IServices
{
    static public IEnumerable<Type> RegisterKnownTypes(ICustomAttributeProvider provider)
    {
    }
}

另请参见 http://msdn.microsoft.com/zh-cn/library/system.servicemodel.serviceknowntypeattribute.aspx

这篇关于在运行时提供ServiceKnownType吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:24