public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging(loggerFactory => loggerFactory.AddConsole()) .ConfigureServices((hostContext, services) => { RabbitMqConfigurations rabbitConfigurations = new RabbitMqConfigurations(); hostContext.Configuration.GetSection("RabbitMqConfigurations") .Bind(rabbitConfigurations); services.AddMediatR(Assembly.GetExecutingAssembly()); services.AddLogging(configure => configure.AddConsole()); services.AddMemoryCache(); services.AddSingleton <IConnectionFactory>(new ConnectionFactory { HostName = rabbitConfigurations.Hostname, Port = rabbitConfigurations.Port, UserName = rabbitConfigurations.Username, Password = rabbitConfigurations.Password }); services.AddSingleton <ICache <Stock>, Cache>(); services.AddSingleton <IApiRequester, ApiRequester>(); services.AddSingleton <IMessageQueue, MessageQueue>(); services.AddSingleton <ITextParser, CsvTextParser>(); services.AddSingleton <IRequestHandler <StockQuoteRequestModel, bool>, ProcessCommandStockRequestHandler>(); services.AddHostedService <Worker>(); });
private ConnectionFactory GenerateConnectionFactory(RabbitMqConfigurations configurations) { return(new ConnectionFactory() { HostName = configurations.HostName, Port = configurations.Port, UserName = configurations.UserName, Password = configurations.Password }); }
public static IBusControl Create(RabbitMqConfigurations configurations, Action <IRabbitMqBusFactoryConfigurator, IRabbitMqHost> action = null) { return(Bus.Factory.CreateUsingRabbitMq(rabbitMqBus => { var host = rabbitMqBus.Host(new Uri(configurations.Uri), rabbitMqHost => { rabbitMqHost.Username(configurations.Username); rabbitMqHost.Password(configurations.Password); }); action?.Invoke(rabbitMqBus, host); })); }
public object Post( [FromServices] RabbitMqConfigurations configurations, [FromBody] Content content) { lock (_counter) { _counter.Increment(); var factory = GenerateConnectionFactory(configurations); using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { DeclareQueue(channel); var body = SetBodyRequest(content); SendMessage(channel, body); } return(new { MessageSuccess = "Message sent" }); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext <KoalaChatDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("LocalDB"))); services.AddDbContext <KoalaChatIdentityDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("LocalDB"))); services.AddIdentity <ChatUser, IdentityRole <Guid> >(options => options.SignIn.RequireConfirmedAccount = false) .AddEntityFrameworkStores <KoalaChatIdentityDbContext>(); RabbitMqConfigurations rabbitConfigurations = new RabbitMqConfigurations(); Configuration.GetSection("RabbitMqConfigurations") .Bind(rabbitConfigurations); services .AddSingleton <RabbitMQ.Client.IConnectionFactory>(new ConnectionFactory { HostName = rabbitConfigurations.Hostname, Port = rabbitConfigurations.Port, UserName = rabbitConfigurations.Username, Password = rabbitConfigurations.Password }); services.AddSingleton <ICommandsHelper, CommandsHelper>(); services.AddSingleton <IMessageQueue, MessageQueue>(); services.AddSingleton <IChatHubService, ChatHubService>(); services.AddScoped <IMessageParser, MessageParser>(); services.AddScoped <IRepository <ChatRoom>, ChatRoomRepository>(); services.AddScoped <IRepository <ChatUser>, UserRepository>(); services.AddScoped <IChatRoomService, ChatRoomService>(); services.AddScoped <ISpecification <ChatRoom>, ChatRoomSpecification>(); services.AddScoped <IRequestHandler <ChatMessageRequestModel, bool>, ProcessMessageSentHandler>(); services.AddMediatR(Assembly.GetExecutingAssembly()); services.AddSignalR(); services.Configure <IdentityOptions>(options => { options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireNonAlphanumeric = true; options.Password.RequireUppercase = true; options.Password.RequiredLength = Configuration.GetSection("UserSettings").GetValue <byte>("PasswordRequiredLength"); options.Password.RequiredUniqueChars = 1; options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(Configuration.GetSection("UserSettings").GetValue <byte>("LockoutTimeout")); options.Lockout.MaxFailedAccessAttempts = Configuration.GetSection("UserSettings").GetValue <byte>("MaxRetriesLogin"); options.Lockout.AllowedForNewUsers = true; options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; options.User.RequireUniqueEmail = false; }); services.ConfigureApplicationCookie(options => { // Cookie settings options.Cookie.HttpOnly = true; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); options.LoginPath = "/Identity/Account/Login"; options.AccessDeniedPath = "/Identity/Account/AccessDenied"; options.SlidingExpiration = true; }); services.AddControllersWithViews(); services.AddRazorPages(); }