2012-09-15 107 views
2

我想在我的项目中使用城堡WCF集成设施。我遵循城堡官方网站http://docs.castleproject.org/Windsor.WCF-Facility-Registration.ashx 的指示,但我无法使它工作。这是我的代码和配置。城堡WCF集成设施

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() 
         )); 
    } 

正如您在上面看到的,我尝试以3种不同的方式注册我的服务。为了清楚起见,我一次只运行这3个注册码中的一个,其他注释则被注释掉。对于一个从castle.config这里得到的配置是我castle.config:

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

最后一点,这里是我的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> 

任何帮助,非常感谢......

回答

2

您可能正在注册您的服务,但不适用于Castle WcfFacility的DefaultServiceHostFactory中使用的Windsor内核。

IMO最简单的方法是创建一个派生自DefaultServiceHostFactory的自定义服务主机工厂。在服务实例本身创建之前,将服务注册到内核的一种优雅方式如下所示:http://blog.ploeh.dk/2010/05/18/SneakViewAtCastlesWCFFacility.aspx。当然,你将不得不修改您的.svc文件,使用自定义工厂类,而不是DefaultServiceHostFactory的,例如:

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

在本质上,你通过准备好的容器中,以DefaultServiceHostFactory的构造,然后将使用容器来解析服务及其依赖关系。

相关问题