WebApi2.0 DelegatingHandler -错误-索引超出了数组的边界

本文关键字:数组 边界 索引 DelegatingHandler 错误 WebApi2 | 更新日期: 2023-09-27 18:09:58

我有一个webapi 2.0项目。我在webapiconfig.cs注册两个委托处理程序。有时它会抛出错误"Index was out of the bounds of array",并在System.Collections.GenericList中显示静态跟踪错误。在handler1和handler2处添加(T项)。我也在使用unityconfig,我在全局的Application_Start注册。asax和我注册WebApiconfig在Application_Start在Global.asax我不知道为什么这种奇怪的行为,因为它在大多数时间工作得很好,但有时它会抛出这个错误。

我在WebApiconfig.cs中的代码如下-

public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        config.EnableCors();
        config.MessageHandlers.Add(new Handler1(););
        config.MessageHandlers.Add(new Handler2());
        // Web API routes
        config.MapHttpAttributeRoutes();
    }

两个处理程序的代码如下-

public class Handler1 : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {

        string routeTemplate = ((IHttpRouteData[])request.GetConfiguration().Routes.GetRouteData(request).Values["MS_SubRoutes"])
                                .First().Route.RouteTemplate.ToString();

        IPrincipal principal = new GenericPrincipal(new GenericIdentity(new Guid()), new string[] { "myRole" });
        HttpContext.Current.User = principal;
        return base.SendAsync(request, cancellationToken).ContinueWith(
        (task) =>
        {
            HttpResponseMessage response = task.Result;
            return response;
        }
    );
    }
    }
public class Handler2 : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        string routeTemplate = ((IHttpRouteData[])request.GetConfiguration().Routes.GetRouteData(request).Values["MS_SubRoutes"])
                                .First().Route.RouteTemplate.ToString();
        HttpConfiguration config = request.GetConfiguration();
        config.Filters.Add(new Filter1());

        return base.SendAsync(request, cancellationToken).ContinueWith(
        (task) =>
        {
            HttpResponseMessage response = task.Result;
            return response;
        }
    );
    }
}
有人能告诉我该怎么做吗?

WebApi2.0 DelegatingHandler -错误-索引超出了数组的边界

这一行看起来很可疑

string routeTemplate = ((IHttpRouteData[])request.GetConfiguration()
                               .Routes.GetRouteData(request)
                               .Values["MS_SubRoutes"])
                               .First()
                               .Route.RouteTemplate.ToString();

因为"MS_SubRoutes"的东西不可能存在,所以你需要打破代码的不同块来验证。你还需要确保集合至少有一个成员,所以你也需要控制这个问题。

var ms_SubRoutes = (IHttpRouteData[])request
    .GetConfiguration()
    .Routes
    .GetRouteData(request)
    .Values["MS_SubRoutes"];
string routeTemplate;
//Verify if "MS_SubRoutes" are part of the request
if (ms_SubRoutes != null)
{
    try
    {
        routeTemplate = ms_SubRoutes.First()
                                    .Route
                                    .RouteTemplate.ToString();
    }
    catch(IndexOutOfRangeException ior)
    {
        //Do something
        throw;
    }
    catch (Exception ex)
    {
        //Do something
        throw;
    }
}

问题是在处理程序中添加过滤器。config.Filters。添加(新Filter1 ());

在GlobalConfig中添加了过滤器,一切都运行顺利。