gpt4 book ai didi

c# - 如何为同一主机上的同一契约(Contract)同时支持 DataContractSerializer 和 XMLSerializer?

转载 作者:太空狗 更新时间:2023-10-29 23:42:16 24 4
gpt4 key购买 nike

在我们的生产环境中,我们的 WCF 服务使用 XMLSerializer 序列化。为此,我们的服务接口(interface)具有 [XMLSerializerFormat] 属性。现在,我们需要更改为 DataContractSerializer,但我们必须与现有客户端保持兼容。因此,我们必须使用两个序列化器公开每个服务。

我们有一个约束:我们不想重新定义每个合约接口(interface)两次,我们有 50 个服务合约接口(interface),我们不想拥有

IIncidentServiceXml 
IIncidentServiceDCS
IEmployeeServiceXml
IEmployeeServiceDCS
IContractServiceXml
IContractServiceDCS

我们怎样才能做到这一点?


更多信息

这是对我们迄今为止尝试过的方法的描述,但我愿意尝试完全不同的方法:

我们试图通过我们自己的 ServiceHostFactory 类中的代码创建所有端点。基本上我们创建每个端点两次。问题是在运行时,WCF 提示该服务有两个具有相同联系人名称但具有不同 ContractDescription 实例的终结点。该消息说我们应该使用不同的契约(Contract)名称或重用相同的 ContractDescription 实例。

其他尝试:

我们还尝试通过为每个 ContractDescription 实例使用不同的命名空间来做到这一点。这样我们将保持相同的契约(Contract)接口(interface) (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)

最佳答案

简短的回答是,你不能,正是因为你的错误消息所说的原因,你不能有太多具有相同名称的端点,而这实际上是你试图做的。我认为你将不得不完全按照你所说的去做你不想做的事。

This might be your only option

The problem is that to specify a service is to use the XmlSerializer you need to declare the [XmlSerializerFormat] attribute on the service or the contract. Well since we want to use the same for both endpoints we can’t place it there, so we are left with placing it on the contract. However, when it boils down to it, both endpoints are using the same service and vicariously the same contract right?

Well, it doesn’t have to be so. You could have a contract A derive from contract B, then have the service implement contract A such that everything in both contracts is part of the service. For this example though, contract B will be our standard contract, and contract A will be an interface that just defines the [XmlSerializerFormat] attribute.

但我不能向您保证,您现有的客户端代码无需更改即可使用。

关于c# - 如何为同一主机上的同一契约(Contract)同时支持 DataContractSerializer 和 XMLSerializer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3025762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com