Modernizing your .NET apps? Look to Steeltoe's Service Connectors to speed things up.

May 24, 2018 Richard Seroter

My ten-year-old son's bedroom needed a refresh. He’s getting older, and some furniture wasn’t a fit anymore. So, I made the questionable decision to pickup up a desk at IKEA and build it myself. To my surprise, a couple of the trickier pieces in the box came pre-assembled. This saved me some serious time and gave me confidence that those pieces were actually built correctly. The same benefits apply when refreshing your software systems. Are you modernizing some of your key .NET apps? You're probably applying some proven strategies to make the process repeatable. One strategy involves updating what bound services you connect to, and how you do it. With Steeltoe and Pivotal Cloud Foundry, you can easily connect to modern data services, and have confidence that it "just works." Let's see how.

Get away from managing credentials

The best part of the Steeltoe connectors is that they completely absolve you of dealing with credentials. No more stashing values in web.config files, or even an external config store. Instead, when your .NET apps run Pivotal Cloud Foundry, the Steeltoe-infused code detects credentials from the environment and uses those to inflate connection objects. The same codebase gets deployed to every environment (dev/test/production) and yanks credentials specific to each one.

Cloud Foundry makes environment variables (custom or built-in) available to every app instance. It's a neat feature. When an application binds to a service instance through the service broker, the connection details get added to that app's environment variables. Steeltoe knows to look for them, and builds up the objects your code needs to connect.

In this example, I provisioned a PostgreSQL database instance via the Cloud Foundry service broker. My app code (below) has no credentials anywhere. The "connection" object gets built out automatically and injected into my ASP.NET Core controller.

 

public IEnumerable<string> Get([FromServices] NpgsqlConnection conn)

      {

          //open connection provided through dependency injection

          conn.Open();

 

          //create command object

          NpgsqlCommand cmd =

             new NpgsqlCommand("SELECT * FROM pivots", conn);

 

          //execute command get back a reader

          var reader = cmd.ExecuteReader();

          List<string> names = new List<string>();

 

          //loop through results and grab the second value from each row

          while(reader.Read()) {

              names.Add(reader[1].ToString());

          }

 

          //clean up after yourself, dammit!

          reader.Close();

          conn.Close();

 

          return names;

      }

In Cloud Foundry, I can see the environment variable that holds the bound connection info.

 

Introduce powerful open source services to your arsenal

Sometimes, you upgrade your .NET apps by replatforming them. This means running them on a better host (and/or framework) but not changing the source code. If you upgrade from .NET Framework 3.5 to 4.7--and leave everything else the same--that's a basic replatforming. Or, you can choose to update the app host operating system from Windows Server 2012R2 to Windows Server 2016. You get incremental benefits, but benefits nonetheless.

You may want more than that, though. And that means adding new capabilities to your existing apps, or swapping out core components. To improve your app's messaging functionality, you might upgrade from MSMQ to RabbitMQ. Or you could introduce an open-source database like MySQL or PostgreSQL and save on licensing cost. You might even add Redis as a caching layer when you get rid of your ASP.NET in-memory session state provider. Fortunately for you, Steeltoe has "connectors" for those technologies. Your code requires very few changes to make it happen. Like swapping out the engine of your car for a better one, this approach gives your app new life.

Create consistency, whether using .NET Framework or .NET Core

Honestly, I think the coolest thing about Steeltoe is that it works with classic .NET Framework on Windows, or .NET Core (anywhere). Use these libraries to realize microservices patterns in your new and existing apps. The connectors are no different. Whether binding to Microsoft SQL Server, MySQL, PostgreSQL, RabbitMQ or Redis, you can use the same code. Thinking about adding RabbitMQ to your architecture? Good choice. The actual code to talk to RabbitMQ is identical. The only difference is how you instantiate the connector. For .NET Framework on Windows, you update the global.asax.cs file:

protected void Application_Start()
{
   AreaRegistration.RegisterAllAreas();
   FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
   RouteConfig.RegisterRoutes(RouteTable.Routes);
   BundleConfig.RegisterBundles(BundleTable.Bundles);

   ServerConfig.RegisterConfig("development");
   var builder = new ContainerBuilder();

   // Register all the controllers with Autofac
   builder.RegisterControllers(typeof(MvcApplication).Assembly);

   builder.RegisterRabbitMQConnection(ServerConfig.Configuration);

   // Create the Autofac container
   var container = builder.Build();
   DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

 

For .NET Core apps, you simply have to add the RabbitMQConnection to the services container in the startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
           services.AddMvc();
           services.AddRabbitMQConnection(Configuration);
 }

When you're knee-deep in your modernization plans, don't forget to look for tools that pay dividends now, and in the future. Incorporating a framework like Steeltoe gives you a straightforward approach to secure, consistent connectivity to the data services that form the backbone of your modern portfolio. Want to learn more? Start using Steeltoe today, and sign up for the 2-day hands-on class in the days leading up to the cloud event of the year: SpringOne Platform.

 

About the Author

Richard Seroter

Richard Seroter is the VP of Product Marketing at Pivotal, a 12-time Microsoft MVP for cloud, an instructor for developer-centric training company Pluralsight, the lead InfoQ.com editor for cloud computing, and author of multiple books on application integration strategies. As VP of Product Marketing at Pivotal, Richard heads up product, partner, customer, and technical marketing and helps customers see how to transform the way they build software. Richard maintains a regularly updated blog (seroter.wordpress.com) on topics of architecture and solution design and can be found on Twitter as @rseroter.

Follow on Twitter More Content by Richard Seroter
Previous
Improve Security and Developer Productivity with Service Instance Sharing in PCF 2.1
Improve Security and Developer Productivity with Service Instance Sharing in PCF 2.1

PCF 2.1 introduces a new capability that enables multiple development teams working on separate microservic...

Next
No, You Can't Cheat the CAP Theorem. But Pivotal Cloud Cache on PCF Comes Close. Here's How.
No, You Can't Cheat the CAP Theorem. But Pivotal Cloud Cache on PCF Comes Close. Here's How.

Pivotal Cloud Cache on PCF stretches the limits of the CAP theorem, making it uniquely positioned to offer ...