コード例 #1
0
        public void SerializationSettingsShouldOverrideHoconSettings()
        {
            var serializationSettings = new SerializationSetup(_ =>
                                                               ImmutableHashSet <SerializerDetails> .Empty
                                                               .Add(SerializerDetails.Create(
                                                                        "test",
                                                                        new TestSerializer(_),
                                                                        ImmutableHashSet <Type> .Empty.Add(typeof(ProgammaticDummy)))));

            var bootstrap = BootstrapSetup.Create().WithConfig(ConfigurationFactory.ParseString(@"
                akka{
                    actor{
                        serialize-messages = on
                        serializers {
                            test = ""Akka.Tests.Serialization.OverridenSerializer, Akka.Test""
                        }
                        serialization-bindings {
                            ""Akka.Tests.Serialization.ProgammaticDummy, Akka.Tests"" = test
                        }
                    }
                }").WithFallback(TestConfigs.DefaultConfig));

            var actorSystemSettings = ActorSystemSetup.Create(serializationSettings, bootstrap);

            var sys2       = ActorSystem.Create("override-test", actorSystemSettings);
            var serializer = sys2.Serialization.FindSerializerFor(new ProgammaticDummy());

            serializer.Should().BeOfType <TestSerializer>();

            sys2.Terminate().Wait();
        }
コード例 #2
0
ファイル: CustomSerializerSpec.cs プロジェクト: eaba/akka.net
        public void Custom_programmatic_SerializerWithStringManifest_should_work_with_base_class_binding()
        {
            var settings = SerializationSetup.Create(system =>
                                                     ImmutableHashSet <SerializerDetails> .Empty.Add(
                                                         new SerializerDetails(
                                                             alias: "custom",
                                                             serializer: new CustomManifestSerializer(system),
                                                             useFor: ImmutableHashSet <Type> .Empty.Add(typeof(MessageBase))))
                                                     );

            var setup = ActorSystemSetup.Create(settings);

            using (var system = ActorSystem.Create(nameof(CustomSerializerSpec), setup))
            {
                var firstMessage  = new FirstMessage("First message");
                var serialization = system.Serialization;
                var serializer    = (CustomManifestSerializer)serialization.FindSerializerFor(firstMessage);

                var serialized = serializer.ToBinary(firstMessage);
                var manifest   = serializer.Manifest(firstMessage);
                var deserializedFirstMessage = serializer.FromBinary(serialized, manifest);
                manifest.Should().Be(FirstMessage.Manifest);
                deserializedFirstMessage.Should().Be(firstMessage);

                var secondMessage = new SecondMessage("Second message");
                serialized = serializer.ToBinary(secondMessage);
                manifest   = serializer.Manifest(secondMessage);
                var deserializedSecondMessage = serializer.FromBinary(serialized, manifest);
                manifest.Should().Be(SecondMessage.Manifest);
                deserializedSecondMessage.Should().Be(secondMessage);
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            // setup declaring our custom serializer to override the default typeof(object) newtonsoft registration
            var serializationSetup = SerializationSetup.Create(system =>
                                                               ImmutableHashSet <SerializerDetails> .Empty.Add(
                                                                   SerializerDetails.Create("json-custom", new CustomSerializer(system),
                                                                                            ImmutableHashSet <Type> .Empty
                                                                                            // this should make all my messages use CustomSerializer
                                                                                            .Add(typeof(object))
                                                                                            // this is provided to show CustomSerializer gets called,
                                                                                            // i.e, everything appears to be configured correctly
                                                                                            .Add(typeof(World))
                                                                                            )
                                                                   )
                                                               );

            // we just want to force serialization so we don't need to set up remote
            // the goal of the programmatic setup is so apps don't need to register the serializer
            var hocon = ConfigurationFactory.ParseString(@"
akka {
    ""loglevel"": ""DEBUG"",
    ""stdout-loglevel"": ""DEBUG"",
    actor {
        ""serialize-messages"": ""on"",
        ""debug"": {
            ""receive"": ""on"",
            ""lifecycle"": ""on"",
            ""unhandled"": ""on"",
            ""event-stream"": ""on""
        }
    }
}
");
            // I'm not sure why 'serialize-messages' isn't triggering my serializer, not even for
            // my 'World' type. Inspecting system.Serialization._serializerMap reveals the following:
            // [object] = NewtonsoftJsonSerializer
            // [World]  = CustomSerializer
            // [byte[]] = ByteArraySerializer
            // * I thought it might have been dependent on the ordering of ActorSystemSetup.Create(params Setup[] setups)
            //   but I observe the same behavior regardless of the setup ordering.
            var bootstrapSetup = BootstrapSetup.Create().WithConfig(hocon);
            var systemSetup    = ActorSystemSetup.Create(serializationSetup, bootstrapSetup);
            var system         = ActorSystem.Create("system", systemSetup);

            // uncommenting this line and the custom serializer is invoked
            // really bizarre since there is a mapping for typeof(World) but w/out this next line it's not invoked on that type either.
            //system.Serialization.AddSerializationMap(typeof(object), new CustomSerializer(system.Serialization.System));

            var actor    = system.ActorOf(Props.Create(() => new Actor()), "actor");
            var response = actor.Ask <World>(new Hello("world!")).GetAwaiter().GetResult();

            system.Log.Info($"Response: {response.Message}");

            Thread.Sleep(TimeSpan.FromSeconds(20));

            system.Dispose();
        }