ayk
ayk

Reputation: 1497

Castle WCF Integration Facility

I’m trying to use Castle WCF integration facility in my project. I followed instructions from the castle official site http://docs.castleproject.org/Windsor.WCF-Facility-Registration.ashx But I couldn't make it work. Here's my code and configuration.

protected void Application_Start(object sender, EventArgs e)
{
    BuildContainer();
}

private void BuildContainer()
    {
        Container = new WindsorContainer();


        //1st
        Container.Kernel.AddFacility<WcfFacility>();
        Container.Kernel.Register(Component.For<IProductService>()
                                           .ImplementedBy<ProductService>()
                                           .Named("ProductService"));

        //2nd
        Container.AddFacility<WcfFacility>()
            .Install(Castle.Windsor.Installer.Configuration.FromXmlFile("Castle.config"));



        //3rd
        Container.Register(
                   Component.For<IProductService>()
                       .ImplementedBy<ProductService>()
                       .LifeStyle.PerWcfOperation()
                       .AsWcfService(new DefaultServiceModel()
                           .AddEndpoints(WcfEndpoint.BoundTo(new WSHttpBinding(SecurityMode.None)
                           {
                               MaxReceivedMessageSize = Int32.MaxValue,
                               MaxBufferPoolSize = Int32.MaxValue,
                               ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas
                               {
                                   MaxStringContentLength = Int32.MaxValue
                               }
                           }).At("http://localhost:1278/ProductService")
                    )
                    .AddBaseAddresses("http://localhost:1278/ProductService")
                    .PublishMetadata()
                       ));
    }

As you can see above I've tried to register my service in 3 different ways. To be clear I run only one of those 3 registration code at a time, the others are commented out. For the one which gets config from castle.config here is my castle.config :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <components>
        <component id="TestService"
                       service="IProductService"
                       type="ProductService"
               lifestyle="transient">
        </component>
    </components>
</configuration>

And lastly here is my web.config

<?xml version="1.0"?>

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

 <system.serviceModel>
  <behaviors>
   <serviceBehaviors>
    <behavior name="">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
   </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
     <services>
         <service name="WCFLibrary.ProductService">
             <endpoint name ="IProductService_Endpoint" address="http://localhost:1278/ProductService" binding="httpBinding" contract="WCFLibrary.IProductService" />
         </service>
     </services>
 </system.serviceModel>
</configuration>

Many thanks for any help...

Upvotes: 2

Views: 2971

Answers (1)

Dirk Trilsbeek
Dirk Trilsbeek

Reputation: 6023

you may be registering your services, but not to the Windsor kernel that is used in the DefaultServiceHostFactory from the Castle WcfFacility.

IMO the easiest way is to create a custom Service Host Factory, deriving from DefaultServiceHostFactory. One elegant way to register your services to the kernel before the service instance itself is created is shown here: http://blog.ploeh.dk/2010/05/18/SneakViewAtCastlesWCFFacility.aspx . You will of course have to modify your .svc files to use your custom factory class instead of the DefaultServiceHostFactory, for instance:

<%@ ServiceHost Language="C#" Service="MyService"
Factory="MyProject.MyServiceHostFactory, MyProject"  %>

In essence, you pass your prepared container to the Constructor of DefaultServiceHostFactory which will then use the container to resolve the services and their dependencies.

Upvotes: 2

Related Questions