Problem with DepencyResolver when using TestServer, difference between instantiating HttpConfiguration and not instantiating

1

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

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

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

But I I'm not directly instantiating HttpConfiguration like most web examples, 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 VS where the Startup class is empty, only with OwinStartup annotation, like this:

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

And in my Global.asax.cs I 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 HttpConfiguration instance that is in GlobalConfiguration .

Because of problems getting the instance of the service in this filter through GlobalConfiguration.Configuration.DependencyResolver , I changed it to this setting. E Even before setting the% of% of the container to SimpleInjector I instantiated, it also did not work. So:

config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);

So the way I set it up works! Until in my Integration Test using HttpConfiguration with the web configuration examples I can not 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 TestServer to this instance of DependencyResolver , but it did not work.

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 directly instantiating the HttpConfiguration against not instantiating directly, as in my configuration, and because I could not get the instantiations when instantiating the HttpConfiguration like that through the Test of Integration with TestServer I am also not able? / p>     

asked by anonymous 29.10.2017 / 22:43

0 answers