neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.36k stars 267 forks source link

TypeLoadException with Service Fabric Remoting deserialization #147

Open xperiandri opened 5 years ago

xperiandri commented 5 years ago

System.TypeLoadException: 'Type 'Utf8Json.Formatters.Sfsync_Serialization_JsonRemotingResponseBodyFormatter3' from assembly 'Utf8Json.Resolvers.DynamicObjectResolverAllowPrivateFalseExcludeNullTrueNameMutateCamelCase, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is attempting to implement an inaccessible interface.'

Value that I read in Immediate Window

new System.IO.StreamReader(buffer).ReadToEnd()
"{}"
using Microsoft.ServiceFabric.Services.Remoting.V2;
using Microsoft.ServiceFabric.Services.Remoting.V2.Messaging;
using System;
using System.Collections.Generic;
using Utf8Json;

namespace Serialization
{
    internal class ServiceRemotingResponseJsonMessageBodySerializer : IServiceRemotingResponseMessageBodySerializer
    {
        public IOutgoingMessageBody Serialize(IServiceRemotingResponseMessageBody responseMessageBody)
        {
            if (responseMessageBody == null) return null;

            var bytes = JsonSerializer.Serialize(responseMessageBody);
            var segment = new ArraySegment<byte>(bytes);
            var segments = new List<ArraySegment<byte>> { segment };
            return new OutgoingMessageBody(segments);
        }

        public IServiceRemotingResponseMessageBody Deserialize(IIncomingMessageBody messageBody)
        {
            using (var buffer = messageBody.GetReceivedBuffer())
            {
                // Exception here
                var body = JsonSerializer.Deserialize<JsonRemotingResponseBody>(buffer);
                return body;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using Microsoft.ServiceFabric.Services.Remoting.V2;
using Microsoft.ServiceFabric.Services.Remoting.V2.Messaging;
using Utf8Json;

namespace Serialization
{
    internal class ServiceRemotingRequestJsonMessageBodySerializer : IServiceRemotingRequestMessageBodySerializer
    {
        public IOutgoingMessageBody Serialize(IServiceRemotingRequestMessageBody requestMessageBody)
        {
            if (requestMessageBody == null) return null;

            var bytes = JsonSerializer.Serialize(requestMessageBody);
            var segment = new ArraySegment<byte>(bytes);
            var segments = new List<ArraySegment<byte>> { segment };
            return new OutgoingMessageBody(segments);
        }

        public IServiceRemotingRequestMessageBody Deserialize(IIncomingMessageBody messageBody)
        {
            using (var buffer = messageBody.GetReceivedBuffer())
            {
                var body = JsonSerializer.Deserialize<JsonRemotingRequestBody>(buffer);
                return body;
            }
        }
    }
}
using Microsoft.ServiceFabric.Services.Remoting.V2;
using System;
using System.Collections.Generic;

namespace Serialization
{
    public class ServiceRemotingJsonSerializationProvider : IServiceRemotingMessageSerializationProvider
    {
        public IServiceRemotingMessageBodyFactory CreateMessageBodyFactory()
         => new JsonMessageFactory();

        public IServiceRemotingRequestMessageBodySerializer CreateRequestMessageSerializer(
            Type serviceInterfaceType,
            IEnumerable<Type> requestWrappedType,
            IEnumerable<Type> requestBodyTypes = null)
         => new ServiceRemotingRequestJsonMessageBodySerializer();

        public IServiceRemotingResponseMessageBodySerializer CreateResponseMessageSerializer(
            Type serviceInterfaceType,
            IEnumerable<Type> responseWrappedType,
            IEnumerable<Type> responseBodyTypes = null)
         => new ServiceRemotingResponseJsonMessageBodySerializer();
    }
}
using Microsoft.ServiceFabric.Services.Remoting.V2;
using System;
using System.Collections.Generic;
using System.Text;

// https://github.com/Microsoft/service-fabric-services-and-actors-dotnet/issues/147#issuecomment-452237798
namespace Serialization
{
    internal sealed class JsonMessageFactory : IServiceRemotingMessageBodyFactory
    {
        public IServiceRemotingRequestMessageBody CreateRequest(
            string interfaceName, string methodName,
            int numberOfParameters, object wrappedRequestObject)
         => new JsonRemotingRequestBody();

        public IServiceRemotingResponseMessageBody CreateResponse(
            string interfaceName, string methodName, object wrappedResponseObject)
         => new JsonRemotingResponseBody();
    }

    internal sealed class JsonRemotingRequestBody : IServiceRemotingRequestMessageBody
    {
        public readonly Dictionary<string, object> Parameters = new Dictionary<string, object>();

        public void SetParameter(int position, string parameName, object parameter)
         => Parameters[parameName] = parameter;

        public object GetParameter(int position, string parameName, Type paramType)
         => Parameters[parameName];
    }

    internal sealed class JsonRemotingResponseBody : IServiceRemotingResponseMessageBody
    {
        public object Value;

        public void Set(object response)
         => Value = response;

        public object Get(Type paramType)
         => Value;
    }
}
using Utf8Json.FSharp;
using Utf8Json.ImmutableCollection;
using Utf8Json.Resolvers;

namespace Serialization
{
    public static class Configuration
    {
        public static void SetupUtf8Json()
        {
            CompositeResolver.RegisterAndSetAsDefault(
                ImmutableCollectionResolver.Instance,
                FSharpResolver.Instance,
                StandardResolver.ExcludeNullCamelCase);
        }
    }
}

Why could this happen and how to fix?

8ggmaker commented 3 years ago

@xperiandri do you solve this now ?