How to change web service URL dynamically or easily? - asp.net

I have few web service references in an asp.net vb web application .net 4 and using VS2010.
One of them is WCF service (I think as it has .svc at the end) and two of them is .asmx services.
I have to change them quite often to change to different environment such as development, test, production. What i normally do is right click on the service reference (.svcmap file) and choose configure service reference. It will update the service reference and web.config. It will also override binding setting such as maxReceivedMessageSize="2147483647" that i have to put in web.config every time I change the URL.
I am wondering if there is an easier way to change the URL than what I am currently doing. I have seen similar posts on SO about setting URL Behaviour = Dynamic but I don't have that option in mine and I don't know why.

With .Net 4, it looks like the proxy class has a constructor that takes some useful parameters. Try instantiating your proxy class like this:
MyService.FooSoapClient foo=new MyService.FooSoapClient("MyServiceSoap",myURL);
The MyServiceSoap name comes from the App.config file. Look for name property in the <client> <endpoint ... name="MyServiceSoap" /> tag.

Related

ASP.NET Configurable Web Service

I have a web service reference in a ASP.NET 2.0 web site project. I'd like to be able to switch between staging and production versions of the service without having to change my code.
However, I'm not seeing how to do that in a clean way. I know that I can change web.config to point to some other service URL, but then isn't my code still hard-coded to one reference or the other?
I saw this post, but how do you edit the proxy class? If I "Go to Definition" on the class, it doesn't take me to any class that I can edit, but it's the object browser... Do I need to run the wsdl.exe utility so that it will generate a proxy class for me that I can then edit?
Edit #1: Here's the code I'm using to instantiate and call the service:
Dim service As New Swan.MagellanLeadSheetService()
Dim response As Swan.MagellanLeadSheetResponse = service.Foo(stuffToSendToService)
Edit #2: Since the web.config already has the URL endpoint address in the appSettings area, can I just simply edit that setting when we deploy to Production to point to the production URL? Is it that simple? I was worried about the potential for breaking changes between the proxy classes of Staging vs. Production, but those should be resolved prior to deploying any changes to Production I think.
The following article explains how you can make the Web Service Reference dynamic by changing the reference properties, adding a key to the web.config file and referencing this key on the application code:
Article Link
Basically you will have 2 versions of web.config file, production and staging with different URLs defined. While the code will point to a unique location.
UPDATE:
Now, before the following line, you have to alter the service.URL according to what stands in the web.config.
Dim response As Swan.MagellanLeadSheetResponse = service.Foo(stuffToSendToService)
First of all here is some info how to add a web reference to your project:
How to: Add and Remove Web References
Then instance of your webservice class (the proxy class that derives from SoapHttpClientProtocol) has to have an property called url, with that property you can swith at runtime to your asmx from staging or production.
CountryService service = new CountryService();
service.Url = "http://foo/bar.asmx";
More info: WebClientProtocol.Url Property

Kind of configuration settings can be made in XML files (web.config)?

As we know there are some config setting we can make to web.config file like mentioned at link
Now as a beginner i need to know what else we can do with this file/ i.e what else we can configure with this file in ASP.NET MVC2,3?
You can configure many other items in the web.config. However this question is not direct enough to provide an answer.
You can see the entire schema here
Web.Config is not limited to any application. Web.config can work verymuch in same way for any applications. Along with the mentioned details we can include a bunch of sections into Web.Config. some of them includes
1) If you are using any services, then service end points you can define in web.config.
2) Error/Exception handling configuraton you can define.
3) if you will use Unity, you can define Register types in config.
4) Any IIS configuratons, can overridden in web.config.
5) Any IIS Error page setup.

How can I add location elements programmatically to the web config?

I have an application which creates page routes from a database. My whole site is secured with forms authentication but I need to allow unauthenticated uses to access these routes. I don't want to hard-code <location> tags for the routes in the web.config as this will negate me using a database to generate the routes.
Can anyone help?
Thanks everyone. I've found an answer here
Basically it involves creating a folder for each route and putting a web.config file in it allowing access. This approach needs to be coupled with setting RouteExistingFiles to false so that the routes don't get confused with the folders.
Rather than using strongly typed configuration classes, why not make the modifications directly in XML?
Here's an abbreviated snippet to demonstrate the concept from some code of mine that performance IIS tuning in the machine.config. The principal is the same for other XML config files though. You just need to create the appropriate XPath statements to do what you need.
XmlDocument machineConfigFile = new XmlDocument();
machineConfigFile.Load(MachineConfigPathString);
XmlNode autoConfig = machineConfigFile.SelectSingleNode(#"/configuration/system.web/processModel/#autoConfig");
autoConfig.Value = "false";
machineConfigFile.Save(MachineConfigPathString);
When saved, the XmlDocument object will preserve all other untouched document nodes. Very handy. It works great for modifying the machine.config. The only possible issue I can see is that your application will probably reset when you save your changes to the web.config. So test it out in a safe environment with a backup of your web.config just in case the reset causes any undesired outcomes!
I found this MSDN link for you. I didn't find whether you can modify the config of running server instance this way though.
Have you considered implimenting your site security in a different way? Having a portion of the site that allows unauthenticated access and a portion that does not. I am "assuming" (bad) that you are using MVC since you are describing routes - this is very easy to do with both MVC and traditional web form applications.

Dynamically reading app.settings in asp.net

I've moved my appsettings section outside of the web.config using:
<appSettings configSource="AppSettings.config"/>
This allows me to change my appsettings without actually restarting IIS.
I know however, that IIS monitors all configuration files constantly. How can I attach to event my-appsetting-has-changed to take some custom action upon that?
According to this reference and this reference,
[The] ASP.Net runtime does not detect when the external [config] file changes.
If that's true, then you might get some mileage out of the FileSystemWatcher, but I cannot think how to use that effectively in an ASP.NET scenario.
I hope this helps.
You can use the FileSystemWatcher class for this.

How do I change the URL of my web service?

If I change the url in the web.config file will the change be reflected in the .disco, .discomap, and .wsdl files that are in the WebReferences folder?
[Edit]
I'm using asp.net 2005
The change will not, as far as I'm aware, be reflected in all the other files. However, if you change the URL in web.config, your application will call the web service from the new URL at run-time. Check out this blog entry. (No, it's not one of mine!)
Assuming you mean in the client, set the Url property at execution time. You can configure this from anywhere you want, so long as you have access to the value at the appropriate time.
As I can remember WSDL file contains the binding(s) (URL mappings) of your WS. So if you change the URL on your machine (in WSDL, Disco, etc.) that's enough.
On the other hand don't forget to regenerate client proxies. They have to reflect WSDL changes.

Resources