Access websites connection string from web jobs - azure

I have been trying to move my connection strings from the App.config files for my webjobs to the host website through the Azure Portal. I've tried the likes of:
ConnectionStringSettings test = ConfigurationManager.ConnectionStrings["AzureDB"];
I've also tried using GetSetting and putting the connectionstring in the app section of the configure options as a key/value pair.
That still only shows my local connection strings. I've also tried using the CloudConfigurationManager to no avail.
There are examples all over the place and in SO citing:
However, you don't have to set it in app.config for the web job if you set them in the portal. The WebJobs SDK knows how to read them from there.
So how do I access them?

Install Microsoft Azure Configuration Manager
Move your connection strings to the appSetting section (for debug purpose)
Retrieve you connection string using the CloudConfigurationManager:
var myConnectionString = CloudConfigurationManager.GetSetting("MyConnectionString");
You can now manage your connection string from within the Azure portal.

Azure WebJobs shares the settings with your website
http://blog.ploeh.dk/2014/05/16/configuring-azure-web-jobs/

Related

Why do I need to configure connection strings for WebJobs in Azure Management Portal rather than in the App.config of my WebJob?

I created a scheduled Azure WebJob by right clicking on my WebApp project and adding a New Azure web project. I set the the AzureWebJobsDashboard and AzureWebJobsStorage connection strings in the App.config of the WebJob project. I left the default code in the .cs files alone for now. Next, I published from Visual Studio (2013 Update 4 by the way) to an Azure website. It created the WebJob and I can run it from the Azure Management site or Visual Studio and it is successful. I can see the output logs where I expect them to be. However, the Azure WebJob Details page shows the following warnings/errors:
Make sure that you are setting a connection string named
AzureWebJobsDashboard in your Microsoft Azure Website configuration by
using the following format
DefaultEndpointsProtocol=https;AccountName=NAME;AccountKey=KEY
pointing to the Microsoft Azure Storage account where the Microsoft
Azure WebJobs Runtime logs are stored.
The configuration is not properly set for the Microsoft Azure WebJobs
Dashboard. In your Microsoft Azure Website configuration you must set
a connection string named AzureWebJobsDashboard by using the following
format DefaultEndpointsProtocol=https;AccountName=NAME;AccountKey=KEY
pointing to the Microsoft Azure Storage account where the Microsoft
Azure WebJobs Runtime logs are stored.
It also provides a link that was to http://azure.microsoft.com/blog/2013/07/17/windows-azure-web-sites-how-application-strings-and-connection-strings-work/ which doesn't explain why I am getting the error message.
Now, to get this warning/error to go away, I go add the same AzureWebJobsDashboard connection string in the configure page in the Azure Management site.
So, I'm trying to figure out if I did something wrong or if this is just a kink in the WebJobs that Microsoft hasn't fixed yet. I would really like to be able to have this in the App.config rather than have to remember to set it up in each website we spawn up for different environments.
Thank you very much for your time and help.
The dashboard doesn't have access to your web job's app.config file and even if it did, you might have multiple webjobs so it wouldn't know which connection string to pick. That is why you need to set the connection string in the portal.
However, you don't have to set it in app.config for the web job if you set them in the portal. The WebJobs SDK knows how to read them from there.
I had to manually add authorization headers:
For get the value anfter "Basic " I used this code:
var byteArray = Encoding.ASCII.GetBytes("<user>:<password>");
var base64 = Convert.ToBase64String(byteArray);
Ithink is a Azure Scheduler UI bug

Is it possible to change connection string on cloud service deploy-time?

I wrote an asp.net mvc 4 web site. For my corporation purpose, I created several publish profiles, in order to publish it on differents azure websites. Each profile changes the connection strings.
It worked very well.
I am now trying to do the same thing with an azure cloud service (I added my web site as a web role in a new cloud service project). But I can't find out how to change the connection string of my web role depending of my cloud service project publish profile at deploy-time.
Is there a way to do such a thing ?
Thanks !
You could store the connection string in the ServiceConfiguration file .cscfg of your Role and then use the following in your code to retrieve the setting:
string connectionString = RoleEnvironment.GetConfigurationSettingValue("myConnectionString");
You can modify the values in the ServiceConfiguration through the Management Portal once deployed.

Windows Azure portal connection strings

couple of questions regarding the connection strings section for websites in the windows azure portal:
do the settings in the portal take precedence over what's in web.config? If so, should I not include connection strings in the web.config file? Are the settings in the portal sometimes referred to as app.config?
can i set connection strings for ado.net entities in the windows azure portal? This does not seem to work for this specific type of connection string.
in the portal i included the following connection string and gave it the same name as what is in my web.config:
metadata=res:///Models.TABLENAME.csdl|res:///Models.TABLENAME.ssdl|res://*/Models.TABLENAME.msl;provider=System.Data.SqlClient;provider connection string="Server=tcp:SERVERNAME.database.windows.net;Database=DATABASENAME;User ID=USER;Password=PASSWORD;Trusted_Connection=False;Encrypt=True;
The connection string in my web.config file works in my dev environment, but i think i have something typed wrong on the azure portal.
"do the settings in the portal take precedence over what's in web.config?"
Yes.
"If so, should I not include connection strings in the web.config file? "
Use the connectionstring found in Azure portal and insert it in your web.config.
This, of course, does not apply to if you are using a local database. Then you should use the local connectionsstring.
"Are the settings in the portal sometimes referred to as app.config?"
No. Not if you are stil on the subject of connectionstrings.
"can i set connection strings for ado.net entities in the windows azure portal? This does not seem to work for this specific type of connection string"
Check my blog post on basic set up for this here under step 2(2. Now, go to the Management portal and Create a new db.) http://geekswithblogs.net/MagnusKarlsson/archive/2012/10/31/add-sql-azure-database-to-azure-web-role-and-persist.aspx

Is it possible to acces a table storage in Azure from an Azure Web Site

I would like to use the Azure Table Storage Service from an Azure Web Site (not a Cloud Service). There is guides on how to do this using Node.js but I like to use .NET MVC instead.
All the guides for .NET talks about storing the Azure Storage connection information in the ServiceConfiguration (as you do in a Cloud Service) but in an Azure Web site I do not have this (just a Web.config). If I am not mistaken it is also not possible to use the RoleEnvironment (used to read from the ServiceConfiguration) without running in the Azure emulator and I do not do this in an AzureWeb Site.
Is it possible to access the Table Storage from an Azure Web Site and if so how do I connect to it?
I have looked at this question and it does not look similar.
You can simply get the connection string from the web.config and parse it (note that you can also use the CloudConfigurationManager.GetSetting method):
var connectionString = ConfigurationManager.AppSettings["myStorageAccountConnectionString"];
var storageAccount = CloudStorageAccount.Parse(connectionString);
var tableClient = storageAccount.CreateCloudTableClient();
And in your web.config you will need to add the connection string like this:
<appSettings>
<add key="myStorageAccountConnectionString" value="DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=fazfazefazefzeefazeefazfazefazef"/>
</appSettings>
Short answer: yes. Table Service has a REST API, which means you can access it from any client platform that can communicate over http.
Googling then produces tons of examples:
http://azure.snagy.name/blog/?p=294
http://blogs.msdn.com/b/rickandy/archive/2012/09/20/azure-table-storage.aspx
You can use the CloudTableClient from MVC: even if most examples are for Cloud Services, you can easily tweak them to get connection data from web.config, or from any other source. How-to docs are here: https://www.windowsazure.com/en-us/develop/net/how-to-guides/table-services/

What is the item of 'connection strings' for on azure? And how could I add it?

Here is the picture.
http://www.dotup.org/uploda/www.dotup.org3258537.jpg
I have an connection string to connect to the SQL Server Database on the web.config of my website.
Would this item on the azure portal bring benefits to me?
It is the connection string for items in the Linked Resources section of the portal for the website - which currently only supports SQL Database (and not Windows Azure Storage, for example). The following is documented:
Connections Strings – View connection strings for linked resources.
For .NET sites, these connection strings will be injected into your
.NET configuration connectionStrings settings at runtime, overriding
existing entries where the key equals the linked database name. For
PHP and Node sites these settings will be available as environment
variables at runtime.
Note Connection strings are created when you link a database resource
to a website and are read only when viewed on the configuration
management page.

Resources