Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Contract Known Types and a set of interfaces inheriting each other

I develop (rewrite onto WCF) a file parsing web service accepting string[] and returning ISection[] but actually this is a set of nested interfaces:

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

    public interface ISummarySection : ISection { }

    public interface IDataSection : ISection { }
}

and classes:

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

    [DataContract]
    public class DataSectionFormatA : IDataSection { }
}

Service interface and its implementation:

[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);
    }
}

I tried to configure declaredTypes on both server and clients:

<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>

But still get the same error:

"Type 'DataSectionFormatA' with data contract name 'DataSection:http://schemas.example.com/Parse' is not expected.

or

The underlying connection was closed: The connection was closed unexpectedly.

I can't decorate interfaces with KnownTypeAttribute because Contracts projects doesn't reference Format projects, and referencing breaks the design. That's why I want to use config.

like image 489
abatishchev Avatar asked Jun 30 '12 08:06

abatishchev


2 Answers

Take a look at the code below

[ServiceContract]
[ServiceKnownType(typeof(SummarySectionFormatA))]
[ServiceKnownType(typeof(DataSectionFormatA))]
public interface IService {}

public class Service : IService {}
like image 63
Alex Kovanev Avatar answered Sep 27 '22 23:09

Alex Kovanev


I believe you should change your implementation a little... have a look at this question and see if it helps.

like image 29
Wali Avatar answered Sep 28 '22 00:09

Wali