Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OWIN Self-hosted mode "perwebrequest" lifecycle #638

Open
ClumsyPenguin opened this issue Oct 20, 2022 · 0 comments
Open

OWIN Self-hosted mode "perwebrequest" lifecycle #638

ClumsyPenguin opened this issue Oct 20, 2022 · 0 comments

Comments

@ClumsyPenguin
Copy link

ClumsyPenguin commented Oct 20, 2022

Hi, i am using windsor for resolving my dependencies, but i am now trying to run a web api in a console using OWIN in self-hosted mode.

Startup.cs in self-hosted solution

 public class StartupSelfHosted : AbstractStartup
{
    public void Configuration(IAppBuilder app)
    {
        var builder = new MobilePosConsoleInfrastructureBuilder();
        var container = builder.Build();
        var config = new HttpConfiguration();
        builder.OnInitialize(config);
        
        app.UseWindsorDependencyResolverScope(config, container) //this is a hack found from a nuget
             .UseWebApi(config);

        container.AddFacility<AspNetWebApiFacility>(x => x.UsingConfiguration(config).UsingSelfHosting());
       
    }
}

in the startup.cs we set up the container and tell it use webapi.

Program.cs

public static void Main(string[] args)
{
    string baseAddress = "http://+:44316/";
    WebApp.Start<StartupSelfHosted>(url: baseAddress);
    System.Console.WriteLine("API is running at {0}", baseAddress);
    System.Console.ReadLine();
}

in program.cs we spin it up in self-hosted mode. as documented here: https://learn.microsoft.com/en-us/aspnet/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

Normally we attach our httprequest to the creationcontext like this:

public class WindsorHttpControllerActivator : IHttpControllerActivator
{
    readonly IKernel _kernel;

    public WindsorHttpControllerActivator(IKernel kernel)
    {
        _kernel = kernel;
    }

    public IHttpController Create(HttpRequestMessage httpRequestMessage, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    {
        var scope = _kernel.BeginScope();
        try
        {
            return (IHttpController)_kernel.Resolve(controllerType, Arguments.FromProperties(new { httpRequestMessage }));
        }
        catch (FastCompositionException<HttpStatusCode> e)
        {
            var errorResponse = httpRequestMessage.CreateErrorResponse(e.ErrorData, e.Message, e);
            throw new HttpResponseException(errorResponse);
        }
        finally
        {
            httpRequestMessage.RegisterForDispose(scope);
        }
    }
}

The thing is it will only work with dependencyresolvers inherited from IDependencyResolver and not with IHttpControllerActivator as described here https://github.com/castleproject/Windsor/blob/master/docs/aspnetwebapi-facility.md

Later when we get a request we use SubDependencyResolvers to resolve e.g. clientkey to route the request to the correct database.

public sealed class RouteParameterResolver : ISubDependencyResolver
{
    public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
        => Resolve(context, contextHandlerResolver, model, dependency) != null;

    public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
    {
        if (!dependency.TargetType.Is<string>())
            return null;
        if (!context.AdditionalArguments.Contains("httpRequestMessage"))
            return null;
        return ((HttpRequestMessage)context.AdditionalArguments["httpRequestMessage"]).GetRouteParameterValue(dependency.DependencyKey);
    }
}

The problem is that the Creationcontext from windsor is not the same as the OwinContext how can i resolve my clientkey if it comes from the self-hosted instance? I am clueless at the moment.

@ClumsyPenguin ClumsyPenguin changed the title OWIN Self-hosted mode perwebrequest lifecycle OWIN Self-hosted mode "perwebrequest" lifecycle Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant