In your Main program method, build up your container and register services using the Autofac extensions. This will attach service registrations from the container and the ServiceRuntime. Dispose of the container at app shutdown.

usingSystem;usingSystem.Diagnostics;usingSystem.Reflection;usingSystem.Threading;usingAutofac;usingAutofac.Integration.ServiceFabric;namespaceDemoService{publicstaticclassProgram{privatestaticvoidMain(){try{// The ServiceManifest.xml file defines one or more service type names.// Registering a service maps a service type name to a .NET type.// When Service Fabric creates an instance of this service type,// an instance of the class is created in this host process.// Start with the trusty old container builder.varbuilder=newContainerBuilder();// Register any regular dependencies.builder.RegisterModule(newLoggerModule(ServiceEventSource.Current.Message));// Register the Autofac magic for Service Fabric support.builder.RegisterServiceFabricSupport();// Register a stateless service...builder.RegisterStatelessService<DemoStatelessService>("DemoStatelessServiceType");// ...and/or register a stateful service.// builder.RegisterStatefulService<DemoStatefulService>("DemoStatefulServiceType");using(builder.Build()){ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id,typeof(DemoStatelessService).Name);// Prevents this host process from terminating so services keep running.Thread.Sleep(Timeout.Infinite);}}catch(Exceptione){ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());throw;}}}}

It is possible to achieve a “per request” style scoping mechanism by making use of the implicit relationships supported by Autofac.

For example, if you have a stateless service, its lifetime is effectively a singleton. You would want to use the Func<T> or Func<Owned<T>> relationships (for non-disposable vs. disposable components, respectively) to inject an auto-generated factory into your service. Your service could then resolve dependencies as needed.

For example, say you have a user service that is stateless and it needs to read from some backing store that shouldn’t be a singleton. Assuming the backing store is IDisposable you’d want to use Func<Owned<T>> and inject it like this: