Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A tiny snippet of what I'm trying to do. Full code is here: https://github.com/NuGet/NuGetGallery/tree/anurse/v3-jobs/src/Services and kinda complicated ;P
public class JobsService : NuGetService {
// .. stuff!
protected override Task<bool> OnStart() {
// Here, Container is the "scope" passed in to service.Initialize above.
Jobs = Container.Resolve<IEnumerable<JobDefinition>>();
// *** KA BOOM! Exception trying to construct a JobDefinition (when it's not designed to be constructed by Autofac) ***
}
public override void RegisterComponents(ContainerBuilder builder)
{
base.RegisterComponents(builder);
var jobdefs = typeof(JobsWorkerRole)
.Assembly
.GetExportedTypes()
.Where(t => !t.IsAbstract && typeof(JobBase).IsAssignableFrom(t))
.Select(t => new JobDefinition(JobDescription.Create(t), t))
.Where(d => d.Description != null);
foreach (var jobdef in jobdefs)
{
builder.RegisterInstance(jobdef).As<JobDefinition>();
}
}
}
public class ServiceHost
{
// ... bunch-o-stuff ...
async Task<NuGetService> StartService(NuGetService service) {
// .. some stuff ..
ILifetimeScope scope = _container.BeginLifetimeScope(builder =>
{
builder.RegisterInstance(service)
.As<NuGetService>()
.As(service.GetType());
// Add the container itself to the container
builder.Register(c => scope)
.As<ILifetimeScope>()
.SingleInstance();
builder.Register(c => c.Resolve<ILifetimeScope>() as IComponentContainer)
.As<IComponentContainer>()
.As<IServiceProvider>()
.SingleInstance();
// Add components provided by the service
service.RegisterComponents(builder);
});
// .. more stuff
service.Initialize(scope);
// .. even more stuff
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.