Geeks With Blogs
Matt Watson Software developer, product visionary, and master of #dadops
One of the biggest problems with using a WCF service in Silverlight is resolving the service itself in the correct place at all times. When moving code from your dev machine to staging and to production you may run in to issues. Using SSL and not using SSL can also be issues. So we came up with this simple solution to a complex problem.

Credit goes to Jeff Wolfer on our team for creating this.

Here is how the solution is actually used. It uses generics:
        private SLGlobalCustomer.SLGlobalCustomerClient client = SilverlightBaseLibrary.SLLocation.GetClient<SLGlobalCustomer.SLGlobalCustomerClient>("SLCustomer.svc");

            client.SaveCustomersCompleted += new EventHandler<GlobalCustomerEdit.SLGlobalCustomer.SaveCustomersCompletedEventArgs>(client_SaveCustomersCompleted);
            client.LoadCustomersCompleted += new EventHandler<GlobalCustomerEdit.SLGlobalCustomer.LoadCustomersCompletedEventArgs>(client_LoadCustomersCompleted);
            client.LoadCustomersAsync(AutoLeadID);



using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;

namespace SilverlightBaseLibrary
{
    public static class SLLocation
    {
        public static Uri SilverlightLocation
        {
            get
            {
                return System.Windows.Application.Current.Host.Source;
            }

        }

        public static Uri RootURL
        {
            get
            {
                return new Uri(SilverlightLocation, @"../");
            }
        }

        public static Uri ServiceURL
        {
            get
            {
                return new Uri(RootURL, "Services/");
            }
        }


        public  static BasicHttpBinding WCFHttpBinding(long? MaxReceivedMessageSize)
        {
                BasicHttpBinding ret = new BasicHttpBinding();

                //had to wrap this in a try catch because Expression Blend would throw a fit over this at design time if
                //referencing GetClient in the constructor (not in a method basically)
                try
                {
                    if (System.Windows.Browser.HtmlPage.Document.DocumentUri.Scheme.ToLower() == "https")
                    {
                        ret.Security.Mode = BasicHttpSecurityMode.Transport;
                    }
                    else
                    {
                        ret.Security.Mode = BasicHttpSecurityMode.None;
                    }
                    ret.MaxReceivedMessageSize = MaxReceivedMessageSize ?? 65536;//65536 is default
                }
                catch { }
                return ret;
        }

        private static System.ServiceModel.EndpointAddress GetEndpoint(string serviceName)
        {
            Uri uri = new Uri(SLLocation.ServiceURL + serviceName);
            return new System.ServiceModel.EndpointAddress(uri.ToString());
        }

        public static T GetClient<T>(string serviceName) where T : class
        {
            return GetClient<T>(serviceName, null);
        }

        public static T GetClient<T>(string serviceName, long? MaxReceivedMessageSize) where T : class
        {
            object[] p = new object[] { WCFHttpBinding(MaxReceivedMessageSize), GetEndpoint(serviceName) };
            return System.Activator.CreateInstance(typeof(T), p) as T;

        }
    }
}

Matt Watson
VinSolutions
Automotive CRM Software Posted on Tuesday, February 24, 2009 6:56 PM | Back to top


Comments on this post: Silverlight WCF Service Reference Usage Simplified

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
Wooooh,
what happened to replacing the URL in the ClientConfig?
You could get CruiseControl or TeamBuild to drop in a new config based on the deployment Plan.
Left by Todd Morrison on Feb 26, 2009 4:19 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
Yeah but doing it this way eliminate the config file. This overrides it!

When you have one XAP it isn't a big deal. I've got like 20 of them in one application and growing.
Left by Matt Watson on Feb 26, 2009 4:21 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
Hi, thanks for the post! I am trying your code but I get an "The name 'InitParams' does not exist in the current context" in the UploadHandler static property.

I got here from Manish's blog (http://weblogs.asp.net/manishdalal/archive/2009/02/23/silverlight-servicereferences-clientconfig-alternatives.aspx). I'm still looking for a solution. Do you know if the web server configuration could be preventing my SL app from contacting the service? Thank you for posting.

Gerardo
Left by G- on Feb 27, 2009 2:28 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
That part of the code wasn't needed. I removed it from the example. Thanks!
Left by Matt Watson on Feb 27, 2009 2:50 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
This seems like it would only work if your services were hosted in the same place as your application. What if you host your services elsewhere?
Left by Shane on May 28, 2009 12:23 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
Another advantage to your scheme is that you do not have to expose your service in a simple text file in the .xap. In my case I am obfuscating any assembly in my .xap, and as no other use but my apps should call my services, I am much happier to have them obfuscated with the rest of my code.
Left by Helen W on Jun 05, 2009 9:17 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
I will feed your RSS after this useful article. Thanks a lot.
Left by kolbastı on Jul 26, 2009 7:23 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
I will feed your RSS after this useful article. Thanks a lot.
Left by pastavekek on Sep 13, 2009 3:47 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
Thanks admin
Left by kayseri sohbet on Feb 14, 2010 8:29 AM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
a good system to protect from spam code. thanks
Left by sağlık on Feb 19, 2010 7:28 PM

# sohbet izmir izmir sohbet sohbet chat izmir
Requesting Gravatar...
very good health thanks to your efforts
Left by izmir sohbet on Feb 24, 2010 5:57 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
Thanks you admin.
Left by Dj Ateş on Mar 09, 2010 3:07 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
Have you actually gotten this to work on an https-enabled web site?
I've followed your example, in combination with Manish Dalal's example web.config using "basicHttpBinding", but it won't work.

For plain http, it works flawlessly, but for https, it gives the dreaded "Not Found" error.
Left by Morten Grøtan on Apr 07, 2010 5:37 AM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
They have a RSS feed so that regular updates can be followed
Left by oyun havaları dinle on Mar 09, 2011 2:39 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
thank you admin really good sites
Left by arsız bela on Nov 15, 2012 3:15 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
thank yok really nice sites by arsiz
Left by arsız bela on Nov 15, 2012 3:16 PM

# re: Silverlight WCF Service Reference Usage Simplified
Requesting Gravatar...
thanks your admin
Left by Tv İzle on Jan 02, 2013 2:47 PM

Your comment:
 (will show your gravatar)


Copyright © Matt Watson | Powered by: GeeksWithBlogs.net