示例#1
0
        public static async Task <JsonRpcServer> From(JsonRpcServerOptions options, IServiceProvider outerServiceProvider, CancellationToken cancellationToken)
        {
            var server = Create(options, outerServiceProvider);
            await server.Initialize(cancellationToken);

            return(server);
        }
        public static Task <JsonRpcServer> From(Action <JsonRpcServerOptions> optionsAction)
        {
            var options = new JsonRpcServerOptions();

            optionsAction(options);
            return(From(options, CancellationToken.None));
        }
示例#3
0
        public static Task <JsonRpcServer> From(Action <JsonRpcServerOptions> optionsAction, IServiceProvider outerServiceProvider, CancellationToken cancellationToken)
        {
            var options = new JsonRpcServerOptions();

            optionsAction(options);
            return(From(options, outerServiceProvider, cancellationToken));
        }
示例#4
0
        public static JsonRpcServer Create(Action <JsonRpcServerOptions> optionsAction, IServiceProvider outerServiceProvider)
        {
            var options = new JsonRpcServerOptions();

            optionsAction(options);
            return(Create(options, outerServiceProvider));
        }
示例#5
0
        public static Task <IJsonRpcServer> From(Action <JsonRpcServerOptions> optionsAction)
        {
            var options = new JsonRpcServerOptions();

            optionsAction(options);
            return(From(options));
        }
        internal JsonRpcServer(JsonRpcServerOptions options) : base(options)
        {
            var outputHandler = new OutputHandler(
                options.Output,
                options.Serializer,
                options.LoggerFactory.CreateLogger <OutputHandler>()
                );
            var services = options.Services;

            services.AddLogging();
            var receiver   = options.Receiver;
            var serializer = options.Serializer;

            _disposable = options.CompositeDisposable;

            services.AddSingleton <IOutputHandler>(outputHandler);
            services.AddSingleton(serializer);
            services.AddSingleton(serializer);
            services.AddSingleton(options.RequestProcessIdentifier);
            services.AddSingleton(receiver);
            services.AddSingleton(options.LoggerFactory);

            services.AddJsonRpcMediatR(options.Assemblies);
            services.AddSingleton <IJsonRpcServer>(this);
            services.AddSingleton <IRequestRouter <IHandlerDescriptor>, RequestRouter>();
            services.AddSingleton <IResponseRouter, ResponseRouter>();
            _collection = new HandlerCollection();
            services.AddSingleton(_collection);
            services.AddSingleton <IHandlersManager>(_ => _.GetRequiredService <HandlerCollection>());

            EnsureAllHandlersAreRegistered();

            var serviceProvider = services.BuildServiceProvider();

            _disposable.Add(serviceProvider);
            _serviceProvider = serviceProvider;
            HandlersManager  = _collection;

            var requestRouter = _serviceProvider.GetRequiredService <IRequestRouter <IHandlerDescriptor> >();
            var router        = ResponseRouter = _serviceProvider.GetRequiredService <IResponseRouter>();

            _connection = new Connection(
                options.Input,
                outputHandler,
                options.Receiver,
                options.RequestProcessIdentifier,
                requestRouter,
                router,
                options.LoggerFactory,
                options.OnUnhandledException ?? (e => { }),
                options.CreateResponseException,
                options.MaximumRequestTimeout,
                options.SupportsContentModified,
                options.Concurrency
                );
            _disposable.Add(_connection);
            _collection.Add(_serviceProvider.GetRequiredService <IEnumerable <IJsonRpcHandler> >().ToArray());
            options.AddLinks(_collection);
        }
        public static async Task <JsonRpcServer> From(JsonRpcServerOptions options, CancellationToken cancellationToken)
        {
            var server = new JsonRpcServer(options);

            await server.Initialize(cancellationToken);

            return(server);
        }
示例#8
0
        public static async Task <IJsonRpcServer> From(JsonRpcServerOptions options)
        {
            var server = new JsonRpcServer(
                options.Input,
                options.Output,
                options.Reciever,
                options.RequestProcessIdentifier,
                options.LoggerFactory,
                options.Serializer,
                options.Services,
                options.HandlerTypes.Select(x => x.Assembly)
                .Distinct().Concat(options.HandlerAssemblies)
                );

            await server.Initialize();

            return(server);
        }
示例#9
0
 public static Task <JsonRpcServer> From(JsonRpcServerOptions options) => From(options, null, CancellationToken.None);
示例#10
0
 public static JsonRpcServerOptions WithHandler <T>(this JsonRpcServerOptions options, T handler)
     where T : IJsonRpcHandler
 {
     options.Services.AddSingleton <IJsonRpcHandler>(handler);
     return(options);
 }
示例#11
0
 public static JsonRpcServer Create(JsonRpcServerOptions options, IServiceProvider outerServiceProvider) =>
 CreateContainer(options, outerServiceProvider).Resolve <JsonRpcServer>();
示例#12
0
 internal static IContainer CreateContainer(JsonRpcServerOptions options, IServiceProvider outerServiceProvider) =>
 JsonRpcServerContainer.Create(outerServiceProvider)
 .AddJsonRpcServerInternals(options);
示例#13
0
 public static JsonRpcServer Create(JsonRpcServerOptions options) => Create(options, null);
 public static Task <JsonRpcServer> From(JsonRpcServerOptions options)
 {
     return(From(options, CancellationToken.None));
 }
示例#15
0
 public static JsonRpcServerOptions WithHandlersFrom(this JsonRpcServerOptions options, TypeInfo typeInfo)
 {
     options.HandlerTypes.Add(typeInfo.AsType());
     return(options);
 }
示例#16
0
 public static JsonRpcServerOptions WithHandler <T>(this JsonRpcServerOptions options)
     where T : class, IJsonRpcHandler
 {
     options.Services.AddSingleton <IJsonRpcHandler, T>();
     return(options);
 }
示例#17
0
 public static JsonRpcServerOptions WithHandlersFrom(this JsonRpcServerOptions options, Assembly assembly)
 {
     options.HandlerAssemblies.Add(assembly);
     return(options);
 }
示例#18
0
 public static Task <JsonRpcServer> From(JsonRpcServerOptions options, CancellationToken cancellationToken) => From(options, null, cancellationToken);
示例#19
0
 public static JsonRpcServerOptions WithServices(this JsonRpcServerOptions options, Action <IServiceCollection> servicesAction)
 {
     servicesAction(options.Services);
     return(options);
 }
示例#20
0
 public static JsonRpcServerOptions WithSerializer(this JsonRpcServerOptions options, ISerializer serializer)
 {
     options.Serializer = serializer;
     return(options);
 }
示例#21
0
 /// <summary>
 /// Sets the scheduler use during writing output
 /// </summary>
 /// <param name="options"></param>
 /// <param name="outputScheduler"></param>
 /// <returns></returns>
 public static JsonRpcServerOptions WithOutputScheduler(this JsonRpcServerOptions options, IScheduler outputScheduler)
 {
     options.OutputScheduler = outputScheduler;
     return(options);
 }
示例#22
0
 /// <summary>
 /// Sets the default scheduler to be used when scheduling other tasks
 /// </summary>
 /// <param name="options"></param>
 /// <param name="defaultScheduler"></param>
 /// <returns></returns>
 public static JsonRpcServerOptions WithDefaultScheduler(this JsonRpcServerOptions options, IScheduler defaultScheduler)
 {
     options.DefaultScheduler = defaultScheduler;
     return(options);
 }
示例#23
0
 /// <summary>
 /// Sets the scheduler used during reading input
 /// </summary>
 /// <param name="options"></param>
 /// <param name="inputScheduler"></param>
 /// <returns></returns>
 public static JsonRpcServerOptions WithInputScheduler(this JsonRpcServerOptions options, IScheduler inputScheduler)
 {
     options.InputScheduler = inputScheduler;
     return(options);
 }
示例#24
0
 public static JsonRpcServerOptions WithAssemblyAttributeScanning(this JsonRpcServerOptions options, bool value)
 {
     options.UseAssemblyAttributeScanning = value;
     return(options);
 }
示例#25
0
 public static Task <JsonRpcServer> From(JsonRpcServerOptions options, IServiceProvider outerServiceProvider) => From(options, outerServiceProvider, CancellationToken.None);
示例#26
0
        internal static IContainer AddJsonRpcServerInternals(this IContainer container, JsonRpcServerOptions options)
        {
            if (options.Serializer == null)
            {
                throw new ArgumentException("Serializer is missing!", nameof(options));
            }

            if (options.Receiver == null)
            {
                throw new ArgumentException("Receiver is missing!", nameof(options));
            }

            if (options.RequestProcessIdentifier == null)
            {
                throw new ArgumentException("RequestProcessIdentifier is missing!", nameof(options));
            }

            container = container.AddJsonRpcServerCore(options);

            container.RegisterInstance(options.Serializer ?? new JsonRpcSerializer());
            container.RegisterInstance(options.Receiver);
            container.RegisterInstance(options.RequestProcessIdentifier);
            container.RegisterInstance(options.OnUnhandledException ?? (e => { }));

            container.RegisterMany <RequestRouter>(Reuse.Singleton);
            container.RegisterMany <HandlerCollection>(
                nonPublicServiceTypes: true,
                serviceTypeCondition: type => typeof(IHandlersManager) == type || type == typeof(HandlerCollection),
                reuse: Reuse.Singleton
                );
            container.RegisterInitializer <IHandlersManager>(
                (manager, context) => {
                var descriptions = context.Resolve <IJsonRpcHandlerCollection>();
                descriptions.Populate(context, manager);
            }
                );

            container.RegisterInstance <IOptionsFactory <JsonRpcServerOptions> >(new ValueOptionsFactory <JsonRpcServerOptions>(options));
            container.RegisterMany <JsonRpcServer>(serviceTypeCondition: type => type == typeof(IJsonRpcServer) || type == typeof(JsonRpcServer), reuse: Reuse.Singleton);

            return(container);
        }
示例#27
0
 public static JsonRpcServerOptions WithRequestProcessIdentifier(this JsonRpcServerOptions options, IRequestProcessIdentifier requestProcessIdentifier)
 {
     options.RequestProcessIdentifier = requestProcessIdentifier;
     return(options);
 }
示例#28
0
 public static JsonRpcServerOptions WithReciever(this JsonRpcServerOptions options, IReceiver receiver)
 {
     options.Receiver = receiver;
     return(options);
 }
        internal static IContainer AddJsonRpcServerInternals(this IContainer container, JsonRpcServerOptions options)
        {
            if (options.Serializer == null)
            {
                throw new ArgumentException("Serializer is missing!", nameof(options));
            }

            if (options.RequestProcessIdentifier == null)
            {
                throw new ArgumentException("RequestProcessIdentifier is missing!", nameof(options));
            }

            container = container.AddJsonRpcServerCore(options);

            if (options.UseAssemblyAttributeScanning)
            {
                container.RegisterInstanceMany(new AssemblyAttributeHandlerTypeDescriptorProvider(options.Assemblies), nonPublicServiceTypes: true);
            }
            else
            {
                container.RegisterInstanceMany(new AssemblyScanningHandlerTypeDescriptorProvider(options.Assemblies), nonPublicServiceTypes: true);
            }


            container.RegisterInstance(options.Serializer);
            if (options.Receiver == null)
            {
                container.Register <IReceiver, Receiver>(Reuse.Singleton);
            }
            else
            {
                container.RegisterInstance(options.Receiver);
            }
            container.RegisterMany <AlwaysOutputFilter>(Reuse.Singleton, nonPublicServiceTypes: true);

            container.RegisterInstance(options.RequestProcessIdentifier);
            container.RegisterInstance(options.OnUnhandledException ?? (_ => { }));

            container.RegisterMany <RequestRouter>(Reuse.Singleton);
            container.RegisterMany <HandlerCollection>(
                nonPublicServiceTypes: true,
                serviceTypeCondition: type => typeof(IHandlersManager) == type || type == typeof(HandlerCollection),
                reuse: Reuse.Singleton
                );
            container.RegisterInitializer <IHandlersManager>(
                (manager, context) => {
                var descriptions = context.Resolve <IJsonRpcHandlerCollection>();
                descriptions.Populate(context, manager);
            }
                );

            container.Register <IJsonRpcServerFacade, DefaultJsonRpcServerFacade>(reuse: Reuse.Singleton);
            container.RegisterInstance <IOptionsFactory <JsonRpcServerOptions> >(new ValueOptionsFactory <JsonRpcServerOptions>(options));
            container.RegisterMany <JsonRpcServer>(
                serviceTypeCondition: type => type == typeof(IJsonRpcServer) || type == typeof(JsonRpcServer),
                reuse: Reuse.Singleton,
                setup: Setup.With(condition: req => req.IsResolutionRoot || req.Container.Resolve <IInsanceHasStarted>().Started)
                );

            return(container);
        }
示例#30
0
 public static JsonRpcServerOptions WithLoggerFactory(this JsonRpcServerOptions options, ILoggerFactory loggerFactory)
 {
     options.LoggerFactory = loggerFactory;
     return(options);
 }