Search code examples
wcfrestcastle-windsorwcffacility

WCFFacility and WVF 4.0 REST


How do you use the Windsor-Castle WCFFacility with the WCF 4.0 REST services ?

How you you make the link to the factory, when you don't have the .svc file anymore?

TIA

Søren


Solution

  • Using Windsor 3.0 this is pretty straightforward (if I have understood your question correctly, my apologies if I am missing something).

    The simplest thing to do to show you is to create a console application and make sure you are referencing:

    • Castle.Core
    • Castle.Windsor
    • Castle.Facilities.WcfIntegration
    • System.ServiceModel
    • System.ServiceModel.Web
    • System.Runtime.Serialization

    Now define a RESTful service like this:

    [DataContract]
    public class Frob
    {
        [DataMember]
        public string Name { get; set; }
    
        [DataMember]
        public string Fribble { get; set; }
    }   
    
    [ServiceContract]
    public interface IFrobService
    {
        [OperationContract]
        [WebGet(UriTemplate = "/")]
        IEnumerable<Frob> GetAllFrobs();
    
        [OperationContract]
        [WebGet(UriTemplate = "/{name}")]
        Frob GetFrobByName(string name);
    }
    
    public class FrobService : IFrobService
    {
        private readonly List<Frob> _frobs
            = new List<Frob>
                  {
                      new Frob {Name = "Foob", Fribble = "Soop"},
                      new Frob {Name = "Hoob", Fribble = "Soop"},
                      new Frob {Name = "Doob", Fribble = "Noop"}
                  };
    
        public IEnumerable<Frob> GetAllFrobs()
        {
            return _frobs;
        }
    
        public Frob GetFrobByName(string name)
        {
            return _frobs
                .FirstOrDefault(f =>
                                f.Name.Equals(name,
                                              StringComparison.OrdinalIgnoreCase));
        }
    }
    

    Now you have that you can hook up that service into the windsor container like so (and since it is a console application, I will just show you the main method):

    public static class Program
    {
        static void Main()
        {            
            var container = new WindsorContainer();
    
            container
                .AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero)
                .Register(Component.For<IFrobService>()
                              .ImplementedBy<FrobService>()
                              .AsWcfService(new RestServiceModel("http://localhost/frobs")));
    
            Console.ReadKey();
        }
    }
    

    And that is a WCF REST service hosted by Castle Windsor.

    Pointing a browser at: "http://localhost/frobs" will get you all the frobs and pointing a browser at, say, "http://localhost/frobs/Doob" will get you the frob called Doob, you get the idea...