我已经在我们的内联网上运行了几个星期的asp.net网站.我收到了来自我的application_error电子邮件发送方法的电子邮件,其中包含未处理的异常.
这是(我已经清理了一些路径以使其更好地显示)
例外:对象引用未设置为对象的实例.堆栈跟踪:位于System.Collections.Generic.Dictionary`2.Insert(TKey键,TValue值,布尔加法)的System.Collections.Generic.Dictionary`2.Add(TKey键,TValue值)位于TimesheetDomain.DataMappers.StaffMemberData TimesheetDomain\DataMappers\StaffMemberData.cs中的.ReadStaff(SqlDataReader reader):第362行
在TimesheetDomain\DataMappers\StaffMemberData.cs中的TimesheetDomain.DataMappers.StaffMemberData.GetStaffMember(String name):第401行
在TimesheetDomain\ServiceLayer\TimesheetManager.cs中的TimesheetDomain.ServiceLayer.TimesheetManager.GetUserFromName(String name):第199行
位于\ App_Code\UserVerification.cs中的UserVerification.GetCurrentUser():位于\ WebTimesheets\WebTimesheets.master.cs中的WebTimesheets.OnInit(EventArgs e)第29行:第159行
System.Web.UI.Page.ProcessRequestMain上的System.Web.UI.Control.InitRecursive(Control namingContainer)中的System.Web.UI.Control.InitRecursive(Control namingContainer)(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)
基本上它看起来像我的ReadStaff方法错误,它读取数据读取器来构建人员成员对象.这是一些代码:
while (reader != null && reader.Read())
{
StaffMember newMember = null;
string firstName = reader["FirstName"].ToString();
string lastName = reader["LastName"].ToString();
int staffID = (int)reader["StaffID"];
int employSection = (int)reader["EmploySection"];
StaffType employType = (StaffType)employSection;
string emailAddress = reader["EmailInt"].ToString();
int employCode = (int)reader["ibbwid"];
//check if they are an admin staff member
if (IsAdminStaff(employType))
{
newMember = new AdminOfficer(firstName, lastName, employType, staffID, emailAddress, employCode);
}
else …Run Code Online (Sandbox Code Playgroud) 我log4net在我的asp.net core 2.0应用程序中配置,如本文中提到的LINK
Program.cs中
public static void Main(string[] args)
{
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
BuildWebHost(args).Run();
}
Run Code Online (Sandbox Code Playgroud)
HomeController的
public class HomeController : Controller
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(HomeController));
public IActionResult Error()
{
log.Info("Hello logging world!");
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
Run Code Online (Sandbox Code Playgroud)
log4net.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFile" />
</root>
<appender name="RollingFile" type="log4net.Appender.FileAppender">
<file value="?C:\Temp\app.log" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d{hh:mm:ss} …Run Code Online (Sandbox Code Playgroud) 如果我编写一个接受从BaseClass(或接口)派生的参数的方法,据我所知,有两种方法可以实现:
void MyMethod<T>(T obj) where T : BaseClass { ... }
Run Code Online (Sandbox Code Playgroud)
和
void MyMethod(BaseClass obj) { ... }
Run Code Online (Sandbox Code Playgroud)
使用一个优于另一个的优点/缺点是什么?
使用-operator()进行常量模式匹配的文档说明:isexpr is constant
常量表达式的计算如下:
如果
expr和constant是整数类型,则C#相等运算符确定表达式是否返回true(即是否返回expr == constant)。否则,表达式的值由对静态
Object.Equals(expr, constant)方法的调用确定。
因此,使用此代码时
public bool IsZero(int value)
{
return value is 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望它使用==运算符(情况1)并生成以下代码:
.method public hidebysig instance bool
IsZero(
int32 'value'
) cil managed
{
.maxstack 8
ldarg.1
ldc.i4.0
ceq
ret
}
Run Code Online (Sandbox Code Playgroud)
但是,实际上,将整数参数和常量(文字)装箱以便传递给静态Object.Equals方法(情况2):
.method public hidebysig instance bool
IsZero(
int32 'value'
) cil managed
{
.maxstack 8
ldc.i4.0
box [mscorlib]System.Int32
ldarg.1
box …Run Code Online (Sandbox Code Playgroud) 假设我有类型
public class A<T> { }
Run Code Online (Sandbox Code Playgroud)
在代码中的某个地方,我想抛出与该类型的错误使用相关的异常:
throw new InvalidOperationException("Cannot use A<T> like that.");
Run Code Online (Sandbox Code Playgroud)
到目前为止,这么好,但我不想硬编码类名,所以我想我也许可以使用
throw new InvalidOperationException($"Cannot use {nameof(A<T>)} like that.");
Run Code Online (Sandbox Code Playgroud)
相反,但在这种情况下,我不知道确切的类型T.所以我想也许我可以用C++中的模板特化来做到这一点:
throw new InvalidOperationException($"Cannot use {nameof(A)} like that.");
Run Code Online (Sandbox Code Playgroud)
要么
throw new InvalidOperationException($"Cannot use {nameof(A<>)} like that.");
Run Code Online (Sandbox Code Playgroud)
但那些产量
类型参数数量不正确.
和
缺少类型参数.
我绝对不想硬编码类名,因为它可能会在以后更改.我怎样才能得到班级的名字,最好是通过nameof?
最理想的是,我想要实现的是"Cannot use A<T> like that."或"Cannot use A like that.".
这个类的方法
public class NullTester
{
public bool EqualsNull<T>(T o) where T : class
{
return o == null;
}
public bool IsNull<T>(T o) where T : class
{
return o is null;
}
public bool EqualsCall<T>(T o) where T : class
{
return object.Equals(o, null);
}
}
Run Code Online (Sandbox Code Playgroud)
.method public hidebysig
instance bool EqualsNull<class T> (
!!T o
) cil managed
{
.maxstack 8
IL_0000: ldarg.1
IL_0001: box !!T
IL_0006: ldnull
IL_0007: ceq // IMPORTANT
IL_0009: ret
} // end …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我基本上分配内存C++并将其排入队列以进行重新分配C#.此解除分配在后台运行并且是非确定性的,因此在极少数情况下,理论上可以在释放所有非托管内存之前退出应用程序.
如果是这种情况,那么行为(粗略地和非常简化)就像我的程序一样
int main()
{
Foo* = new Foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我现在的问题是
编辑: 这只是关于Windows,有些人提到这是依赖于操作系统的.
编辑2: 我不是在谈论简单地忽略我的应用程序中的所有内存泄漏,而是关于我是否需要确保在应用程序退出之前正确释放所有内存.
编辑3: 这不是关于打开文件句柄,析构函数和副作用或任何东西,这是关于将被非确定性地解除分配的内存以及在终止之前未释放内存的非常罕见的情况.
在下面Window我定义了一个触发器IsMouseOver.背景颜色正确更改,但它具有渐变效果.见下图.如何摆脱这种影响?
只有Theme.DataGrid.Row.Background.Hover从单独的样式文件,下面的代码片段移动.
<Window x:Class="MyCompany.Application.Shared.UI.Dialogs.SomeWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="#FFF3F3F7">
<Window.Resources>
<SolidColorBrush x:Key="Theme.DataGrid.Row.BorderBrush" Color="#FFF3F3F7" options:Freeze="True" />
<SolidColorBrush x:Key="Theme.DataGrid.Row.Background" Color="White" options:Freeze="True" />
<SolidColorBrush x:Key="Theme.DataGrid.Row.Background.Hover" Color="#FFAEAEB6" options:Freeze="True" />
<SolidColorBrush x:Key="Theme.DataGrid.Row.Background.Active" Color="#FF0D6AA8" options:Freeze="True" />
<SolidColorBrush x:Key="Theme.DataGrid.Row.Background.HoverSelected" Color="#FF009AD9" options:Freeze="True" />
<SolidColorBrush x:Key="Theme.DataGrid.Row.Background.Disabled" Color="#FFAEAEB6" options:Freeze="True" />
<SolidColorBrush x:Key="Theme.DataGrid.Row.Foreground.Selected" Color="White" options:Freeze="True" />
<Style x:Key="GridView.ColumnHeader.Gripper.Style" TargetType="{x:Type Thumb}">
<Setter Property="Width" Value="8" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Cursor" Value="SizeWE" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Border Background="{TemplateBinding Background}" …Run Code Online (Sandbox Code Playgroud) 我为正确“设置”我无法验证的方法以使其符合代码访问安全性准则而需要做的事情感到困惑。
给出以下方法
[MethodImpl(MethodImplOptions.ForwardRef)]
private extern void DoStuffUnverifiable();
Run Code Online (Sandbox Code Playgroud)
哪些是无法验证的PEVerify,我绝对需要将哪些属性应用于方法定义?
[SecurityCritical]? [SecuritySafeCritical]?我该如何在这两者之间做出决定?进一步,
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]吗?SecurityAction.Demand还是其他?我肯定还需要应用其他属性吗?我有什么可以申请的,尽管不是必需的?
我正在使用[NotNull]像这样的ReSharpers 注释:
public void MyMethod([NotNull] string a)
{
if(a == null) // Warning: Expression is always false.
{
throw new ArgumentNullException();
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是,由于有NotNull注释,ReSharper会警告我有关未使用的前提条件检查的信息,因为
表达总是假的
但是,据我了解,这些注释仅表示该参数永远不应为null;即,它们不禁止呼叫者通过null,例如
this.MyMethod(null);
Run Code Online (Sandbox Code Playgroud)
甚至不那么明显(更像真实代码)
string foo = null;
this.MyMethod(foo);
Run Code Online (Sandbox Code Playgroud)
因此,我认为包括对的前提条件检查确实是有道理的null,但是也许我错过了一个概念或对它的理解不正确。
为带[NotNull]注释的参数包括显式的可空性检查是否有意义?