Created
December 18, 2013 17:46
-
-
Save analogrelay/8026685 to your computer and use it in GitHub Desktop.
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
This file contains hidden or 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
| 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>(); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| 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