Is there a way to programmatically set the connection string for ASP.NET Universal Providers?

Go To StackoverFlow.com

4

I'm looking to deploy a solution to Azure that uses the ASP.NET Universal Providers. I want to store my connection strings in Azure config instead of in the web.config so I can change them without redeploy. I had thought about loading from settings and adding to the ConfigurationManager.ConnectionStrings collection but that is readonly. There are reflection hacks to overcome this, but that doesn't seem like the right way to go. I also was thinking about deriving from the provider classes to create my own provider classes that can set the connection string, but I don't see a property to set to override the connection string. Any ideas on a way to use the providers but not have the connection string in my web.config?

2012-04-04 18:46
by Bryan
Using ILSpy, I can see that the connection string is set in the Initialize method and is a private property - Bryan 2012-04-04 18:59


1

I would suggest you put the settings in the Azure config file and then write a startup task or in the WebRole.cs OnStart you can access the IIS Management API's and update the web.config file in there with the correct connection string. That way you aren't having to get programmatic access to the connection string but still do what you want.

You can check out this blog post for some samples of using the API's althought it isn't doing what you are wanting to:

http://blogs.msdn.com/b/tom/archive/2011/02/18/installing-and-using-an-httpmodule-in-windows-azure.aspx

2012-04-04 20:58
by Tom
Right now I'm thinking about looking at getting the source and making the changes I need. I don't know that the ASP Universal Provider source is available, but the 2.0 versions are available (http://weblogs.asp.net/scottgu/archive/2006/04/13/442772.aspx) or I might look into this: http://efmembership.codeplex.com - Bryan 2012-04-06 21:03
@Bryan - You can decompile System.Web.Providers.dll by yourself. I used Telerik JustDecompile which is free - Win 2012-04-10 16:09
I have a solution working using the Microsoft.Web.Administration API to modify the connection strings in the web config at role startup with values from my azure config file similar to this post:http://blog.elastacloud.com/2011/05/13/azure-howto-programmatically-modify-web-config-on-webrole-startup/ I had to make sure I raised priveleges which might not be ideal, so I may look to do code in a start up task and only raise the start up task's permissions - Bryan 2012-04-13 02:23
Ads