2

プロジェクトで Castle WCF 統合機能を使用しようとしています。城の公式サイトhttp://docs.castleproject.org/Windsor.WCF-Facility-Registration.ashxの指示に従いましたが、 うまくいきませんでした。これが私のコードと構成です。

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 つの登録コードのうち 1 つだけを実行し、他のコードはコメントアウトしています。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>

助けてくれてありがとう...

4

1 に答える 1

2

you may be registering your services, but not to the Windsor kernel that is used in the DefaultServiceHostFactory from the Castle WcfFacility.

IMO the easiest way is to create a custom Service Host Factory, deriving from DefaultServiceHostFactory. One elegant way to register your services to the kernel before the service instance itself is created is shown here: http://blog.ploeh.dk/2010/05/18/SneakViewAtCastlesWCFFacility.aspx . You will of course have to modify your .svc files to use your custom factory class instead of the DefaultServiceHostFactory, for instance:

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

In essence, you pass your prepared container to the Constructor of DefaultServiceHostFactory which will then use the container to resolve the services and their dependencies.

于 2012-09-15T17:22:45.673 に答える