posts - 58, comments - 59, trackbacks - 86

My Links

News

Archives

My Links

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

Print | posted on Tuesday, February 24, 2009 6:56 PM |

Feedback

Gravatar

# re: Silverlight WCF Service Reference Usage Simplified

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

# re: Silverlight WCF Service Reference Usage Simplified

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

# re: Silverlight WCF Service Reference Usage Simplified

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

# re: Silverlight WCF Service Reference Usage Simplified

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

# re: Silverlight WCF Service Reference Usage Simplified

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

# re: Silverlight WCF Service Reference Usage Simplified

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

# re: Silverlight WCF Service Reference Usage Simplified

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

# re: Silverlight WCF Service Reference Usage Simplified

I will feed your RSS after this useful article. Thanks a lot.
9/13/2009 3:47 PM | pastavekek
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: