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 a Senior Director of Product for Pivotal, an 11-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 a Senior Director of Product at Pivotal, Richard heads up product 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
Getting Ready to cf push? Great! Now, Think About These 5 Things Before You Write a Line of Code.
Getting Ready to cf push? Great! Now, Think About These 5 Things Before You Write a Line of Code.

Many enterprises are standardizing on Pivotal's app platform, PAS. Once that decision is made, developers t...

Next
Getting Started with Advanced Analytics
Getting Started with Advanced Analytics

A conversation with Ovum’s Tony Baer about the future of data.Some overnight successes hide the decades of ...

Enter curious. Exit smarter.

Learn More