Live is streaming live. Watch now.

Steeltoe & Unity in Full Framework

It takes quite an effort to instrument/inject some of the Steeltoe features in full framework app. SteeltoeOSS has Autofac extensions for full framework to abstract away that effort. But there are no Unity extensions available. Sample apps in steeltoe-unity-samples git repo illustrate how to use Steeltoe features with less effort when using Unity for IoC services.

How it works? - configure and add services to Microsoft DI container and load them into Unity container. So you can leverage Unity IoC in your apps the same way you do and can inject Steeltoe interfaces. Unity container will resolve them without any issues.

Note: Create/upgrade your app to target .Net Framework 4.6.1 or above, since we are making use of .Net Standard libraries

Prerequisites

  1. ASP.NET app targets .Net Framework 4.6.1 or above
  2. ASP.NET WEB API project use Unity.Aspnet.Webapi nuget
  3. ASP.NET MVC project use Unity.Mvc nuget

How to instrument the app to load registration into Unity container

  1. Get Unity.Microsoft.DependencyInjection nuget

  2. Add services to Microsoft DI container. Refer to ApplicationConfig.cs

  3. Use DiscoveryConfig.cs to register/fetch your app in/from Eureka server

  4. Use ManagementConfig.cs to add Cloud Foundry management endpoints and healthchecks

  5. Register and build service provider in Application_Start() method in Global.asax.cs

    // register microsoft & steeltoe services  
    ApplicationConfig.Register(Environment.GetEnvironmentVariable("ASPNET_ENVIRONMENT") ?? "Development");  
    
    // build service provider for unity container  
    ApplicationConfig.BuildServiceProvider(UnityConfig.Container);
    

    BuildServiceProvider(UnityConfig.Container) method loads registrations from Microsoft container to Unity container

    public static void BuildServiceProvider(IUnityContainer container)
        {
            // add services to unity container. piggy backing on Unity.Microsoft.DependencyInjection extensions
            container.BuildServiceProvider(_services);
        }
    
  6. Refer to Global.asax.cs to start and stop Discovery client

ASP.NET 4.x Samples with Unity and Steeltoe

  • src/FortuneTeller/Fortune-Teller-Service - use config server, connect to a MS SQL database on Azure, use Discovery client, add health management
  • src/FortuneTeller/Fortune-Teller-UI - use config server, connect to Fortune-Teller-Service using Discovery client, add health management, connect to Redis server on Cloud Foundry