5
votes

I am calling a web api service that only fails the first time I recycle the application pool. After that all calls work fine.

The process is like this..

Call Service --> return OK

Call Service --> return OK

Call Service --> return OK

Go to iis and recycle app pool ( I wait 10 secs)

Call Service --> An internal server error occurred. Please try again later.

Call Service --> return OK

Call Service --> return OK

Call Service --> return OK

... Go to iis and recycle app pool ( I wait 10 secs) Call Service --> An internal server error occurred. Please try again later.

Call Service --> return OK

Call Service --> return OK

Call Service --> return OK

...

The client is a console application framework 4.0 using this method:

        public static object Send(string webAddr, object param)
        {
            HttpUtil.IgnoreBadCertificates();

            HttpWebRequest request = null;
            object result = null;

            request = (HttpWebRequest)WebRequest.Create(webAddr);
            request.ContentType = "application/json; charset=utf-8";
            request.Method = "POST";
            request.Accept = "application/json";

            String u = GetString(a1);
            String p = GetString(a1) + GetString(a2);

            String encoded = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(u + ":" + p));
            request.Headers.Add("Authorization", "Basic " + encoded);

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                var myJsonString = Newtonsoft.Json.JsonConvert.SerializeObject(param);
                streamWriter.Write(myJsonString);
                streamWriter.Flush();
            }

            var response = (HttpWebResponse)request.GetResponse();

            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }

            return result;
        }

And the webapi is framework 4.5:

 public class CambiosController : BaseApiController
    {
        private ICambioService cambioService;

        public CambiosController(ICambioService cambioService)
        {
            this.cambioService = cambioService;
        }

        [HttpPost]
        [Authorize]
        public HttpResponseMessage Post([FromBody] CambioRequest cambioRequest)
        {
            HttpResponseMessage response = null;

            if (cambioRequest == null)
            {
                Exception ex = new ArgumentException("cambioRequest");
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }

            try
            {
                var result = this.cambioService.EnviarConfiguracion(cambioRequest);
                response = Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (Exception ex)
            {
                response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }

            return response;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.cambioService.Dispose();
            }

            base.Dispose(disposing);
        }

Unity config:

public static void RegisterComponents()
{
    LoggerFacility.Debug(string.Empty, string.Empty, "UnityConfig.RegisterComponents()");

    var container = new UnityContainer();

    container.RegisterTypes(
        AllClasses.FromAssemblies(BuildManager.GetReferencedAssemblies().Cast<Assembly>())
        .Where(x => x.Name != "IUnitOfWork" && x.Name != "IDbContext"),
        WithMappings.FromMatchingInterface, WithName.Default, overwriteExistingMappings: true);

    container.RegisterType<IUnitOfWork, UnitOfWork>(new PerResolveLifetimeManager());
    container.RegisterType<IDbContext, ControlConfigContext>(new PerResolveLifetimeManager());

    GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}

WebApi application pool is framework 4 integrated mode, running as Network Service. ProcessModel Idle Timeout (minutes 0) The only configuration changed from default. IIS 7.5

1
Hi, I did a similar case for test it, but for me all is ok. Can you add a global error handler to log more information about the error? asp.net/web-api/overview/error-handling/…Julito Avellaneda
Grrr. Your title totally asks the same question I have, but your answer is nothing like my problem. :-)Neil

1 Answers

0
votes

The problem was related to a reverse proxy we had. It was set to wait only 15 seconds for the url I was using. Therefore, when recycling the app pool (as it has to load all unity stuff) it took 16 seconds most of the time. The thing to notice is that a reverse proxy error will throw a Internal Server Error from ASP.NET webapi, noting related to Timeout or something to give some clue. Good to know now...