Provides factory methods for creating and starting endpoint instances.
예제 #1
0
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="endpoint"></param>
 /// <param name="bus"></param>
 public AsyncHandle(Endpoint endpoint, IBus bus)
     : this(endpoint
     , bus
     , endpoint.Resolve<ILoggerFactory>()
     , endpoint.Resolve<ISerializer>())
 {
 }
예제 #2
0
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddLogging(loggingBuilder => loggingBuilder.AddConsole());

        var builder = new ContainerBuilder();

        builder.Populate(services);
        builder.RegisterInstance(new MyService());

        builder.Register(c => endpoint)
        .As <IEndpointInstance>()
        .SingleInstance();

        var container = builder.Build();

        var endpointConfiguration = new EndpointConfiguration("Sample.Core");

        endpointConfiguration.UseTransport <LearningTransport>();
        endpointConfiguration.UseContainer <AutofacBuilder>(
            customizations: customizations =>
        {
            customizations.ExistingLifetimeScope(container);
        });

        endpoint = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();

        return(new AutofacServiceProvider(container));
    }
예제 #3
0
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLogging(loggingBuilder => loggingBuilder.AddConsole());
        services.AddSingleton(sp => endpoint);
        services.AddSingleton <MyService>();

        var endpointConfiguration = new EndpointConfiguration("Sample.Core");

        endpointConfiguration.UseTransport <LearningTransport>();
        endpointConfiguration.UseContainer <ServicesBuilder>(
            customizations: customizations =>
        {
            customizations.ExistingServices(services);
        });

        endpoint = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
    }
        public static void AddNServiceBus(this IServiceCollection services, string endpointName)
        {
            var endpointConfiguration = new EndpointConfiguration(endpointName);

            endpointConfiguration.EnableInstallers();

            var transport = endpointConfiguration.UseTransport <RabbitMQTransport>().ConnectionString("host=localhost");

            transport.UseConventionalRoutingTopology();

            endpointConfiguration.UseContainer <ServicesBuilder>(
                customizations: customizations => { customizations.ExistingServices(services); });

            var routing = transport.Routing();

            DoCustomRouting(routing);

            var endpoint = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();

            services.AddSingleton(sp => endpoint);
        }
예제 #5
0
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="endpoint"></param>
 /// <param name="bus"></param>
 /// <param name="factory"></param>
 /// <param name="serializer"></param>
 public AsyncHandle(Endpoint endpoint, IBus bus, ILoggerFactory factory, ISerializer serializer)
 {
     this._bus = bus;
     this._log = factory.Create(typeof(AsyncHandle));
     this._serializer = serializer;
 }
예제 #6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(setup =>
            {
                setup.ReturnHttpNotAcceptable = true;

                setup.OutputFormatters.Add(new XmlSerializerOutputFormatter());
                setup.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());

                setup.Filters.Add(new ProducesResponseTypeAttribute(StatusCodes.Status200OK));
                setup.Filters.Add(new ProducesResponseTypeAttribute(StatusCodes.Status404NotFound));
                setup.Filters.Add(new ProducesResponseTypeAttribute(StatusCodes.Status422UnprocessableEntity));
                setup.Filters.Add(new ProducesResponseTypeAttribute(StatusCodes.Status400BadRequest));
                setup.Filters.Add(new ProducesResponseTypeAttribute(StatusCodes.Status401Unauthorized));
                setup.Filters.Add(new ProducesResponseTypeAttribute(StatusCodes.Status500InternalServerError));

                setup.Filters.Add <ValidationFilter>();
            })
            .AddFluentValidation(configuration =>
                                 configuration.RegisterValidatorsFromAssemblyContaining <Startup>());

            services.AddControllers();


            services.AddVersionedApiExplorer(setupAction => { setupAction.GroupNameFormat = "'v'VV"; });

            services.AddApiVersioning(setupAction =>
            {
                setupAction.AssumeDefaultVersionWhenUnspecified = true;
                setupAction.ReportApiVersions = true;
                setupAction.DefaultApiVersion = new ApiVersion(1, 0);
                //setupAction.ApiVersionReader = new HeaderApiVersionReader("api-version");
            });

            var apiVersionDescriptionProvider =
                services.BuildServiceProvider().GetService <IApiVersionDescriptionProvider>();

            services.AddSwaggerGen(setupAction =>
            {
                foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
                {
                    setupAction.SwaggerDoc($"ReadingIsGood.API{description.GroupName}",
                                           new OpenApiInfo
                    {
                        Title       = "ReadingIsGood.API",
                        Version     = description.ApiVersion.ToString(),
                        Description = "ReadingIsGood.API Description",
                        Contact     = new OpenApiContact
                        {
                            Email = "*****@*****.**",
                            Name  = "Kudret Kurt"
                        },
                        License = new OpenApiLicense
                        {
                            Name = "MIT License",
                            Url  = new Uri("https://opensource.org/licenses/MIT")
                        }
                    });
                }

                setupAction.DocInclusionPredicate((documentName, apiDescription) =>
                {
                    var actionApiVersionModel =
                        apiDescription.ActionDescriptor.GetApiVersionModel(ApiVersionMapping.Explicit |
                                                                           ApiVersionMapping.Implicit);

                    if (actionApiVersionModel == null)
                    {
                        return(true);
                    }

                    if (actionApiVersionModel.DeclaredApiVersions.Any())
                    {
                        return(actionApiVersionModel.DeclaredApiVersions.Any(v =>
                                                                             $"ReadingIsGood.APIv{v.ToString()}" == documentName));
                    }

                    return(actionApiVersionModel.ImplementedApiVersions.Any(v =>
                                                                            $"ReadingIsGood.APIv{v.ToString()}" == documentName));
                });

                setupAction.AddSecurityDefinition("basic", new OpenApiSecurityScheme
                {
                    Name        = "Authorization",
                    Type        = SecuritySchemeType.Http,
                    Scheme      = "basic",
                    In          = ParameterLocation.Header,
                    Description = "Basic Authorization header using the Bearer scheme."
                });

                setupAction.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "basic"
                            }
                        },
                        new string[] { }
                    }
                });
            });

            services.AddSwaggerGen(setupAction =>
            {
                setupAction.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title   = "ReadingIsGood.API",
                    Version = "v1"
                });

                var xmlCommentsFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlCommentsPath = Path.Combine(AppContext.BaseDirectory, xmlCommentsFile);
                setupAction.IncludeXmlComments(xmlCommentsPath);
            });

            services.AddAuthentication("BasicAuthentication")
            .AddScheme <AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);

            services.AddSingleton(setup =>
            {
                var endpointName =
                    ApplicationConfiguration.Instance.GetValue <string>("ReadingIsGoodApi:CallbacksSenderEndpointName");
                var errorQueueName =
                    ApplicationConfiguration.Instance.GetValue <string>(
                        "ReadingIsGoodApi:CallbacksSenderErrorQueueName");

                var endpointConfiguration = new EndpointConfiguration(endpointName);


                endpointConfiguration.EnableInstallers();
                endpointConfiguration.SendFailedMessagesTo(errorQueueName);
                endpointConfiguration.EnableCallbacks();
                endpointConfiguration.MakeInstanceUniquelyAddressable(Environment.MachineName);

                endpointConfiguration.DisableFeature <AutoSubscribe>();

                endpointConfiguration.Recoverability()
                .Immediate(
                    immediate => { immediate.NumberOfRetries(0); })
                .Delayed(
                    delayed => { delayed.NumberOfRetries(0); });


                endpointConfiguration.UseTransport <RabbitMQTransport>()
                .ConnectionString(
                    ApplicationConfiguration.Instance.GetValue <string>("ServiceBus:TransportConnectionString"))
                .UseConventionalRoutingTopology();


                return(Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult());
            });

            services.AddSingleton(setup => LoggingMechanism.CreateLogger("ReadingIsGoodAPI", enableConsoleLog: true));
        }