【问题标题】:ServiceRemoting V2_1 still throwing serialization exception when working with interface return types使用接口返回类型时,ServiceRemoting V2_1 仍然抛出序列化异常
【发布时间】:2019-05-01 07:39:33
【问题描述】:

在过去的几天里,我尝试为我的应用程序配备服务远程 IPC 堆栈。我最初实现了 V2 版本,但由于这篇文章 (https://github.com/Azure/service-fabric-issues/issues/735) 才注意到不支持返回接口。

所以,我刚刚切换到 V2_1。

但是,我仍然面临这个问题:

One or more errors occurred. (Type 'MooMed.Core.DataTypes.Session.SessionContext' with data contract name 'SessionContext:http://schemas.datacontract.org/2004/07/MooMed.Core.DataTypes.Session' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.)'

这是在端点上调用的服务方法:

    [CanBeNull]
    public Task<ISessionContext> GetSessionContext(int accountId):

涉及的类/接口如下所示:

public interface ISessionContext
{
    Account Account { get; set; }
}

[DataContract]
public class SessionContext : ISessionContext
{
    [DataMember]
    public Account Account { get; set; }
}

另外,正如我所提到的,我刚刚将远程处理版本从 V2 更改为 V2_1,因此将其添加到包含 GetSessionContext 方法的 Service 类中:

[assembly: FabricTransportServiceRemotingProvider(RemotingListenerVersion = RemotingListenerVersion.V2_1, RemotingClientVersion = RemotingClientVersion.V2_1)]

因此,根据文档 (https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-remoting#use-the-remoting-v2-interface-compatible-stack),我现在应该完全有能力让一切正常运行。

我发现自己做的与教程不同的唯一一件事是我声明端点侦听器的方式。我通过FabricTransportServiceRemotingListener 这样做:

    public static ServiceReplicaListener CreateTypedListener([NotNull] IService service)
    {
        return new ServiceReplicaListener(context => new FabricTransportServiceRemotingListener(context, service), 
            $"I{service.GetType().Name}");
    }

但是,我不明白这可能是罪魁祸首,因为请求正确进入,而且我认为声明端点不一定会干扰响应的序列化方式。

那么,我在这里做错了什么?

【问题讨论】:

  • 首先在更改版本时确保重新编译所有代码。编译中的依赖项无法识别库版本更改。所以我通常会删除项目的 bin 文件夹,以确保一切都可以编译。序列化的错误消息不是很好。所以我通常注释掉所有可疑的代码并让序列化运行无异常。然后开始取消注释代码以查找代码的哪一部分实际上失败了。您可能会遇到不止一个问题,因此请确保您正在寻找正确的问题。
  • @jdweng 问题当然出在使用接口的问题上。如果我返回一个具体的实现,一切都会奏效。我按照手册告诉我的做了。
  • 我认为你应该在 GITHUB 提问。

标签: c# serialization azure-service-fabric


【解决方案1】:

DataContractSerializer 无法序列化接口。 (见how to mark an interface as DataContract in WCF)。

但是,您可以编写自己的自定义序列化程序来实现此目的。从您在底部发布的那篇文章中可以找到有关如何执行此操作的说明。您还可以在此处查看我实现 protobuf-net 的示例: https://github.com/mikeruhl/Frenetik.Fabric.Remoting.Protobuf

我的示例不应该在生产中使用。这是一项正在进行中的工作,目前为它编写的测试为零。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多