Thursday, May 17, 2007

Changing the web service URL dynamically in .net

How do I change the web service URL's dynamically across projects in

If you do not want to hard code your web service URL's into your project then there's a way out!
1> You can add web reference in and change the web service property to dynamic.

What we did -
2> Create a utility class and added this static method -
public static CrmService GetCrmService()
CrmService crmService = new CrmService();
crmService.Url = Client.Sample.Data.Properties.Settings.Default.Client_Sample_Data_CrmWebService_CrmService;
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
crmService.PreAuthenticate = true;

return crmService;

3> In the same utility class add Settings file (VS 2005) and add webservice URL's there. Settings is a user friendly GUI add webservice_URL = "\Test.asmx".

4> Save the settings file. The changes once saved would get synced up/updated in app.config available in Utility project. All settings should reflect in applicationSettings in App.config.

5> Copy this section of application settings into your web.config or any config file under any project... reference the utility class (.dll) we just created.

6> Call the static method from your app/project and you should copy paste applicationSettings from utility app.config into your config file.

You should now be good to go. Just change the URL's in your web.config file and without compiling you should be good to go.

Cheers - Dipesh Joshi

No comments: