WCF

RESTFul Services in Silverlight - CT Code Camp June 2009

Downloade the Presentation and Code 

The Connecticut User Group organized a full day code camp event in Bloomfield, CT. The topic I presented on was “RESTFul Services and Silverlight”. The presentation was organized around the following main topics:

  • Web Services via SOAP
  • RESTFul Services
  • Creating a WCF RESTFul Service
  • Consuming a Custom WCF RESTFul Service in Silverlight

The attached slide deck will provide you with highlights on each topic. The code samples revolve around the AdventureWorks Database. I have partitioned the code into Data Access, Service Layer and a UI Layer.

  • Database: AdventureWorksDB from code plex. You can download it from here: http://www.codeplex.com/MSFTDBProdSamples
  • Data Access: Used Nettiers and Codesmith to Generate the CRUD methods to access Adventure Works.
  • Service Layer: WCF, ASMX and WCF REST service examples. I have also included a sample service from John Papa’s Data Services with Silverlight book. I would recommend this book for who ever is working with Siverlight
  • UI Layer: Consists of a ASP.NET web site that houses the silverlight controls and the silverlight controls themselves. The services are conusmed by silverlight.

During the actual presentation, i also created a simple WCF Service and demo’ed how to use a simple SOAP WCF service in a silverlight client using the Asynchronous pattern.

Check the samples out!

Happy coding

Reddy (reddy.kadasani@tallan.com)

.NET Framework
Presentations
User Groups
WCF

Comments (0)

Permalink

Recover from a WCF Service Fault, Part 2 (Generic ServiceClientFactory Class)

After finding the simple solution for handling WCF Service Faults in the original post, I figured it should be relatively trivial to find a generic solution to this problem if you’re using similar WCF clients in a project and want to reset ALL of them on a channel fault.  I developed a generic ServiceClientFactory class that will generate a WCF Service Client instance from a generic “GetClient” function and will automatically handle resetting of faulted channels.

Class Implementation Source Code:

/* Service Client Factory
 * Author: Michael Gerety, Senior Consultant, Tallan, Inc.
 * Description: A generic service client factory that automatically
 *              resets faulted channels for WCF services that have
 *              endpoints and behaviors defined in web/app.config files.
 */             

using System;
using System.Collections.Generic;
using System.ServiceModel;
namespace Tallan
{
    /// <summary>
    /// Singleton factory class for WCF Services.
    /// Creates service clients based on interface type and automatically
    /// resets faulted channels.
    /// </summary>
    public class ServiceClientFactory
    {
        private readonly Dictionary<Type, object> factories;
        private static ServiceClientFactory instance;

        private ServiceClientFactory()
        {
            factories = new Dictionary<Type, object>();
        }

        /// <summary>
        /// Retrieves a service client for the interface specified in generic parameter.
        /// </summary>
        /// <typeparam name="T">Interface type to use for Service Client creation.</typeparam>
        /// <returns>Service client instance for specified interface.</returns>
        public T GetClient<T>()
        {
            var genericType = typeof(T);
            Type serviceClientType;
            if (genericType.IsInterface)
            {
                serviceClientType = GetClientType(genericType);

                if (serviceClientType == null)
                    return default(T);

                var client = Activator.CreateInstance(serviceClientType);
                if (!(client is ICommunicationObject))
                {
                    client = null;
                    return (T)client;

                }
                (client as ICommunicationObject).Faulted += Channel_Faulted<T>;

                if (!factories.ContainsKey(typeof(T)))
                {
                    var prop = serviceClientType.GetProperty("ChannelFactory");
                    var factory = prop.GetValue(client, null);
                    factories.Add(typeof(T), factory);
                }
                return (T)client;
            }
            return default(T);
        }

        #region Reflection Utilities
        private static Type GetClientType(Type type)
        {
            var assy = type.Assembly;
            var serviceModelAssy = typeof(ChannelFactory).Assembly;
            var clientBaseType = serviceModelAssy.GetType("System.ServiceModel.ClientBase`1").MakeGenericType(type);

            foreach (var classType in assy.GetTypes())
            {
                if (classType.IsClass && type.IsAssignableFrom(classType))
                {
                    if (classType.IsSubclassOf(clientBaseType))
                        return classType;
                }
            }

            return null;
        }

        #endregion

        /// <summary>
        /// Event handler for ClientBase.Faulted event.
        /// </summary>
        /// <typeparam name="T">Interface type of service</typeparam>
        /// <param name="sender">ClientBase instance</param>
        /// <param name="e">Event Args</param>
        private void Channel_Faulted<T>(object sender, EventArgs e)
        {
            ((ICommunicationObject)sender).Abort();
            var factory = (ChannelFactory<T>)factories[typeof(T)];
            factory.CreateChannel();
        }

        /// <summary>
        /// Returns the singleton instance of ServiceClientFactory.
        /// </summary>
        /// <returns>Singleton instance of ServiceClientFactory</returns>
        public static ServiceClientFactory GetFactory()
        {
            if (instance == null)
            {
                instance = new ServiceClientFactory();
            }
            return instance;
        }
    }
}

Sample Usage:

//Get instance of ServiceClientFactory
            var factory = ServiceClientFactory.GetFactory();
            var client = factory.GetClient<IAuthenticateService>();
            try
            {
                client.AuthenticateUser("joe", "bob");
            }
            catch (Exception)
            {
                //Handle Exception
            }

            //channel isn't in faulted state, you can re-execute.
            client.AuthenticateUser("joe", "bob1");

This was thrown together and proven to work for my purposes.  If anyone has any suggestions about how to improve this utility class, please feel free to comment or contact me with suggestions.

.NET Framework
WCF

Comments (2)

Permalink

Debugging Windows Services in the Visual Studio IDE

In one of our current projects we made  the decision to move our WCF services from an IIS hosted environment into a Window Service hosted environment.  This move gave us greater flexibility in mangement and distribution of our WCF services, and allowed us to use multiple endpoints (http, net.tcp, pipes) without the need for Windows Activation Services (WAS) which would have required that our clients be running Vista and/or Server 2008.

One of the major pains about working with developing Windows Services, however, is that you can’t start a windows service project from within the debugger.  You have to instead install and start the service to execute it.

I have a workaround for this that allows us to test our WCF services from within the IDE without installing the service.

Steps:

1. In the Windows Service, put any code you’d normally put in the OnStart and OnStop events into their own functions:

private void OpenWCFServices()
{
    householdHost.Open();
}

private void CloseWCFServices()
{
    householdHost.Close();

}
protected override void OnStart(string[] args)
{
    OpenWCFServices();
}

protected override void OnStop()
{
    CloseWCFServices();
}

2. Using precompiler directives, specify 2 public functions that also call your start and stop methods.  This allows these methods to be publicly available at debug time only (you’ll see why in Step 3).

#if DEBUG
        public void DebugStart()
        {
            OpenWCFServices();
        }

        public void DebugStop()
        {
            CloseWCFServices();
        }
#endif

3. Create a new Windows Form in your service project.  Alter the constructor so that it requires an instance of your service to be created.  Add a Button to stop your WCF services to the form.

    public partial class DebugForm : Form
    {
        private FEEAApplicationServiceHost service;
        public DebugForm(FEEAApplicationServiceHost service)
        {
            InitializeComponent();
            this.service = service;
            this.service.DebugStart();
        }

        private void ExitButton_Click(object sender, EventArgs e)
        {
#if DEBUG
            service.DebugStop();
#endif
        }
    }

 

4. Alter your Program.cs to use this form as your running application instead of the Service using the same #if DEBUG directives.

       /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {

#if DEBUG
            Application.EnableVisualStyles();
            Application.Run(new DebugForm(new FEEAApplicationServiceHost()));
#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new FEEAApplicationServiceHost()
            };
            ServiceBase.Run(ServicesToRun);
#endif

        }
    }

This allows you to launch your service from the IDE and debug during development, while still having the ability to compile and distribute your service without changing a line of code.  (Although I’d probably recommend removing this code from anything that is truly production-level.

So now, when you right click your service project and select Debug –gt; Start new instance, you see the following form:

 

image

Once this is up, you can start an instance of your code that accesses the service, and you’re all set.

.NET Framework
WCF

Comments (0)

Permalink