简体   繁体   English

WCF和通过Castle.Windsor注入依赖

[英]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: 我将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;
        }
    }

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'. 它给我关于数据合同的错误。在服务“ CustomerSearchService”实现的合同列表中找不到合同名称“ Riverdale.Api.DataContracts.CustomerInfoType”。 I checked the attributes, configs, everything is configured as it should be. 我检查了属性configs,所有内容都按应有的方式进行了配置。 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: 更重要的是,我已经下载了3.0版的WCF工具,并且其中的演示在我的本地PC上无法正常运行:

Could not load type 'Castle.Facilities.WcfIntegration.Demo.Global'. 无法加载类型“ 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 Service1.svc文件

<%@ 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. 来自的样本的问题在于垃圾箱的bin / output配置。 The way to do it with the 3.0 library of Castle windsor WCF facility is writing the next code in Global.asax : 使用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();
            }
        }
    }
}

And the configurational xml file should contain something like: 并且配置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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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