Silverlight WCF Service Reference Usage Simplified

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
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
Print | posted on Tuesday, February 24, 2009 6:56 PM

Feedback

# re: Silverlight WCF Service Reference Usage Simplified

left by Todd Morrison at 2/26/2009 4:19 PM 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.

# re: Silverlight WCF Service Reference Usage Simplified

left by Matt Watson at 2/26/2009 4:21 PM 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.

# re: Silverlight WCF Service Reference Usage Simplified

left by G- at 2/27/2009 2:28 PM 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

# re: Silverlight WCF Service Reference Usage Simplified

left by Matt Watson at 2/27/2009 2:50 PM Gravatar
That part of the code wasn't needed. I removed it from the example. Thanks!

# re: Silverlight WCF Service Reference Usage Simplified

left by Shane at 5/28/2009 12:23 PM 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?

# re: Silverlight WCF Service Reference Usage Simplified

left by Helen W at 6/5/2009 9:17 PM 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.

# re: Silverlight WCF Service Reference Usage Simplified

left by kolbastı at 7/26/2009 7:23 PM Gravatar
I will feed your RSS after this useful article. Thanks a lot.

# re: Silverlight WCF Service Reference Usage Simplified

left by pastavekek at 9/13/2009 3:47 PM Gravatar
I will feed your RSS after this useful article. Thanks a lot.

# re: Silverlight WCF Service Reference Usage Simplified

left by kayseri sohbet at 2/14/2010 8:29 AM Gravatar
Thanks admin

# re: Silverlight WCF Service Reference Usage Simplified

left by sağlık at 2/19/2010 7:28 PM Gravatar
a good system to protect from spam code. thanks

# sohbet izmir izmir sohbet sohbet chat izmir

left by izmir sohbet at 2/24/2010 5:57 PM Gravatar
very good health thanks to your efforts

# re: Silverlight WCF Service Reference Usage Simplified

left by Dj Ateş at 3/9/2010 3:07 PM Gravatar
Thanks you admin.

# re: Silverlight WCF Service Reference Usage Simplified

left by Morten Grøtan at 4/7/2010 5:37 AM 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.

# re: Silverlight WCF Service Reference Usage Simplified

left by oyun havaları dinle at 3/9/2011 2:39 PM Gravatar
They have a RSS feed so that regular updates can be followed
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: