19

I'm working in a Windows Phone 8 project and in order to use some webservices I added a service reference with a specific URL.

My problem is the URL because it changes fom time to time so I need to let the user insert the new URL from some menu when the app is running.

I know how to change it in Visual Studio but now I need to change it in code when the app is running..so my question is: how do I change the URL in code?

I have done some search and the file "app.config" seems to do the job but I don't have any "app.config" in my project and from what I saw Windows Phone projects don't use such file.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
sparcopt
  • 416
  • 2
  • 8
  • 22
  • possible duplicate of [dynamically switch WCF web service reference URL path through config file](http://stackoverflow.com/questions/5036308/dynamically-switch-wcf-web-service-reference-url-path-through-config-file) – Cᴏʀʏ Jul 22 '13 at 15:44

1 Answers1

41

Simply change the endpoint address, e.g.

clientProxy.Endpoint.Address = new EndpointAddress(yourUri);
George Johnston
  • 31,652
  • 27
  • 127
  • 172
  • 2
    The constructor for the `clientProxy` class can also take a URL which will basically do the same thing. – Cᴏʀʏ Jul 22 '13 at 15:40
  • Actually if you want to pass the value through constructor like Cory suggested, you should make sure you pass in the right params as there are several overloads of the constructor. – Sirar Salih Oct 18 '15 at 13:26
  • If I do this, I encounter a contract mismatch exception, since the URL used when creating the hard coded service reference differs from the URL used in the constructor for the client. It seems like this method does not truly change the endpoint address? I may be missing something. – J. Schei Aug 18 '16 at 16:11
  • I would really hope that a constructor would take that in. It's something I get to do every few years and always have to look it up here... Thanks for the answer. – JP Chapleau Apr 26 '17 at 19:56