Skip to content

Autofac integration for Azure Service Fabric. Provides service factory implementations for Actors, Stateful Services and Stateless Services.

License

Notifications You must be signed in to change notification settings

autofac/Autofac.ServiceFabric

Repository files navigation

Autofac.ServiceFabric

Service Fabric support for Autofac.

Build status

Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.

Quick Start

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.

using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using Autofac;
using Autofac.Integration.ServiceFabric;

namespace DemoService
{
  public static class Program
  {
      private static void Main()
      {
        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.
          var builder = new ContainerBuilder();

          // Register any regular dependencies.
          builder.RegisterModule(new LoggerModule(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 (Exception e)
      {
        ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
        throw;
      }
    }
  }
}

Check out the documentation for more usage details.

Get Help

Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.

About

Autofac integration for Azure Service Fabric. Provides service factory implementations for Actors, Stateful Services and Stateless Services.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published