/// <summary> /// Configures a scoped host that serves gRPC endpoints of the specified <see cref="EndpointVisibility"/> using Kestrel. /// </summary> /// <param name="builder">The host builder to add a scoped host to.</param> /// <param name="visibility">The endpoint visibility to serve endpoints for.</param> /// <returns>The builder for continuation.</returns> public static IHostBuilder AddGrpcHost(this IHostBuilder builder, EndpointVisibility visibility) => builder .AddGrpcEndpointHealthCheck(visibility) .AddScopedHost(_ => _.ConfigureWebHost(grpcHost => { grpcHost.UseKestrel(); grpcHost.ConfigureServices(services => { services.AddKestrelConfigurationFor(visibility); services.AddGrpc(); services.AddGrpcReflection(); }); grpcHost.Configure(app => { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapDiscoveredGrpcServicesOf(visibility); endpoints.MapGrpcReflectionService(); endpoints.MapGrpcService <HealthService>(); }); }); }));
/// <inheritdoc/> public void Start(EndpointVisibility type, EndpointConfiguration configuration, IEnumerable <Service> services) { try { var keepAliveTime = new grpc.ChannelOption("grpc.keepalive_time", 1000); var keepAliveTimeout = new grpc.ChannelOption("grpc.keepalive_timeout_ms", 500); var keepAliveWithoutCalls = new grpc.ChannelOption("grpc.keepalive_permit_without_calls", 1); _server = new grpc::Server(new[] { keepAliveTime, keepAliveTimeout, keepAliveWithoutCalls }) { Ports = { new grpc.ServerPort("0.0.0.0", configuration.Port, grpc::SslServerCredentials.Insecure) } }; _server .Ports .ForEach(_ => _logger.Information($"Starting {type} host on {_.Host}" + (_.Port > 0 ? $" for port {_.Port}" : string.Empty))); services.ForEach(_ => _server.Services.Add(_.ServerDefinition)); _server.Start(); } catch (Exception ex) { _logger.Error(ex, $"Couldn't start {type} host"); } }
public static void AddKestrelConfigurationFor(this IServiceCollection services, EndpointVisibility visibility) { services.AddSingleton <IConfigureOptions <KestrelServerOptions> >(_ => new KestrelConfiguration( _.GetRequiredService <IOptions <EndpointsConfiguration> >(), visibility)); }
/// <inheritdoc /> public void MapServicesForVisibility(IEndpointRouteBuilder endpoints, EndpointVisibility visibility) { foreach (var service in _services.Where(_ => _.Visibility == visibility)) { _logger.MappingDiscoveredGrpcService(service.ImplementationType, visibility); MapDynamicGrpcService(endpoints, service.ImplementationType); } }
/// <summary> /// Initializes a new instance of the <see cref="Client"/> class. /// </summary> /// <param name="visibility">The <see cref="EndpointVisibility"/> of the hosted <see cref="Endpoint"/>.</param> /// <param name="type"><see cref="Type"/> of <see cref="ClientBase">client</see>.</param> /// <param name="serviceDescriptor"><see cref="ServiceDescriptor"/> for the service the <see cref="Client"/> is for.</param> public Client(EndpointVisibility visibility, Type type, ServiceDescriptor serviceDescriptor) { ThrowIfTypeDoesNotImplementClientBase(type); Visibility = visibility; Type = type; ServiceDescriptor = serviceDescriptor; }
IEndpoint GetEndpointFor(EndpointVisibility type) { if (_endpoints.ContainsKey(type)) { return(_endpoints[type]); } var endpoint = _container.Get <IEndpoint>(); _endpoints[type] = endpoint; return(endpoint); }
internal static partial void PreparingEndpoint(ILogger logger, EndpointVisibility visibilityType, int port);
public KestrelConfiguration(IOptions <EndpointsConfiguration> configuration, EndpointVisibility visibility) { _configuration = configuration.Value.GetConfigurationFor(visibility); }
/// <summary> /// Initializes a new instance of <see cref="EndpointInfo"/> /// </summary> /// <param name="visibility"><see cref="EndpointVisibility">Type</see> of endpoint</param> /// <param name="configuration"><see cref="EndpointConfiguration">Configuration</see> for the endoint</param> public EndpointInfo(EndpointVisibility visibility, EndpointConfiguration configuration) { Visibility = visibility; Configuration = configuration; }
public void TestNodeCollapse(EndpointVisibility visibility, bool nonCollapsedNonConnectedVisible, bool nonCollapsedConnectedVisible, bool collapsedNonConnectedVisible, bool collapsedConnectedVisible) { NodeOutputViewModel nodeAOutput = new NodeOutputViewModel(); NodeViewModel nodeA = new NodeViewModel { Outputs = { nodeAOutput } }; NodeInputViewModel nodeBInput = new NodeInputViewModel { Visibility = visibility }; NodeInputViewModel nodeBInput2 = new NodeInputViewModel { Visibility = visibility }; NodeOutputViewModel nodeBOutput = new NodeOutputViewModel { Visibility = visibility }; NodeOutputViewModel nodeBOutput2 = new NodeOutputViewModel { Visibility = visibility }; NodeViewModel nodeB = new NodeViewModel { Inputs = { nodeBInput, nodeBInput2 }, Outputs = { nodeBOutput, nodeBOutput2 } }; NodeInputViewModel nodeCInput = new NodeInputViewModel(); NodeViewModel nodeC = new NodeViewModel { Inputs = { nodeCInput } }; NetworkViewModel network = new NetworkViewModel { Nodes = { nodeA, nodeB, nodeC } }; network.Connections.Add(network.ConnectionFactory(nodeBInput, nodeAOutput)); network.Connections.Add(network.ConnectionFactory(nodeCInput, nodeBOutput)); var expectedInputSeq = Enumerable.Empty <Endpoint>(); var expectedOutputSeq = Enumerable.Empty <Endpoint>(); if (nonCollapsedConnectedVisible) { expectedInputSeq = expectedInputSeq.Concat(new[] { nodeBInput }); expectedOutputSeq = expectedOutputSeq.Concat(new[] { nodeBOutput }); } if (nonCollapsedNonConnectedVisible) { expectedInputSeq = expectedInputSeq.Concat(new[] { nodeBInput2 }); expectedOutputSeq = expectedOutputSeq.Concat(new[] { nodeBOutput2 }); } Assert.IsTrue(nodeB.VisibleInputs.SequenceEqual(expectedInputSeq)); Assert.IsTrue(nodeB.VisibleOutputs.SequenceEqual(expectedOutputSeq)); nodeB.IsCollapsed = true; expectedInputSeq = Enumerable.Empty <Endpoint>(); expectedOutputSeq = Enumerable.Empty <Endpoint>(); if (collapsedConnectedVisible) { expectedInputSeq = expectedInputSeq.Concat(new[] { nodeBInput }); expectedOutputSeq = expectedOutputSeq.Concat(new[] { nodeBOutput }); } if (collapsedNonConnectedVisible) { expectedInputSeq = expectedInputSeq.Concat(new[] { nodeBInput2 }); expectedOutputSeq = expectedOutputSeq.Concat(new[] { nodeBOutput2 }); } Assert.IsTrue(nodeB.VisibleInputs.SequenceEqual(expectedInputSeq)); Assert.IsTrue(nodeB.VisibleOutputs.SequenceEqual(expectedOutputSeq)); }
/// <summary> /// Initializes a new instance of the <see cref="ServiceDefinition"/> class. /// </summary> /// <param name="visibility">The visibility of the implemented service.</param> /// <param name="implementationType">The type that implements the service.</param> public ServiceDefinition(EndpointVisibility visibility, Type implementationType) { Visibility = visibility; ImplementationType = implementationType; }
internal static void CouldNotStartHost(this ILogger logger, Exception ex, EndpointVisibility endpointVisibility) => _couldNotStartHost(logger, endpointVisibility.ToString(), ex);
/// <summary> /// Initializes a new instance of the <see cref="ServiceAttribute"/> class. /// </summary> /// <param name="visibility">The visibility of the service with this attribute defined.</param> protected ServiceAttribute(EndpointVisibility visibility) { Visibility = visibility; }
/// <summary> /// Initializes a new instance of the <see cref="UnknownEndpointVisibility"/> class. /// </summary> /// <param name="visibility">The visibility that is unknown.</param> public UnknownEndpointVisibility(EndpointVisibility visibility) : base($"The {nameof(EndpointVisibility)} {visibility} is unknown") { }
internal static partial void AddingHealthCheckFor(this ILogger logger, EndpointVisibility visibility);
/// <summary> /// Maps discovered gRPC service implementations for the specified <see cref="EndpointVisibility"/>. /// </summary> /// <param name="endpoints">The endpoint route builder to map services to.</param> /// <param name="visibility">The endpoint visibility to map services for.</param> public static void MapDiscoveredGrpcServicesOf(this IEndpointRouteBuilder endpoints, EndpointVisibility visibility) => endpoints .ServiceProvider.GetRequiredService <ICanConfigureGrpcServicesForEndpoint>() .MapServicesForVisibility(endpoints, visibility);
internal static partial void EndpointDisabled(ILogger logger, EndpointVisibility visibilityType);
internal static partial void MappingDiscoveredGrpcService(this ILogger logger, Type implementation, EndpointVisibility visibility);
internal static void StartingHost(this ILogger logger, EndpointVisibility visibility, string host, int port) => _startingHost(logger, visibility.ToString(), host, port, null);
/// <summary> /// Initializes a new instance of the <see cref="HealthCheckConfiguration"/> class. /// </summary> /// <param name="visibility">The endpoint visibility to add a health check for.</param> /// <param name="getConfiguration">The factory to use to get the endpoint configuration.</param> /// <param name="logger">The logger to use for logging.</param> public HealthCheckConfiguration(EndpointVisibility visibility, Func<IOptions<EndpointsConfiguration>> getConfiguration, ILogger logger) { _visibility = visibility; _getConfiguration = getConfiguration; _logger = logger; }
/// <summary> /// Configures a <see cref="HealthCheckRegistration"/> that performs a gRPC <see cref="HealthCheckRequest"/> on the specified <see cref="EndpointVisibility"/>. /// </summary> /// <param name="builder">The host builder to configure.</param> /// <param name="visibility">The endpoint visibility to add a health check for.</param> /// <returns>The builder for continuation.</returns> public static IHostBuilder AddGrpcEndpointHealthCheck(this IHostBuilder builder, EndpointVisibility visibility) => builder.ConfigureServices(services => services.AddTransient <IConfigureOptions <HealthCheckServiceOptions> >(_ => new HealthCheckConfiguration( visibility, _.GetRequiredService <Func <IOptions <EndpointsConfiguration> > >(), _.GetRequiredService <ILogger <HealthCheckConfiguration> >())));
/// <summary> /// Initializes a new instance of the <see cref="EndpointHealthCheck"/> class. /// </summary> /// <param name="visibility">The endpoint visibility to attempt to connect to.</param> /// <param name="getConfiguration">The factory to use to resolve the endpoint configuration.</param> public EndpointHealthCheck(EndpointVisibility visibility, Func <IOptions <EndpointsConfiguration> > getConfiguration) { _visibility = visibility; _getConfiguration = getConfiguration; }