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
Related
I need to generate machineKey in my web application installer and put it into the application's web.config. How can I do this?
We need to split the answer in two:
Generate the Machine Key: You will need to implement a custom action to generate the Machine Key (There are many tutorials about creating custom actions so I won't cover that here, review the links below). The important part is the code to generate the Key, review these links: C#, Powershell. You can store the result on an installer property, you may need to make it a secure property to avoid it to apear on the installer logs.
Add the value to the Web.config: Now that you have the key, you can use some of the wix custom actions to modify the web.config, you can use XmlConfig or XmlFile. With this you will be able to modify the Xml file to add the machineKey node using the property created on the previous step. Review the links below for reference on how to use these to update the configuration file.
IMPORTANT: The machineKey element is only valid in the Web.config file at the root of your application and is not valid at the subfolder level.
Additional links:
Adding a Custom Action
Editing Web.Config Connection string settings with Wix
Custom actions with C#
How to pass parameters to the custom action?
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.
I'm developing a website in VB.Net, visual studio 2010, that requires accessing a webservice to access the user's login information.
They are logged in through a separate page and when they redirect to my page I access their credentials through the webservice and then handle the session through my own scripts.
What I need to know is:
Can a webservice be checked on pageload if a condition isn't met?
I have not worked with webservices before and have no clue how to add parameters or how to get a value from it. Is it possible to add the reference to my login class(or a class in general)?
I have added a reference through the visual studio: website -> add web reference
But this simply generated a bunch of files and I can't find a good tutorial online about how to use the generated references/files.
I thought it was supposed to generate some class files, however it added a folder (.discomap) with the following types:
.disco
.wsdl
.xsd
Finally, can I test this webservice (which is online and running) on my local host?
Thanks!
It sounds like you're wanting to call these services from your code behind.
When you added a web reference it should have generated a bunch of class files you can use to call the service methods. You should be able to do this from localhost.
A WCF service call from the code behind would look something like this
ServiceReference1.Service1Client client = new
ServiceReference1.Service1Client();
string returnString;
returnString = client.GetData(Param);
label1.Text = returnString;
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.
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.