Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ASPNETCoreDockerMicroservices/Services/Identity.Api/Startup.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
101 lines (86 sloc)
3.91 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
using System; | |
using Autofac; | |
using Autofac.Extensions.DependencyInjection; | |
using Identity.Api.Messaging.Consumers; | |
using Identity.Api.Models; | |
using Identity.Api.Services; | |
using MassTransit; | |
using MassTransit.Util; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using StackExchange.Redis; | |
namespace Identity.Api | |
{ | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IContainer ApplicationContainer { get; private set; } | |
public IConfiguration Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public IServiceProvider ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
//By connecting here we are making sure that our service | |
//cannot start until redis is ready. This might slow down startup, | |
//but given that there is a delay on resolving the ip address | |
//and then creating the connection it seems reasonable to move | |
//that cost to startup instead of having the first request pay the | |
//penalty. | |
services.AddSingleton(sp => | |
{ | |
var configuration = new ConfigurationOptions {ResolveDns = true}; | |
configuration.EndPoints.Add(Configuration["RedisHost"]); | |
return ConnectionMultiplexer.Connect(configuration); | |
}); | |
services.AddTransient<IIdentityRepository, IdentityRepository>(); | |
var builder = new ContainerBuilder(); | |
// register a specific consumer | |
builder.RegisterType<ApplicantAppliedEventConsumer>(); | |
builder.Register(context => | |
{ | |
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg => | |
{ | |
var host = cfg.Host(new Uri("rabbitmq://rabbitmq/"), h => | |
{ | |
h.Username("guest"); | |
h.Password("guest"); | |
}); | |
// https://stackoverflow.com/questions/39573721/disable-round-robin-pattern-and-use-fanout-on-masstransit | |
cfg.ReceiveEndpoint(host, "dotnetgigs" + Guid.NewGuid().ToString(), e => | |
{ | |
e.LoadFrom(context); | |
//e.Consumer<ApplicantAppliedConsumer>(); | |
}); | |
}); | |
return busControl; | |
}) | |
.SingleInstance() | |
.As<IBusControl>() | |
.As<IBus>(); | |
builder.Populate(services); | |
ApplicationContainer = builder.Build(); | |
return new AutofacServiceProvider(ApplicationContainer); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public async void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider, IApplicationLifetime lifetime) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
app.UseMvc(); | |
// stash an applicant's user data in redis for test purposes...this would simulate establishing auth/session in the real world | |
var identityRepository=serviceProvider.GetService<IIdentityRepository>(); | |
await identityRepository.UpdateUserAsync(new User {Id = "1", Email = "josh903902@gmail.com",Name = "Josh Dillinger"}); | |
var bus = ApplicationContainer.Resolve<IBusControl>(); | |
var busHandle = TaskUtil.Await(() => bus.StartAsync()); | |
lifetime.ApplicationStopping.Register(() => busHandle.Stop()); | |
} | |
} | |
} |