gpt4 book ai didi

WCF 数据合约解析器

转载 作者:行者123 更新时间:2023-12-02 02:14:18 25 4
gpt4 key购买 nike

我正在尝试遵循 http://kellabyte.com/2010/11/13/building-extensible-wcf-service-interfaces-with-datacontractresolver/ 中的指南创建并附加 DataContractSerializer。

我已经声明了序列化程序并实现了方法,然后使用以下代码将其附加到客户端和服务器:

public class ModuleDataContractResolver : DataContractResolver {
public override bool TryResolveType(Type type, Type declaredType,
DataContractResolver knownTypeResolver,
out System.Xml.XmlDictionaryString typeName,
out System.Xml.XmlDictionaryString typeNamespace) {
....// I return a true/false here
}

public override Type ResolveName(string typeName, string typeNamespace,
Type declaredType, DataContractResolver knownTypeResolver) {
....// I return a type here
}

-

        var endpoint = _svcHost.Description.Endpoints.FirstOrDefault()
ContractDescription cd = endpoint.Contract;
foreach (OperationDescription opdesc in cd.Operations) {
DataContractSerializerOperationBehavior serializerBehavior = opdesc.Behaviors.Find<DataContractSerializerOperationBehavior>();

if (serializerBehavior == null) {
serializerBehavior = new DataContractSerializerOperationBehavior(opdesc);
opdesc.Behaviors.Add(serializerBehavior);
}

serializerBehavior.DataContractResolver = new ModuleDataContractResolver();
}

尽管附加了解析器,但服务和客户端均未调用这两个方法,因此服务抛出异常。我错过了一步吗?

更新:我不完全相信这不是因为使用 MEF 返回这些类型。所讨论的类型是 MEF 类型,该类型由服务检测但仅作为接口(interface)公开给客户端,因此不会加载程序集。

想法是让服务加载 MEF 模块列表,然后通过此 WCF 服务将它们作为接口(interface)公开给客户端。

最佳答案

服务端:

foreach (OperationDescription operation in endpoint.Contract.Operations)
{
operation.Behaviors.Find<DataContractSerializerOperationBehavior>()
.DataContractResolver = new ModuleDataContractResolver();
}

客户端:

foreach (var operation in factory.Endpoint.Contract.Operations)
{
operation.Behaviors.Find<DataContractSerializerOperationBehavior>()
.DataContractResolver = new ModuleDataContractResolver();
}

关于WCF 数据合约解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11292357/

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