Skip to content
Really poorly documented project with Autofac DI working with Service Fabric WebAPI
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
LICENSE
README.md

README.md

Autofac Service Fabric Example

Really poorly documented project with Autofac DI working with Service Fabric WebAPI to respolve a reliable sevice.

It sets up Autofac, configures a basic repo and a Reliable Service, before requesting the Repo (which in turn requests the service) via DI in the Controller.

###Config in Startup.cs

public static void ConfigureApp(IAppBuilder appBuilder)
{

    var builder = new ContainerBuilder();
    // Register your Web API controllers.
    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

    // Configure Web API for self-host. 
    HttpConfiguration config = new HttpConfiguration();
    
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    builder.RegisterWebApiFilterProvider(config);

    //Register the repo that our code will use to abstract the end code one level from the actor
    builder.RegisterType<SomeRepo>().As<ISomeRepo>();

    //Register the actor.
    builder.Register((e) => ServiceProxy.Create<IStateless1>(new Uri("fabric:/Application3/Stateless1")))
        .As<IStateless1>();

    // Set the dependency resolver to be Autofac.
    var container = builder.Build();
    config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    
    appBuilder.UseWebApi(config);
}

###The repo in SomeRepo.cs

public interface ISomeRepo
{
    Task<string> GetSomething();
}

public class SomeRepo : ISomeRepo
{
    private readonly IStateless1 _service;

    public SomeRepo(IStateless1 service)
    {
        _service = service;
    }

    public async Task<string> GetSomething()
    {
        return await _service.GetHello();
    }
}

###Consuming it in the ValuesController.cs

public class ValuesController : ApiController
{
    private readonly ISomeRepo _someRepo;

    public ValuesController(ISomeRepo someRepo)
    {
        _someRepo = someRepo;
    }
    
    // GET api/values 
    public async Task<IHttpActionResult> Get()
    {
        return Ok(await _someRepo.GetSomething());
    }
}

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.