A good code is something that does the same job with less
We all know about configuration files used in .NET applications we use them from
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 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
Now add the below lines
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
Right, we have just finished configuring the connection string with
Now move to
Program.cs file there, delete the lines 20 – 23 and as the 20th line add
Now hit the F5 key and see whats the good thing happening.
Happy coding folks.