miércoles, julio 30, 2008

No More Need to Change Your URIs

It's a pain having to remember to edit your ServiceReferences.ClientConfig (the file with the service proxies configuration) or editing your code to point to another path when changing between developing, testing and production environments. Well, it's time to stop the pain:

   1:              Uri uri = HtmlPage.Document.DocumentUri;
2: return String.Format("{0}://{1}:{2}/", uri.Scheme, uri.Host, uri.Port);

It would be better to encapsulate this in a class:

   1:  public static class UriHelper
2: {
3: public static string GetHost()
4: {
5: if(!HtmlPage.IsEnabled) // Get design time URI
6: return http://localhost:1650/;
7: Uri uri = HtmlPage.Document.DocumentUri;
8: return String.Format("{0}://{1}:{2}/", uri.Scheme, uri.Host, uri.Port);
9: }
10: }

I suggest you to update your proxies, so instead of using the URL defined in your ServiceReferences file it will use the URI provided by your method:

   1:  public AuthenticationServiceClient()
2: : base("BasicHttpBinding_AuthenticationService", UriHelper.GetHost() + "B2B2/Cotizador/AuthenticationService.svc"){}

It will be useful for pictures too:

   1:              string uri = String.Format("{0}/Images/{1}.jpg", UriHelper.GetHost(), productId);
2: image.ImageSource = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));

Hope this helps.
Don't forget to vote for your favorite topics.
I format my code using Manoli's website

No hay comentarios.: