WCF并通过Castle.Windsor注入依赖项

时间:2011-08-25 13:53:15

标签: c# wcf castle-windsor

我使用Castle.Windsor作为IoC容器,并尝试注册类似于此处描述的方式的依赖项:http://blog.ploeh.dk/CommentView,guid,f1a71969-0584-4a15-9395-9f2ac65f104b.aspx#commentstart我编写了下一个代码:

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

它给我一个关于datacontracts的错误在服务'CustomerSearchService'实现的合同列表中找不到合同名称'Riverdale.Api.DataContracts.CustomerInfoType'。我检查了属性,配置,一切都按照应有的配置。自从帖子以来,图书馆似乎已经改变了,并且知道它不是可行的方式。

更重要的是,我已经下载了WCF工具的3.0版本,并且那里的演示在我的电脑上无法正常工作:

无法加载类型'Castle.Facilities.WcfIntegration.Demo.Global'。

最佳做法是什么?我错过了什么?

2 个答案:

答案 0 :(得分:1)

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文件

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

答案 1 :(得分:0)

示例来自带有bin / output配置的垃圾。 使用Castle Windsor WCF工具的 3.0库来实现它的方法是在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();
            }
        }
    }
}

配置xml文件应包含以下内容:

<?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>