2011-08-25 28 views
0

注入依賴我用Castle.Windsor作爲IoC容器,並試圖註冊這裏描述的類似的方式依賴關係:http://blog.ploeh.dk/CommentView,guid,f1a71969-0584-4a15-9395-9f2ac65f104b.aspx#commentstart我寫了下面的代碼:WCF並通過Castle.Windsor

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

它給我有關數據合同的錯誤無法在由服務「CustomerSearchService」實現的合同列表中找到合同名稱「Riverdale.Api.DataContracts.CustomerInfoType」。我檢查了屬性,配置,所有配置都應該如此。看起來圖書館已經考慮過自從這篇文章後發生的變化,並且知道這不是要走的路。

更重要的,我已經下載了3.0版本的WCF設施,並在那裏演示我的電腦上無法正常工作本地話說:

未能加載類型「Castle.Facilities.WcfIntegration.Demo 。全球'。

這樣做的最佳做法是什麼?我錯過了什麼?

回答

0

來自樣本的問題是垃圾桶和輸出配置。 的方式與做到這一點的3.0庫溫莎城堡WCF設施的是寫在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> 
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" %>