Well, call me an "Ideationista" because what takes me more than 30 minutes to do, my creativity sparks. I always have an idea to build something to do it easier and in the shortest time, so I can have more time for my coffee :D
Today's post is related to a small application that updates several organization and uses several servers, this means, instead of doing everything manually this does it automatically in a way it can be used several times. But I had a small issue when I was trying to run the application. My goal was to run the same code (in my case clean a certain workflows, plugins and do some update on configuration records) for several servers and organizations using a file as configuration so other users could use it without the need of editing the application code. Calling the right method with the right value and connections strings... and here's where the party begins.
Even if you try to change the connection string by using the following code, the application continues to do the query expression based on the same server and organization, even if you see that the connection string is in fact changed.
string _Server = list6[i];Solution:
string _Org = list7[i] ;
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["xrm"].ConnectionString = "Server=https://"+_Server+".contoso.com/"+_Org+"; Username=Domain\Contoso; Password=ContosoPassword";
First, I tryed to clean the connection string and change everything on the console application even on the app.config, but still with no success, It seems that the connection was still in memory... then I google it, I found this code:
xrm.Dispose();I tried it, hoping this would clean the connection... but it didn't... so, a few webpages after, I found that without changing any app.config or other code, the only thing I needed was to replace the code above for the one below.... and it did work out:
This application, what it does, is to stream a file:
var reader = new StreamReader(File.OpenRead(@"file.csv"));Put everything into lists and then execute the methods and connection dynamically, in my case I have more than 60 organizations and this is the fastest way to update all the organization in one shot.
Example .CSV file: