Dynamic Web reference problem in C#

I have a problem with a small app I am writing in c~ .net using Visual Studio 2010.

When I add a web reference, and set it as "dynamic" so that I can later change the URL that is used to access it, I come across a problem.

Although, the application does indeed access the web reference at the URL I later specify using:

proxy.URL = "http://pathtothewsdl"

In the proxy file that is generated by Visual Studio for the web reference, it is littered with hard coded references to the namespace of the web reference... i.e....

[System.Web.Services.Protocols.SoapRpcMethodAttribute("http://wsdl.wipapp.co.uk/sync_interface.php/create_session", RequestNamespace="http://wipapp.co.uk", ResponseNamespace="http://wipapp.co.uk")]

This kind of thing repeated for every function specified in the WSDL and every custom type defined in there too.

This causes the app to start randomly throwing errors when I change the URL for the web reference, which I need to do, as I am actually calling an identical web reference on two different servers (the app does a sync of data between two identical applications sat on two different servers).

Any clues how I can make the "dynamic" web reference, so it actually IS dynamic?!
TeamLinkAsked:
Who is Participating?
 
13598Connect With a Mentor Commented:
Look at this. You said it failed with certain functions? This example uses reflection:
http://www.crowsprogramming.com/archives/66
0
 
13598Commented:
0
 
TeamLinkAuthor Commented:
Thanks but I've already done this. I still have the problem above - because of code like this that it seems to add...

[System.Web.Services.Protocols.SoapRpcMethodAttribute("http://wsdl.wipapp.co.uk/sync_interface.php/create_session", RequestNamespace="http://wipapp.co.uk", ResponseNamespace="http://wipapp.co.uk")]


where http://wipapp.co.uk is the address of the web reference when I first add it, whenever I change the web reference URL, anything using any of the items declared as above (like custom return types for example), stops working.
Although actually, I say "anything", but in fact it's only some things. And some things still work. It's really bizarre and quite hard to explain!

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
13598Commented:
How exactly are you 'changing the web reference URL'?
0
 
TeamLinkAuthor Commented:
here is the code, where soap is the name of the web reference. I've just changed the URL for privacy.

soap Soap_source;
Soap_source = new soap();
Soap_source.Url = "http://wipapp.co.uk/sync_interface.php";
0
 
13598Commented:
You can use the Web Reference URL property to specify the URL to the XML Web service. The Add Web Reference sets this property by default to the URL of the XML Web service you select, which is a static URL. A Web reference can use either a static URL or a dynamic URL.

If you leave the URL Behavior set to the default value of static, the proxy class sets the URL property using a hard-coded URL when you create an instance of the class.

If you set the URL Behavior property of the Web reference to dynamic, the application obtains the URL at run time from the <appSettings> Element of your application's configuration file. When you specify a dynamic URL after adding a Web reference, Visual Studio updates the proxy class to obtain the URL from the configuration file.

Copy<appSettings>
   <add key="myApplication.myServer.Service1"
        value="http://myServer/myXmlWebService/Service1.asmx"/>
</appSettings>
When you create an instance of a proxy object, you can also set the URL property of the object in your application.

Regardless of which URL the proxy uses, it must be for an XML Web service that adheres to a WSDL that matches the one used when adding the Web reference
0
 
13598Commented:
Try changing the value in your appsettings since you are changing it at runtime.
0
 
TeamLinkAuthor Commented:
Yes, I've tried all of that - this is the error I get (and see screenshot):

{"The specified type was not recognized: name='entity_detail', namespace='http://wipapp.co.uk/soap/soap', at <return xmlns=''>."}

Basically, I make one call to one web service and it works absolutely fine - and then another to another web service which is to a system which is an exact replica (they are even both on the same machine - the one I am developing the app on), but with a different URL and I get this error. But ony on running certain functions.

It's so bizarre and frustrating!
Capture.JPG
0
 
13598Commented:
What version of C# are you using?
0
 
TeamLinkAuthor Commented:
it's C# in Visual Studio 2010 RC1 - is that what you need to know?
0
 
TeamLinkAuthor Commented:
But having just tried, I got exactly the same problem in Visual Studio 2003 and 2008 :(
0
 
13598Commented:
Sorry I meant what .net framework version.
That message indicates it doesn't like some value in the xml. If you just try calling the service that fails dynamically first, does it work? What I mean is we don't want to assume that it fails because of dynamically changing the call at runtime. Have you tried calling that service first to make sure it does work and it only fails when called second?
0
 
TeamLinkAuthor Commented:
OK, sorry, well, currently 3.5 framework - although again - it doesn't seem to make much difference which I use.

It definitely works before I dynamically change the URL.

I'm convinced it's because the C# file it creates has hard-coded in it the path to the WSDL - as I've put above a few times - lines like this:

[System.Web.Services.Protocols.SoapRpcMethodAttribute("http://wsdl.wipapp.co.uk/sync_interface.php/create_session", RequestNamespace="http://wipapp.co.uk", ResponseNamespace="http://wipapp.co.uk")]

If I manually change all of the references in bold above to point to the same place as the URL I set as the URL for the web service, then it starts to work again.

It's as though it can't match the two up. If I could change all the references like the one above, by setting a variable, just like I do by setting the URL varaible, I'm pretty certain that would work, but I can't figure out how to do this, as it will only seemingly let me replace these values with constants!


I do now have a sort of work around, which sort of works. I have added 2 separate web references to my project for the two sides of the sync process.

However, still, if I change the URL on either of these - they stop working - which means it will have to be compiled with the final URL's to be used - and these are likely to be changeable, which would mean I'd need to recompile everytime they changed - Surely there must be a better way?!
0
 
13598Commented:
Can you post the code where you change the url in the appsettings?
Also is there a reason why you chose C# instead of asp.net?
0
 
13598Commented:
You did this, right?
Simply choose the properties of the web service and change the "Url Behavior" from "Static" to "Dynamic". This will automatically create an entry in the <appSettings> section of the Web.Config file that contains the Url string. The constructor of the Web Service proxy class is automatically changed to set the Url property to the value stored in Web.Config.
0
 
13598Commented:
I found this I don't know if it will help you:
'I found a method of the class (SoapHttpClientProtocol) from which my web reference object was created that allowed me to set the "Url" that specified the location of the Web Service. At this point, I realized I could delete all but one of my web references, and then set the "Url" at run-time to choose the appropriate Web Service. I simply renamed my one remaining web reference (right click in the Solution Explorer) from "localhost" to a common name, like "AcmecoService". I then set the "AmcecoService" object's "Url" at run-time from a value stored in "appSettings" of the web.config file.


  service = new Acmeco.AcmecoService();
  service.Timeout = 10000;
  service.Url = ConfigurationSettings.AppSettings["AcmecoWebServiceUrl"];
0
 
TeamLinkAuthor Commented:
Thanks for the suggestions - all of the comments about the app settings, and dynamic URL settings, whilst absolutely correct, are not going to help me. I need to be able to dynamically change the URL at run time and have all the functions available and working when I do this.

The latest comment, however, sounds very useful. I've moved off this project for the time being, but will investigate that when I come back to it.

I am happy to award the points on this answer at this stage as opposed to leaving the question hanging open which I know is not advised. As you have provided all the assistance so far anyway, I'm happy to give you the points!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.