Asp.Net User Secrets

So I’ve been playing around with Asp.Net 5 beta 5 trying to figure out how to use some of the new configuration stuff. Especially the user secrets stuff.

The idea of user secrets is that you need to have some settings on you dev machine which you don’t want in source control. Things like passwords etc.

I’ve found the documentation some what lacking so hopefully this overview will help.

Firstly you need to install the user-secret command. The installation instructions aren’t too bad and can be found here DNX-Secret-Configuration.

To add the user secrets

Once I had that installed I added the userSecrestsId to my project.json, but that didn’t seem to be picked up.

{  
    "webroot": "wwwroot",  
    "userSecretsId": "aspnet5-4c570577-d4ba-491c-a56c-3f267e3d5211",  
    "version": "1.0.0-*",

I found that I need to add this to the startup.js when I loaded the user secrets.

public class Startup  
    {  
        public Startup(IHostingEnvironment env)  
        {  
            var builder = new ConfigurationBuilder();  
            if (env.IsDevelopment())  
            {  
                builder.AddUserSecrets("aspnet5-4c570577-d4ba-491c-a56c-3f267e3d5211");  
            }  
            else  
            {  
                builder.AddEnvironmentVariables();  
            }  
            Configuration = builder.Build();  
        }

The next problem was figuring out how to inject these values into the controller. What I found is you need to AddOptions in the ConfigureServices method and they Configure the class you want the values put in.

public void ConfigureServices(IServiceCollection services)  
       {  
           services.AddMvc();  
           services.AddOptions();  
           services.ConfigureAwsOptions>(Configuration);

My AwsOptions class just has two string fields with the name of the secret properties I want to use.

namespace Config  
{  
    public class AwsOptions  
    {  
        public string awsAccessKey { get; set; }  
        public string awsSecret { get; set; }          
    }  
}

The ASP.NET configuration system will look for values in the Configuration object that match these name and load them into that class. Then to get them in the controller you do the following

[Route("api/[controller]")]  
public class AController : Controller  
{  
    private readonly IOptionsAwsOptions> _awsOptions;  
    private readonly ILoggerSeverController> _logger;  

    public AController(IOptionsAwsOptions> awsOptions, ILoggerAController> logger)  
    {  
        _awsOptions = awsOptions;  
        _logger = logger;  
    }  

    // POST api/values  
    [HttpPost]  
    public async TaskIActionResult> Post(Guid Id)  
    {  

        AmazonSimpleNotificationServiceClient snsclient =  new AmazonSimpleNotificationServiceClient(_awsOptions.Options.awsAccessKey,  
                 _awsOptions.Options.awsSecret, RegionEndpoint.USWest2);

That’s it. Then you will get your user secret values injected into you class.

Full Startup.cs (because I hate it when the snippets miss something I need)

using Microsoft.AspNet.Builder;  
using Microsoft.AspNet.Hosting;  
using Microsoft.Framework.Configuration;  
using Microsoft.Framework.DependencyInjection;  
using Microsoft.Framework.Logging;  
using Severing.Service.Config;  

namespace Service  
{  
    public class Startup  
    {  
        public Startup(IHostingEnvironment env)  
        {  
            var builder = new ConfigurationBuilder();  
            if (env.IsDevelopment())  
            {  
                builder.AddUserSecrets("aspnet5-4c570577-d4ba-491c-a56c-3f267e3d5211");  
            }  
            else  
            {  
                builder.AddEnvironmentVariables();  
            }  
            Configuration = builder.Build();  
        }  

        public IConfiguration Configuration { get; set; }  

        // This method gets called by a runtime.  
        // Use this method to add services to the container  
        public void ConfigureServices(IServiceCollection services)  
        {  
            services.AddMvc();  
            services.AddOptions();  
            services.ConfigureAwsOptions>(Configuration);  
            services.AddLogging();  
            // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.  
            // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.  
            // services.AddWebApiConventions();  
        }  

        // Configure is called after ConfigureServices is called.  
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
        {  

            loggerFactory.AddConsole();  
            // Configure the HTTP request pipeline.  
            app.UseStaticFiles();  

            // Add MVC to the request pipeline.  
            app.UseMvc();  
            // Add the following route for porting Web API 2 controllers.  
            // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");  
        }  
    }  
}

Fix for NServiceBus Testing.

Looks like the issue I had with NSB testing may not have been the Unobtrusive messages.

What I’ve ended up having to do is this.

1
2
3
MessageConventionExtensions.IsMessageTypeAction =
t => t.Namespace != null && t.Namespace.EndsWith(“Messages”) && !t.Namespace.StartsWith(“NServiceBus”);
Test.Initialize();

Making ConfigurationElementCollection Strongly Typed.

I was getting annoyed with ConfigurationElementCollection returning IEnumerable and not IEnumerable, after some Googling I came across this post on StackOverflow https://stackoverflow.com/questions/7991252/read-web-config-section-to-list.

Basically you implement IEnumerable and in GetEnumerator do this

1
2
3
4
public new IEnumerator GetEnumerator()
{
return this.OfType().GetEnumerator();
}

No more casting when you want to use Linq on the ConfigurationElementCollection

Object Reference During NServiceBus Start Up.

One of our NServiceBus services started getting this error today.

1
2
3
4
5
6
7
Fatal    System.NullReferenceException: Object reference not set to an instance of an object.  
   at System.Object.GetType()
   at NServiceBus.Timeout.Core.TimeoutRunner.CacheExistingTimeouts()
   at NServiceBus.Timeout.Core.TimeoutRunner.Run()
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at NServiceBus.Unicast.UnicastBus.NServiceBus.IStartableBus.Start(Action startupAction)
   at NServiceBus.Hosting.GenericHost.Start() in c:\TeamCity\buildAgent\work\nsb.master_8\src\hosting\NServiceBus.Hosting\GenericHost.cs:line 34

Turns out the NServiceBus.Persistence connection string was pointing to a Raven instance that was turned off.

NServiceBus Testing With Unobtrusive Messages

I’ve just had a problem when using nServiceBus.Testing.

When I was running my unit test i was getting
image

Turns out this service was using unobtrusive messages and it appears that nServiceBus.Testing isn’t aware of that and there doesn’t seem to be a way to make it aware. Currently I’ve just added IMessage to my messages.

Stop EF Looking for Migration Tables

I keep forgetting how to do this should I’ll post it here.

Database.SetInitializer(null);