mardi 29 juin 2021

Web API slow performance

I am working on Web API project under .NET Framework 4.6 currently. It uses bearer token authentication. But I have noticed the issue with response time of controllers' actions. The response time is quite big even Web API is hosted on local IIS Express. Namely the logging (based on IActionFilter) shows the execution time of the controller is 20 milliseconds, meanwhile Postman shows the response time is about 3 or 4 seconds. What can be the reason of such difference ?

Two steps were taken:

  1. to use the extension method SuppressDefaultHostAuthentication in order to avoid possible side effect from a default authentication. No improvements unfortunately.
  2. to add the dependency injection the default implementation of interfaces which were missing initially and respective exceptions were thrown on Web API start. Namely I have added
    .RegisterType<IHttpControllerSelector, DefaultHttpControllerSelector>() .RegisterType<IHttpActionSelector, ApiControllerActionSelector>(). No improvements unfortunately.

Thanks in advance for any help or advice.

Please find below the content of WebApiConfig.cs and Startup.cs files

WebApiConfig.cs

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        config.MapHttpAttributeRoutes();
        //config.SuppressDefaultHostAuthentication();

        // TODO: check the necessity to use Storages here, why not on services level

        var container = new UnityContainer();

/some dependecies mapping here/

        container.AddExtension(new Diagnostic());

        config.DependencyResolver = new UnityResolver(container);

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }    
        );

        config.Filters.Add(new ApiAuthenticationFilter(container.Resolve<BLI.IUserSessionManagement>()));

        config.Filters.Add(new ApiAuthorizationFilter(container.Resolve<BLI.IAuthorizer>(), container.Resolve<BET.IAuthLogger>()));

        config.Filters.Add(new LoggingFilterAttribute(new BET.ControllerTracer()));

}

Startup.cs file

    public void Configuration(IAppBuilder app)
    {
        //TODO : try to find better solution

        BackEnd.WebAPI.Models.UnityResolver ur = (BackEnd.WebAPI.Models.UnityResolver)System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver;

        Type providerType = Type.GetType("Microsoft.Owin.Security.OAuth.IOAuthAuthorizationServerProvider, Microsoft.Owin.Security.OAuth", true);

        ApiOAuthAuthorizationServerProvider serverProvider = (ApiOAuthAuthorizationServerProvider)ur.GetService(providerType);

        //

        OAuthAuthorizationServerOptions oAuthOptions = new OAuthAuthorizationServerOptions()
        {
            TokenEndpointPath = new PathString("/auth"),
            Provider = serverProvider,
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
            AllowInsecureHttp = true
        };

        app.UseOAuthAuthorizationServer(oAuthOptions);            
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());            
    }



Aucun commentaire:

Enregistrer un commentaire