数据协定已知类型和一组相互继承的接口

aba*_*hev 8 c# wcf exception-handling datacontractserializer known-types

我开发(重写到WCF)文件解析Web服务接受string[]和返回ISection[]但实际上这是一组嵌套接口:

namespace Project.Contracts // Project.Contracts.dll
{
    public interface ISection { }

    public interface ISummarySection : ISection { }

    public interface IDataSection : ISection { }
}
Run Code Online (Sandbox Code Playgroud)

和班级:

namespace Project.Format.A // Project.Format.A.dll
{
    [DataContract]
    public class SummarySectionFormatA : ISummarySection { }

    [DataContract]
    public class DataSectionFormatA : IDataSection { }
}
Run Code Online (Sandbox Code Playgroud)

服务接口及其实现:

[ServiceContract]
public interface IService // Project.Contracts.dll
{
    ISection[] Parse(string format, string[] data);
} 

[ServiceKnownType(typeof(SummarySectionFormatA))] // tried this also
[ServiceKnownType(typeof(DataSectionFormatA))]
public class Service : IService // Project.Service.dll
{
    public ISection[] Parse(string format, string[] data)
    {
        return Factory.Create(format).Parse(data);
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试declaredTypes在服务器和客户端上进行配置:

<system.runtime.serialization>
  <dataContractSerializer>
    <declaredTypes>
      <add type="Project.Contracts.ISumarySection, Project.Contracts">
        <knownType type="Project.Format.A.SummarySectionFormatA, Project.Format.A" />
      </add>
      <add type="Project.Contracts.IDataSection, Project.Contracts">
        <knownType type="Project.Format.A.DataSectionFormatA, Project.Format.A" />
      </add>
    </declaredTypes>
  </dataContractSerializer>
</system.runtime.serialization>
Run Code Online (Sandbox Code Playgroud)

但仍然得到同样的错误:

"不要求输入数据合约名称为'DataSection:http://schemas.example.com/Parse'的'DataSectionFormatA'.

要么

底层连接已关闭:连接意外关闭.

我无法使用KnownTypeAttribute修饰接口,因为Contracts项目不引用Format项目,并且引用会破坏设计.这就是我想使用配置的原因.

aba*_*hev 0

试图让这个工作:

[KnownType("GetKnownType")]
public class Section
{
    static Type[] GetKnownType()
    {
        return new[]
        {
            Type.GetType("Project.Format.A.DataSectionFormatA, Project.Format.A")
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

但似乎服务器和客户端都必须引用Project.Format.A.dll 才能使其正常工作(方法不返回 null)