C# WCF是否保留(生成的)数据契约的缓存以及如何清除它 Windows通信基础(WCF)是否保存了(生成的)数据集中的缓存以及如何清除?为什么我在我的情况下会这样:

C# WCF是否保留(生成的)数据契约的缓存以及如何清除它 Windows通信基础(WCF)是否保存了(生成的)数据集中的缓存以及如何清除?为什么我在我的情况下会这样:,c#,wcf,C#,Wcf,我已经创建了一个类“BaseClass”的子类“ChildClass”。“基类”是服务器应用程序和UI应用程序之间的Datacontract定义的一部分。 现在我想让服务器通过接口将“ChildClass”发送到UI,但将其伪装成“基类”。我为我的“儿童班”尝试了一些不同的装饰(受以下示例启发:)。 在UI端反序列化对象时,我总是会出错,但错误确实引用了我在“ChildClass”上的一些装饰,所以有些东西起作用了 然后我尝试了另一种方法:我只是通过添加以下内容将“ChildClass”添加到接

我已经创建了一个类“BaseClass”的子类“ChildClass”。“基类”是服务器应用程序和UI应用程序之间的Datacontract定义的一部分。 现在我想让服务器通过接口将“ChildClass”发送到UI,但将其伪装成“基类”。我为我的“儿童班”尝试了一些不同的装饰(受以下示例启发:)。 在UI端反序列化对象时,我总是会出错,但错误确实引用了我在“ChildClass”上的一些装饰,所以有些东西起作用了

然后我尝试了另一种方法:我只是通过添加以下内容将“ChildClass”添加到接口的DataContract中: [KnownType(typeof(ChildClass))] 这起作用了。(请注意,该代码是我没有编写的更大代码库的一部分。它包括datacontracts的特定于客户端的定义,并且生成了一些代码)。 但是,客户端也可能会发回此对象,从而导致错误:

{"格式化程序在尝试反序列化消息时引发异常:尝试反序列化参数BaseClassUISpecificDefinition时出错。InnerException消息为“Element”UINamespace:BaseClassUISpecificDefinition“包含映射到名称“”的类型的数据。反序列化程序不知道该类型映射到这个名称。如果您正在使用DATACONTROPTIONALZER或将对应于“ChildClass”的类型添加到已知类型的列表中,请考虑使用DATACONTROPTRIFER。例如,使用NoNyType属性或将其添加到传递给序列化程序的已知类型的列表中。“请参阅InEnExcor以获得更多的细节。”}

除了这个错误之外,使用这种客户端将返回ChildClass的方法可能会导致其他逻辑问题,因为服务器在ChildClass中使用了唯一的标识符,而客户端不应该返回这些标识符,所以我想恢复到我原来的解决方案

但是,无论我重建了多少,删除了bin/obj文件,生成了多少代码,签出了以前的提交,当客户机收到一个对象时,我总是会收到相同的错误:

{”格式化程序在尝试反序列化消息时引发异常:尝试反序列化参数UINamespace:UIServiceContract时出错。InnerException消息为“Element”UINamespace:UIServiceContract“包含来自映射到名称“”的类型的数据。反序列化器不知道映射到此名称“”的任何类型如果您正在使用DATACONTROPTIONALZER或将对应于“ChildClass”的类型添加到已知类型的列表中(例如,使用NoNyType属性属性或将其添加到传递给序列化程序的已知类型的列表中),请参阅InEnExcor以获得更多的细节。 然后,为了验证我的假设,即某处存在缓存,我在buildserver上运行了完全相同的代码(在干净的环境中生成干净的构建),实际上,我在那里遇到了不同的错误。 我预计当客户端尝试反序列化对象时会发生此错误,并且:

套接字连接已中止。这可能是由于处理消息时出错、远程主机超过接收超时或underlyinig网络资源问题造成的。本地套接字超时为“00:01:00”


那么,是否存在缓存/代码生成?它存储在哪里以及如何清除?

描述如何在WCF中声明子类的一篇优秀文章如下:

我的类的正确属性必须是:

在BaseClass.cs中:

namespace ServerBaseClassNamespace
{
    //
    // Summary:
    //     Coordinates system in all the Machine Control and Subsystem software.
    [DataContract]
    [KnownType(typeof(BaseClass))]
    public class BaseClass
    {
    }
}
在ChildClass.cs中:

namespace ServerChildClassNamespace
{
    using ServerBaseClassNamespace;

    [System.Runtime.Serialization.KnownType(typeof(ChildClass))]
    [System.Runtime.Serialization.DataContract(Name = "BaseClass", Namespace = "ServerBaseClassNamespace")] /* We disguise ChildClass as an BaseClass.
    It is sent over the interfaces as an BaseClass. The interface contracts: [ServiceContract], must define [ServiceKnownType(typeof(ChildClass))] to use this.
    See: https://www.c-sharpcorner.com/uploadfile/c1de77/data-contract-and-known-type-in-wcf/
    for explanation. */
    public class ChildClass : BaseClass
    {
    }
}
在IExampleContract.cs中:

namespace UiService
{
    using System;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using ServerChildClassNamespace;

    [ServiceContract(Namespace = "UiService",SessionMode = SessionMode.Required)]
    [ServiceKnownType(typeof(ChildClass))] // On the server side this is an ChildClass, but the client will receive it as a BaseClass.
    public interface IExampleContract
    {
        [OperationContract]
        BaseClass ExampleMethod(BaseClass ExampleArgument);
    }
}
在UIServiceImplementation.cs中:

namespace UiService
{
    using System;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using ServerChildClassNamespace;

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    [ServiceKnownType(typeof(ChildClass))] // On the server side this is an ChildClass, but the client will receive it as a BaseClass.
    public sealed class UIServiceImplementation : IExampleContract
    {
    BaseClass ExampleMethod(BaseClass ExampleArgument);
    }
}

您是否添加了
[KnownType(typeof(DerivedClassNameHere))]
到基类?这是典型的解决方案。我知道磁盘上不存在缓存。不,我没有。我在stackoverflow上读到其他人关于这样做的建议,但这对我来说毫无意义,所以我将其作为输入错误丢弃。但现在我有点明白了逻辑。这是将childclass伪装为基类所必需的。我将在mon上尝试day.OK,实际上我犯了一个错误。我在测试客户端应用程序,但在进行更改时,我没有在本地更新服务器应用程序。这解释了为什么我在测试时没有看到任何更改。