Mik*_*oud 6 .net c# attributes asp.net-web-api
奇怪的是,如果我将属性从ComponentModel文件夹移回Common项目的根目录,代码工作正常.在为InflowHealth.Common.ComponentModel命名空间重构所有引用之后,我无法想象可能引用旧命名空间的内容.
这几乎就像有一些引用隐藏在一些不是基于代码而是运行时和动态的地方,但我确实在查看所有查找结果时都看不到它InflowHealthErrorContext.
有趣的是,当我注释掉使用自定义属性继承路由的行并使用默认路由时,它仍然会爆炸.更令人感兴趣的是,在重构它并将其移动到文件夹(和命名空间)之前,它正在寻找的命名空间InflowHealth.Common.InflowHealthErrorContextAttribute 实际上是旧的FQN ComponentModel.
我相信我已经确定该问题与我用来继承操作的另一个自定义属性有关.此属性添加到HttpConfiguration以下内容中:
public static void MapInheritedAttributeRoutes(this HttpConfiguration config)
{
config.MapHttpAttributeRoutes(new InheritanceDirectRouteProvider());
}
Run Code Online (Sandbox Code Playgroud)
该属性的实现非常简单:
public class InheritanceDirectRouteProvider : DefaultDirectRouteProvider
{
protected override IReadOnlyList<IDirectRouteFactory> GetActionRouteFactories(HttpActionDescriptor actionDescriptor)
{
return actionDescriptor.GetCustomAttributes<IDirectRouteFactory>(true);
}
}
Run Code Online (Sandbox Code Playgroud)
看来继承此InflowHealthErrorContext属性会导致问题,但我不确定问题究竟是什么.我试过了:
Inherited = false以便它是可继承的.AllowMultiple = true只是因为配置错误.那些没有改变错误.
我在Common一些Web API应用程序共享的程序集中有一个非常简单的属性.这很简单,我只是无法弄清楚会导致这个异常的原因.
我试图在此收集Fusion日志,但它没有记录它们.
这是Attribute:
using System;
namespace InflowHealth.Common.ComponentModel
{
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public sealed class InflowHealthErrorContextAttribute : Attribute
{
// This is a positional argument
public InflowHealthErrorContextAttribute(string errorContext)
{
ErrorContext = errorContext;
}
public string ErrorContext { get; }
}
}
Run Code Online (Sandbox Code Playgroud)
这将在路由上使用,以便稍后为过滤器内部执行的自动错误记录提供一些额外的上下文:
[Authorize(Roles = Roles.ALL_ADMINS)]
[Route("api/ControlPanelApi/PayerClassifications")]
[InflowHealthErrorContext("Error getting payer classifications.")]
public IHttpActionResult GetPayerClassifications(int clientId, bool showAllRows)
{
return Ok(GetData(payerClassificationManager, clientId, showAllRows));
}
Run Code Online (Sandbox Code Playgroud)
加载应用程序后,当注册Web API路由时,它将失败.这是它突破的路线:
GlobalConfiguration.Configure(WebApiConfig.Register);
Run Code Online (Sandbox Code Playgroud)
它抛出了这个例外:
无法从程序集"InflowHealth.Common,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null"加载类型"InflowHealth.Common.InflowHealthErrorContextAttribute".
这是堆栈跟踪:
at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
at System.Reflection.RuntimeMethodInfo.GetCustomAttributes(Type attributeType, Boolean inherit)
at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element)
at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.IsValidActionMethod(MethodInfo methodInfo)
at System.Array.FindAll[T](T[] array, Predicate`1 match)
at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem..ctor(HttpControllerDescriptor controllerDescriptor)
at System.Web.Http.Controllers.ApiControllerActionSelector.GetInternalSelector(HttpControllerDescriptor controllerDescriptor)
at System.Web.Http.Controllers.ApiControllerActionSelector.GetActionMapping(HttpControllerDescriptor controllerDescriptor)
at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider)
at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.<>c__DisplayClass4.<MapAttributeRoutes>b__1()
at System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer)
at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.<MapAttributeRoutes>b__0(HttpConfiguration config)
at System.Web.Http.HttpConfiguration.EnsureInitialized()
at System.Web.Http.GlobalConfiguration.Configure(Action`1 configurationCallback)
at InflowHealthPortal.MvcApplication.Application_Start() in Global.asax.cs:line 22
Run Code Online (Sandbox Code Playgroud)
我建议您删除bin和obj文件夹并重建项目。
当项目编译时存在一个未覆盖的杂散 dll 时,有时会发生此问题。通常在重命名输出文件时。
过去,当涉及到未反映对代码和重新编译所做的更改的引用时,执行上述操作对我很有用。
Nuget 包有时也会发生同样的情况,这将导致必须删除该包并重新安装它才能引用正确的 dll。
| 归档时间: |
|
| 查看次数: |
258 次 |
| 最近记录: |