1

I have a single WCF service but, until runtime, I don't know the correct address of the service. It may be :

http://example1.com/MyService.svc
// or
http://example2.com/MyService.svc

The service is used by a class library (DAL). I have two options:

  1. Add a service reference to the service (Visula Studio 2010) and change the address at run-time. This way VS-2010 will create WSDL and other stuff for me (I'm not sure if this is even possible).
  2. Create the proxy on the fly and set the base service address. This needs more work and if I make any change to service, I need to generate WSDL myself. Maintenance of this code is not as easy as option one.

Which option to use? Also if option two is recommended by you, then should I my client wrapper class be singleton or I can create all the connection stuff on each call?

Xaqron
  • 29,931
  • 42
  • 140
  • 205

2 Answers2

2

you can point to localhost or any other address in development then in production if the url changes you simply modify the web.config or the app.config where you have configured the WCF end point.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Yup. We do this all the time with our projects. During install, a path gets set, but afterwards you can change it to anything else. As long as the interface(s) are exactly the same, you're good! :) – Tad Donaghe Feb 15 '11 at 19:11
  • Hi. The address is changing on each call (consider retreiving data from different branch offices, same contract but different addresses). I should set the address at run-time so I don't use config files. `marc_s` at this post says it's possible in `.NET 4.0` but doesn't give a clue: http://stackoverflow.com/questions/2016285/wcf-client-configuration-base-address – Xaqron Feb 15 '11 at 19:14
1

Option 1 - you get all the advantages and none of the pain. Just use something factory-oriented (i.e. Don't do new MyProxy(), but instead stick that code somewhere central like a static CreateMyProxy() method, or consider an IoC/DI container).

How to consume WCF web service through URL at run time?

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900