Tags

, , ,

A good code is something that does the same job with less lines chars.

We all know about configuration files used in .NET applications we use them from app.config to web.config. But most of the beginners in .NET never employ them efficiently. We all have almost forgot connection strings and System.Configuration namespace. I never see them in many of the codes.

The primary advantage of having Config files is exactly the scenario that you are dealing with (multiple projects and centralized configuration). When it comes specifically with web.config, Storing the encrypted connection string in web.config file is really a good practice. Even for app.config for console and other non web apps in .NET we can use app.config to boost the project efficiency, we never need to call the connection strings again in again in every files or even sometimes, like the below scenario, we never wanted to create a new object to handle the credentials and waste some good memory from the device.

The Good Scenario

In my earlier post, Dummies’ guide to Upload to Azure Cloud Storage I explained how to create a cloud storage service and how to upload a local file to the storage via a C# Console application. Please click the link above and go to that post and see the code ((code.Visible=true) ? "read it carefully" : "scroll down until code visible please";).

There carefully see lines 20-23, that code created 2 objects, sc which is a StorageCredentials object and storageAcc which is a CloudStorageAccount object. Remember both these classes are respictively from Microsoft.WindowsAzure.Storage.Auth and Microsoft.WindowsAzure.Storage namespaces. My argument is those lines are really not necessary and there is no need to use Microsoft.WindowsAzure.Storage.Auth namespace as well by using connection strings to connect with the cloud storage service.

How to do this

Consider the same application. If you tried that sample and implemented yours open that solution/project and open app.config file.

Initially that file might have something like

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

Now add the below lines

1
2
3
4
5
6
<configuration>
    <appSettings>
        <add key="StorageConnectionString" 
          value="DefaultEndpointsProtocol=https;AccountName=paste_the_cloud_service_name_here_eg_somegoodstorage;AccountKey=please_paste_the_accesskey_here" />
    </appSettings>
</configuration>

Remember to change paste_the_cloud_service_name_here_eg_somegoodstorage and please_paste_the_accesskey_here with your credentials. Now the app.config looks something similar to

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="StorageConnectionString" 
         value="DefaultEndpointsProtocol=https;AccountName=somegoodstorage;AccountKey=kdfjhksdjfksjdfkasjdfsdf" />
  </appSettings>
</configuration>

Right, we have just finished configuring the connection string with app.config file.
Now move to Program.cs file there, delete the lines 20 – 23 and as the 20th line add

CloudStorageAccount storageAcc = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

Now hit the F5 key and see whats the good thing happening.

Happy coding folks.

Advertisements