How to support both DataContractSerializer and XMLSerializer for the same contract on the same host?

In our production environment, our WCF services are serialized using XMLSerializer. To do this, our service interfaces have the [XMLSerializerFormat] attribute. Now we need to switch to the DataContractSerializer, but we must remain compatible with our existing customers. Therefore, we must expose each service using serializers.

We have one limitation: we do not want to redefine each contract interface twice, we have 50 contract service interfaces, and we do not want to have

IIncidentServiceXml 
IIncidentServiceDCS
IEmployeeServiceXml 
IEmployeeServiceDCS
IContractServiceXml 
IContractServiceDCS

How can we do this?


Additional Information

This is a description of what we have tried so far, but I am ready to try completely different approaches:

ServiceHostFactory. . , WCF , , ContractDescription. , ContractDescription.

:

, ContractDescription. , (IIncidentService), :

http://ourcompany/XML/IIncidentService
http://ourcompany/DCS/IIncidentService

, :

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.XmlSerializerOperationBehavior
contract: http://ourcompany.cs/XML:IUserServiceWCF ----> System.NullReferenceException: Object reference not set to an instance of an object.
   at System.ServiceModel.Description.XmlSerializerMessageContractExporter.ExportFaults(Object state)
   at System.ServiceModel.Description.MessageContractExporter.ExportMessageContract()
   at System.ServiceModel.Description.XmlSerializerOperationBehavior.System.ServiceModel.Description.IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext)
   at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
+5
3

: , , . , , , .

, XmlSerializer [XmlSerializerFormat] . , , , . , ?

, . , B, , . , A , [XmlSerializerFormat].

, .

+1

, [DataContract] [DataMember]. XmlSerializer DataContractSerializer . XmlSerializer 3.0 datacontract. DataContractSerializer [Serializable], .

, Data Contracts. , , ( ). XmlSerializer DataContractSerializer.

behavoir, [Serializable], [DataContract], .

[Serializable]
[DataContract]
public class Customer

{
    [DataMember]
    public int Age { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Number { get; set; }

    [DataMember]
    public string FullName { get; set; }

    [XmlIgnore]
    public int IgnoredNumber { get; set; }
}

XmlSerializer :

<?xml version="1.0" encoding="utf-16" ?> 
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Age>88</Age> 
  <Name>Bob</Name> 
  <Number>808</Number> 
  <FullName>Bob Jones</FullName> 
  </Customer>

DataContractSerializer :

  <?xml version="1.0" encoding="utf-8" ?> 
<Customer xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1">
  <Age>88</Age> 
  <FullName>Bob Jones</FullName> 
  <Name>Bob</Name> 
  <Number>808</Number> 
  </Customer>

, . , , .

+1

How about two services running the same contract?

how

class DcsService : Service
{}

[XmlSerializerFormat] 
class XmlService : Service
{}

class Service : IServiceContract
{}

Never worked with XmlSerializer, but we used this construct for other purposes.

0
source

All Articles