ActionDescriptorProviderContext如何获得结果?

时间:2019-04-22 08:10:34

标签: c# asp.net-core odata

在   Microsoft.AspNetCore.OData.Routing.Conventions.DefaultODataRoutingConvention.SelectAction(RouteContext routeContext),我的Controller类未出现在IActionDescriptorCollectionProvider requiredService = routeContext.HttpContext.RequestServices.GetRequiredService(); < / b>

包含上述类的我的程序集位于同一bin文件夹中。 即使实际上有一个代码(控制器类及其方法),这也会导致“无操作匹配模板”问题。

我发现问题是由前一行 Microsoft.AspNetCore.Mvc.Infrastructure.ActionDescriptorCollection.Items 中缺少的项(这是我的控制器类)引起的。 b> routeContext.HttpContext.RequestServices.GetRequiredService();

...
      string str3 = !string.IsNullOrEmpty(str2) ? str1 + "/" + str2 : str1;
      //below is the problematic part
      IActionDescriptorCollectionProvider requiredService = routeContext.HttpContext.RequestServices.GetRequiredService<IActionDescriptorCollectionProvider>();
      List<ActionDescriptor> actionDescriptorList = new List<ActionDescriptor>();
      ActionDescriptor actionDescriptor1 = (ActionDescriptor) null;
      foreach (ControllerActionDescriptor actionDescriptor2 in requiredService.ActionDescriptors.Items.OfType<ControllerActionDescriptor>())
      {...

由于属性Items是在 ActionDescriptorCollection 的构造函数中分配的,该构造函数接收参数 IReadOnlyList项目,因此我也尝试查找该构造函数的调用方式。

namespace Microsoft.AspNetCore.Mvc.Infrastructure
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;

    public class ActionDescriptorCollection
    {
        public ActionDescriptorCollection(IReadOnlyList<ActionDescriptor> items, int version)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }
            this.<Items>k__BackingField = items;
            this.<Version>k__BackingField = version;
        }

基于堆栈跟踪,由 Microsoft.AspNetCore.Mvc.Internal.ActionDescriptorCollectionProvider 中的方法 UpdateCollection()在行 _collection =新的ActionDescriptorCollection(                 新的ReadOnlyCollection( context.Results ),                 Interlocked.Increment(ref _version));

namespace Microsoft.AspNetCore.Mvc.Internal
{
    /// <summary>
    /// Default implementation of <see cref="IActionDescriptorCollectionProvider"/>.
    /// </summary>
    public class ActionDescriptorCollectionProvider : IActionDescriptorCollectionProvider
    {
...
        private void UpdateCollection()
        {
            var context = new ActionDescriptorProviderContext();

            for (var i = 0; i < _actionDescriptorProviders.Length; i++)
            {
                _actionDescriptorProviders[i].OnProvidersExecuting(context);
            }

            for (var i = _actionDescriptorProviders.Length - 1; i >= 0; i--)
            {
                _actionDescriptorProviders[i].OnProvidersExecuted(context);
            }

            _collection = new ActionDescriptorCollection(
                new ReadOnlyCollection<ActionDescriptor>(context.Results),
                Interlocked.Increment(ref _version));
        }
    }
}

context.Results 是ActionDescriptorProviderContext.Results

namespace Microsoft.AspNetCore.Mvc.Abstractions
{
    public class ActionDescriptorProviderContext
    {
        public IList<ActionDescriptor> Results
        {
            [CompilerGenerated]
            get
            {
                return this.<Results>k__BackingField;
            }
        }

        public ActionDescriptorProviderContext()
        {
            this.<Results>k__BackingField = new List<ActionDescriptor>();
            base..ctor();
        }
    }
}

从这一点来看,如何用结果填充结果? 我希望可以从bin文件夹中的程序集中看到该类。

0 个答案:

没有答案