简体   繁体   中英

WCF and injecting dependency via Castle.Windsor

I use Castle.Windsor as a IoC container, and tried to register the dependencies similar to the way described here: http://blog.ploeh.dk/CommentView,guid,f1a71969-0584-4a15-9395-9f2ac65f104b.aspx#commentstart I wrote the next code:

   public class RiverdaleServiceHostfactory : DefaultServiceHostFactory
    {
        public RiverdaleServiceHostfactory()
            : base(CreateKernel())
        {
        }
        private static IKernel CreateKernel()
        {
            InversionOfControl.RegisterAll();
            InversionOfControl.Container.AddFacility<WcfFacility>();
            return InversionOfControl.Container.Kernel;
        }
    }

It gives me a mistake about datacontracts The contract name 'Riverdale.Api.DataContracts.CustomerInfoType' could not be found in the list of contracts implemented by the service 'CustomerSearchService'. I checked the attributes, configs, everything is configured as it should be. It seems the library has considerbaly changed since the post and know it is not the way to go.

More than that, I've downloaded the 3.0 version of WCF facility and the demo in there doesn't work on my PC locally saying:

Could not load type 'Castle.Facilities.WcfIntegration.Demo.Global'.

What is the best practice to do it? What am I missing?

new WindsorContainer()
                .AddFacility<WcfFacility>()
                .Register(
                    Component.For<IServiceBehavior>().Instance(metadata),
                    Component.For<IServiceBehavior>().Instance(debug),
                    Component
                        .For<IService1>()
                        .ImplementedBy<Service1>()
                        .Interceptors(Castle.Core.InterceptorReference.ForType<ServiceInterceptor>()).Anywhere
                        .Named("service1")
                        .LifeStyle.Transient
                        .AsWcfService(new DefaultServiceModel().Hosted()
                            .AddEndpoints(
                                WcfEndpoint.BoundTo(new BasicHttpBinding()),
                                WcfEndpoint.BoundTo(new WSHttpBinding(SecurityMode.None)).At("ws")
                                ))


                );
        }

Service1.svc file

<%@ ServiceHost Language="C#" Debug="true"  Service="service1" 
 Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory,
 Castle.Facilities.WcfIntegration"  %>

The problem with sample from was in the garbage with bin/output configuration. The way to do it with the 3.0 library of Castle windsor WCF facility is writing the next code in Global.asax :

using System;
using System.ServiceModel.Description;
using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Installer;


namespace Riverdale.Web
{
    public class Global : System.Web.HttpApplication
    {
        private static IWindsorContainer _container;
        protected void Application_Start(object sender, EventArgs e)
        {
            var returnFaults = new ServiceDebugBehavior
                                   {
                                       IncludeExceptionDetailInFaults = true,
                                       HttpHelpPageEnabled = true
                                   };

            var metadata = new ServiceMetadataBehavior { HttpGetEnabled = true };

            InversionOfControl.RegisterAll();
            InversionOfControl.Container
                .AddFacility<WcfFacility>()
                .Install(Configuration.FromXmlFile("SearchCustomerWindsorConfig.xml"))
                .Register(
                    Component.For<IServiceBehavior>().Instance(returnFaults),
                    Component.For<IServiceBehavior>().Instance(metadata));
            _container = InversionOfControl.Container;
        }

        protected void Application_End(object sender, EventArgs e)
        {
            if (_container != null)
            {
                _container.Dispose();
            }
        }
    }
}

And the configurational xml file should contain something like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <components>
    <component id="CustomerSearchService"
                   service="Riverdale.Api.ICustomerSearchService, Riverdale.Api"
                   type="Riverdale.Api.CustomerSearchService, Riverdale.Api">
    </component>
  </components>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM