我正在尝试将ASP.NET Core 1.1应用程序迁移到ASP.NET Core 2.0.
该应用程序非常简单,涉及以下内容:
options.Authentication.Schemes = AuthenticationSchemes.NTLMoptions.Authentication.AllowAnonymous = true因为有些控制器不需要身份验证)[Authorize]属性进行修饰.该项目编译并启动就好了.它还提供不需要身份验证的控制器的操作.
但是,只要我使用该[Authorize]属性命中控制器,就会出现以下异常:
System.InvalidOperationException: No authenticationScheme was specified,
and there was no DefaultChallengeScheme found.
at Microsoft.AspNetCore.Authentication.AuthenticationService.<ChallengeAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.ChallengeResult.<ExecuteResultAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeResultAsync>d__19.MoveNext()
--- End …Run Code Online (Sandbox Code Playgroud) 在后台线程中,我的应用程序会定期检查网络文件夹(UNC路径)以获取应用程序更新.它会读出文件的汇编版本,如下所示:
Try
newVers = System.Reflection.AssemblyName.GetAssemblyName("\\server\app.exe").Version
Catch ex As Exception
' ignore
End Try
Run Code Online (Sandbox Code Playgroud)
这段代码经常被执行,到目前为止我总共在多个客户站点猜测超过100.000次没有问题.
有时GetAssemblyName会引发a FileNotFoundException,例如万一网络文件夹无法访问(可能发生并且必须处理).这个异常被Catch下面的块捕获,一切正常.
然而,在三个报告的案例中,GetAssemblyName电话提出了一个SEHException.奇怪的是,这个异常并没有被Catch下面的块捕获,而是被我的全局未处理异常处理程序(System.AppDomain.CurrentDomain.UnhandledException)捕获.结果,应用程序崩溃.
这是异常细节(遗憾的是,我的错误处理例程没有记录异常的ErrorCode和CanResume字段):
Caught exception: System.Runtime.InteropServices.SEHException
Message: External component has thrown an exception.
Source: mscorlib
TargetSite: System.Reflection.AssemblyName nGetFileInformation(System.String)
StackTrace:
at System.Reflection.AssemblyName.nGetFileInformation(String s)
at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)
at SyncThread.Run()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart() …Run Code Online (Sandbox Code Playgroud) 我正在使用一个选择查询和一个变量来连接查询中的一些字符串,如下所示:
DECLARE @sConcat nvarchar(max)
SET @sConcat = ''
SELECT @sConcat = @sConcat + '[' + SomeColumn + ']'
FROM SomeTable
ORDER BY SomeColumn
Run Code Online (Sandbox Code Playgroud)
在我的特殊情况下,我将FROM子句更改为派生表,它只是将整数列强制转换为 nvarchar,这样我就不必每次在串联中出现时都将其强制转换。
进行此更改后,连接的结果只是 table 中的一个值。将 CAST 移动到外部查询时,或删除ORDER BY子句时,结果符合预期。
这一切都可以用下面的 t-sql 来重现。
BEGIN TRAN
-- Create a dummy table and populate it with some values
CREATE TABLE TTest (
TTest_ID int IDENTITY(1,1) NOT NULL,
TTest_Text varchar(8) NOT NULL
CONSTRAINT PK_TTest PRIMARY KEY CLUSTERED (
TTest_Id ASC
)
) ON [PRIMARY]
INSERT INTO …Run Code Online (Sandbox Code Playgroud) Visual Studio有一个名为"Step over properties and operators(Managed only)"的调试设置.此设置非常有用,我通常打开它.
现在,每隔一段时间,项目中就会有一个属性设置器/获取器,这是相当复杂的,在调试时我希望能够进入它.有没有办法用属性装饰这个属性,以便调试器忽略上面提到的属性设置,并允许我进入它?
基本上,它应该与DebuggerStepThroughAttribute相反.
或者有另一种方法来实现这一目标吗?我现在所做的是在跳过它之前在属性getter/setter中设置一个断点,但这不是很方便,因为它要求我每次单步执行特定的代码片段时添加/删除断点.
编辑:评论建议重构.但这并不能真正回答我的问题,在我的案例中并不是必需的.通过"涉及"我并不意味着许多代码或一些资源密集型代码.在我的例子中,属性设置器触发对象内部的计算(O(1)复杂度,大约2毫秒).然而,这种计算并不那么明显,我偶尔会想要通过按下步骤键进入属性设置器.
我试图使用Reflection.Emit创建一个继承自给定类型的动态类型,并添加一个新的属性,其getter/setter调用基类型的方法.
假设我的基本类型如下所示:
class Test
{
private int _val1;
public int GetVal(int fld)
{
if (fld == 1) return _val1;
return 0;
}
public void SetVal(int fld, int val)
{
if (fld == 1) _val1 = val;
}
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个具有新属性的子类型,定义如下:
public int NewProp { get { return GetVal(1); } set { SetVal(1, value); } }
Run Code Online (Sandbox Code Playgroud)
看起来很简单.
我想出了以下内容(这是工作答案):
PropertyBuilder pbNewProp = tb.DefineProperty("NewProp", PropertyAttributes.HasDefault, typeof(int), null);
MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
// Define the "get" accessor method
MethodBuilder …Run Code Online (Sandbox Code Playgroud) .net ×3
c# ×3
asp.net-core ×1
debugging ×1
exception ×1
reflection ×1
seh ×1
sql-server ×1
t-sql ×1
vb.net ×1