webapi调用只有在使用unity DI回收应用程序池时才会失败
本文关键字:应用程序 应用 程序池 失败 DI 调用 unity webapi | 更新日期: 2023-09-27 18:07:56
我正在调用一个web api服务,该服务仅在我第一次回收应用程序池时失败。之后所有的调用都可以正常工作。
过程是这样的…
Call Service -> return OK
Call Service -> return OK
Call Service -> return OK
进入iis并回收应用程序池(我等待10秒)
Call Service ->发生内部服务器错误。请稍后再试。
Call Service -> return OK
Call Service -> return OK
Call Service -> return OK
…进入iis并回收应用程序池(我等了10秒)呼叫服务-->发生内部服务器错误。请稍后再试。
Call Service -> return OK
Call Service -> return OK
Call Service -> return OK
…
客户端是一个控制台应用程序框架4.0使用这个方法:
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;
}
webapi是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);
}
统一配置:
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应用池是framework 4集成模式,作为Network Service运行。ProcessModel Idle Timeout (minutes 0)唯一的配置从默认更改。IIS 7.5
问题与我们的反向代理有关。它被设置为只等待我使用的url 15秒。因此,当回收应用程序池(因为它必须加载所有unity内容)时,大多数情况下需要16秒。需要注意的是,反向代理错误将从ASP抛出内部服务器错误。NET webapi,注意与Timeout相关的东西给一些线索。