openrasta / openrasta-castle-windsor

Integrates Castle Windsor with OpenRasta.
1 stars 6 forks source link

OpenRasta castle windsor is not working #9

Closed JornWildt closed 8 years ago

JornWildt commented 8 years ago

I have the simplest possible setup with one handler and one resource. It works "out of the box" with OpenRasta but as soon as I start using the Castle Windsor container it fails.

The log output is:

33-[2016-10-28 17:39:34Z] Verbose(0) Starts pre-executing the request.
33-[2016-10-28 17:39:34Z] Verbose(0) Incoming host request for http://localhost/OpenRastaTest/home
33-[2016-10-28 17:39:34Z] Verbose(0) Adding communication context data
33-[2016-10-28 17:39:34Z] Warning(0) Contributor call for BootstrapperContributor had a null Action.
33-[2016-10-28 17:39:34Z] Start(1) Entering PipelineRunner: Executing contributor HttpMethodOverriderContributor.OverrideHttpVerb
33-[2016-10-28 17:39:34Z] Stop(1) Exiting PipelineRunner
33-[2016-10-28 17:39:34Z] Start(1) Entering PipelineRunner: Executing contributor UriDecoratorsContributor.ProcessDecorators
33-[2016-10-28 17:39:34Z] Stop(1) Exiting PipelineRunner
33-[2016-10-28 17:39:34Z] Start(1) Entering PipelineRunner: Executing contributor ResourceTypeResolverContributor.ResolveResource
33-[2016-10-28 17:39:34Z] Stop(1) Exiting PipelineRunner
33-[2016-10-28 17:39:34Z] Verbose(0) Rewrote path.
33-[2016-10-28 17:39:34Z] Start(1) Entering OpenRastaRewriterHandler: Rewriting to original path
    33-[2016-10-28 17:39:34Z] Start(1) Entering OpenRastaIntegratedHandler: Request for http://localhost/OpenRastaTest/home
        33-[2016-10-28 17:39:34Z] Verbose(0) Incoming host request for http://localhost/OpenRastaTest/home
        33-[2016-10-28 17:39:34Z] Verbose(0) Adding communication context data
        33-[2016-10-28 17:39:34Z] Start(1) Entering PipelineRunner: Executing contributor HandlerResolverContributor.ResolveHandler
        33-[2016-10-28 17:39:34Z] Stop(1) Exiting PipelineRunner
        33-[2016-10-28 17:39:34Z] Start(1) Entering PipelineRunner: Executing contributor OperationCreatorContributor.CreateOperations
            33-[2016-10-28 17:39:34Z] Verbose(0) Created operation named Get with signature HomeHandler::Get()
        33-[2016-10-28 17:39:34Z] Stop(1) Exiting PipelineRunner
        33-[2016-10-28 17:39:34Z] Start(1) Entering PipelineRunner: Executing contributor OperationFilterContributor.ProcessOperations
The program '[16160] w3wp.exe: Program Trace' has exited with code 0 (0x0).
The program '[16160] w3wp.exe' has exited with code 0 (0x0).

With some debugging it turns out that WindsorDependencyResolver throws exceptions when calling ResolveAllCore for IOperationFilter which in turn means no operations are found.

The output from Castle is:

Exception thrown: 'Castle.MicroKernel.Handlers.HandlerException' in Castle.Windsor.dll

Additional information: Can't create component 'bf7488c1-797e-429b-9843-751964387ea0' as it has dependencies to be satisfied.

'bf7488c1-797e-429b-9843-751964387ea0' is waiting for the following dependencies:
- Service 'System.Web.HttpContext' which was not registered.

"bf7488c1-797e-429b-9843-751964387ea0" : AspNetRequest / IRequest = ContextStoreLifetime

So for some reason it cannot get an instance of IRequest as AspNetRequest since AspNetRequest depends on System.Web.HttpContext which is not added to the container.

AspNetRequest has the following constructor:

public class AspNetRequest : IRequest
{
  string _httpMethod;

  public AspNetRequest(HttpContext context)
  {
    ...
  }
}

So it does unfortunately depend on HttpContext which is not expected to be added to the container.

But AspNetRequest is added as an instance to the container so it should in principle not be an issue. Unfortunately Windsor insists on looking for the dependencies even if the component is added as an instance.

The solution seems to be to register the instances differently in the Windsor container?

JornWildt commented 8 years ago

Here is an example showing the problem.

For a working version, exclude the IDependencyResolverAccessor implementation of the Configuration class. For a non working version, include it.

OpenRastaTest.zip

holytshirt commented 8 years ago

Hi Jorn,

Have you tried the suggestion in the readme? https://github.com/openrasta/openrasta-castle-windsor/blob/master/README.md

 //Register stubs to stop Windsor complaining 
                _windsorContainer.Register(
                    Component.For<HttpContext>().UsingFactoryMethod(() => (HttpContext)null),
                    Component.For<AspNetRequest>().UsingFactoryMethod(() => (AspNetRequest)null),
                    Component.For<AspNetResponse>().UsingFactoryMethod(() => (AspNetResponse)null));

It is due to a change in the newer version of Castle Windsor, we currently have a that work around. As we would have to rewrite the castle windsor intergration otherwise.

Here is what your config should look like: https://gist.github.com/holytshirt/bcbd85a9ce2bd41481c2ddadda672c03

Let me know it this solves you problem.

JornWildt commented 8 years ago

That looks like the thing I am missing. I'll try it ASAP and close the issue if it works.

JornWildt commented 8 years ago

Registering the stubs as sugested worked like a charm. Thanks.

serialseb commented 7 years ago

That integration must die.