Пример #1
0
        public SerializerConfig()
        {
            config = new ContainerConfig();

            config.Register <IActivator>()
            .ImplementedBy(c => new ActivatorConfig().Create())
            .As <Singleton>();

            var innerScope = config.CreateScope();

            converterContainer = new ConverterContainer(innerScope);

            Match <byte>().With <Core.ByteConverter>();
            Match <sbyte>().With <SByteConverter>();
            Match <short>().With <ShortConverter>();
            Match <ushort>().With <UShortConverter>();
            Match <int>().With <Int32Converter>();
            Match <uint>().With <UInt32Converter>();
            Match <long>().With <Int64Converter>();
            Match <ulong>().With <UInt64Converter>();
            Match <string>().With <StringConverter>();
            Match <bool>().With <BoolConverter>();
            Match <float>().With <SingleConverter>();
            Match <double>().With <DoubleConverter>();
            Match <DateTime>().With <DateTimeConverter>();
            Match <Array>().With <ArrayConverter>();
            Match <IList>().With <IListConverter>();
            Match <ICollection>().With <CollectionConverter>();
            Match <IEnumerable>().With <IEnumerableConverter>();
            Match <Guid>().With <GuidConverter>();

            config             = innerScope;
            converterContainer = new ConverterContainer(config, converterContainer);
        }
        public async Task Run()
        {
            using (var rootContainer = config.Create())
            {
                var configuration = rootContainer.Resolve <IConfiguration>();

                var tcpListener = new TcpListener(new IPEndPoint(IPAddress.Parse(configuration.Host), configuration.Port));
                tcpListener.Start();

                while (true)
                {
                    var tcpClient = await tcpListener.AcceptTcpClientAsync().ConfigureAwait(false);

                    tcpClient.NoDelay           = true;
                    tcpClient.ReceiveTimeout    = 0;
                    tcpClient.SendTimeout       = 0;
                    tcpClient.ReceiveBufferSize = 256;
                    tcpClient.SendBufferSize    = 256;

                    var scope = config.CreateScope();

                    scope.Register <IContainerConfig>()
                    .ImplementedBy(c => scope)
                    .As <Singleton>();

                    var container = scope.Create();

                    scope.Register <IContainer>()
                    .ImplementedBy(c => c)
                    .As <Singleton>();

                    scope.Register <TcpClient>()
                    .ImplementedBy(c => tcpClient)
                    .As <Singleton>();

                    var connection = container.Resolve <IConnection>();

                    scope.Register <IConnection>()
                    .ImplementedBy(c => connection)
                    .As <Singleton>();
                }
            }
        }
        public Task RaiseCommand <TData>(IPayload <TData> payload)
        {
            using (var scope = config.CreateScope())
            {
                scope.Register(payload.Data.GetType())
                .ImplementedBy(payload.Data);

                using (var container = scope.Create())
                {
                    var command = container.Resolve(payload.CommandType);

                    RaiseSyncCommands(container, command, payload);

                    var asyncHandlers = commandHolder.GetAsyncHandlers(payload);

                    return(asyncHandlers.Count == 0
                                                ? Task.CompletedTask
                                                : RaiseAsyncCommands(container, asyncHandlers, command, payload));
                }
            }
        }
Пример #4
0
        public ISerializer Create()
        {
            if (disposed)
            {
                throw new ObjectDisposedException(nameof(SerializerConfig));
            }

            var scope     = config.CreateScope();
            var converter = new ConverterContainer(scope, converterContainer);

            using (var container = scope.Create())
            {
                var activator  = container.Resolve <IActivator>();
                var serializer = new Core.Serializer(activator, converter);

                scope.Register <ISerializer>()
                .ImplementedBy(c => serializer)
                .As <Singleton>();

                return(serializer);
            }
        }