Data contract Known types and set of interfaces inheriting from each other

I am developing (rewriting to WCF) a file that analyzes a web service that accepts string[] and returns ISection[] , but it’s actually 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 declaredTypes on the server and on 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:

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

or

The connected connection was closed: the connection was unexpectedly closed.

I cannot decorate interfaces with KnownTypeAttribute because contract projects do not refer to format projects and refer to design gaps. So I want to use config.

+8
c # exception-handling wcf datacontractserializer known-types
source share
3 answers

Trying to get this to work:

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

but it seems that the server and client should reference Project.Format.A.dll to make it work (the method does not return null)

0
source share

Take a look at the code below

 [ServiceContract] [ServiceKnownType(typeof(SummarySectionFormatA))] [ServiceKnownType(typeof(DataSectionFormatA))] public interface IService {} public class Service : IService {} 
+2
source share

I believe that you need to slightly change your implementation ... look at question and see if it helps.

+1
source share

All Articles