在运行时提供ServiceKnownType?

Sam*_*Sam 3 c# wcf servicecontract

我在合同中使用了超过100个ServiceKnownType的WCF接口,如下所示:

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

有没有办法在运行时提供这些ServiceKnownTypes?
将所有这些ServiceKnownTypes添加到源代码中不仅容易出错且繁琐,它使我的程序集以我不喜欢的方式绑定在一起(我希望能够将这些类型提取到子组件中以将它们分离,但不能,因为服务需要列出所有已知的类型).

Wil*_*mpt 11

就在这里.

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

指定的方法必须是static和public,并且返回类型为IEnumerable.

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

另见http://msdn.microsoft.com/en-us/library/system.servicemodel.serviceknowntypeattribute.aspx