Depencyresolver problem when using Testserver, difference between instantiating Httpconfiguration and not instantiating

Asked

Viewed 32 times

1

I have a simple authorization filter that to get an instance of a service I require in GlobalConfiguration.Configuration.DependencyResolver.
Thus:

var service = GlobalConfiguration.Configuration
    .DependencyResolver.GetService(typeof(UserService)) as UserService;

Running the Web API 2 application to manually test works, the instance is obtained.

But I nay I am instantiating directly the HttpConfiguration like most examples on the web, which are similar to this:

public void Configuration(IAppBuilder app)
{
    var config = new HttpConfiguration();
    ...
    config.MapHttpAttributeRoutes();
    ...
    app.UseWebApi(config);
}

Instead I’m using a template that was delivered to me by the VS where the class Startup is empty, only with the annotation of OwinStartup, thus:

[assembly: OwinStartup(typeof(SampleAPI.WebApi.Startup))]
namespace SampleAPI.WebApi
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
        }
    }
}

And in my Global.asax.cs have:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ValidatorOptions.LanguageManager.Culture = new CultureInfo("pt-BR");

        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

That is, it works when I use the instance of HttpConfiguration who is in GlobalConfiguration.

Due to problems picking up the service instance in this filter through the GlobalConfiguration.Configuration.DependencyResolver is that I switched to this configuration.
And even before setting the container of SimpleInjector in the HttpConfiguration that I instituted, it didn’t work either. So:

config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);

So the way I set it up works! Until in my Integration Test using the TestServer with the web configuration examples I cannot get the service instance:

internal class OwinTestConf
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        WebApiConfig.Register(config);
        app.UseWebApi(config);
    }
}

I’ve also tried passing the DependencyResolver to that instance of HttpConfiguration, but it didn’t help.

internal class OwinTestConf
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        WebApiConfig.Register(config);
        config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver();
        app.UseWebApi(config);
    }
}

What is the difference between instantiating directly the HttpConfiguration against not instantiating directly, as in my configuration, and because I could not get the instances by instantiating Httpconfiguration as that by the Testserver Integration Test I am also not getting?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.