Best way to handle such situation is to properly configure Urls in local and hosting enviornements. But if it is required to configure the webservice from code behind, i have a simple solution.
Other than that i am going to introduce you to service agent pattern. This is a very simple and generic pattern for Asynchoronous web clients like silverlgiht. This helps configure webservice in reuseable way and makes testing easier. Helps in plugable webservice, and specially in IoC/Dependecny injections.
namespace FileUploader.ServiceAgents { public interface IImageServiceAgent { void SaveImage(byte[] buffer, string fileName, EventHandlercallback); } public class ImageServiceAgent : IImageServiceAgent { private readonly ImageServiceSoapClient _proxy; public ImageServiceAgent() { Binding binding = new BasicHttpBinding(); var endpoint = new EndpointAddress( new Uri(Application.Current.Host.Source, "../Webservices/ImageService.asmx")); _proxy = new ImageServiceSoapClient(binding, endpoint); } #region IImageServiceAgent Members public void SaveImage(byte[] buffer, string fileName, EventHandler callback) { _proxy.SaveImageCompleted += callback; _proxy.SaveImageAsync(buffer,fileName); } #endregion } }
Lets start with the inteface. First line defines the IImageServiceAgent. This interface lists all the functions of webservice along with their call backs.
In the implemenations, it has service client that is using basicHttpBinding with endpoint configuraiton in such a way that it will automatically handle any enviornment given that your client and web service is hosted in the same hosting url.
Implementaion of SaveImage is simple, first it subscribes to SaveImageCompleted call back and then calls the Async client.
This is a good way as it can helps using anonymous functions in a good way. Let me know if this helps you or you have a better solution to today world coding standards.