gpt4 book ai didi

c# - AccessViolationException 未处理

转载 作者:可可西里 更新时间:2023-11-01 09:15:37 26 4
gpt4 key购买 nike

我正在尝试使用 Steve Sanderson's blog post为了在我的 ASP MVC 3 View 中编辑可变长度列表。该项目构建良好,但是每当呈现局部 View 时,程序就会在 using(Html.BeginColletionItem() 行上爆炸并出现此错误:

AccessViolationException was unhandled
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

这是完整异常的屏幕截图

enter image description here

下面是完整的堆栈跟踪

at Microsoft.VisualStudio.WebHost.Host.ProcessRequest(Connection conn)
at Microsoft.VisualStudio.WebHost.Server.OnSocketAccept(Object acceptedSocket)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

局部 View

@model Monet.Models.AgentRelationshipCodes


@using (Html.BeginCollectionItem("AgentRelationshipCodes")) @*Exception thrown here*@
{
<tr>
<td>@Html.EditorFor(model => model.EffectiveDate, "NullableDate", new { @class = "relCodeDate2" })</td>
<td>@Html.EditorFor(model => model.RelationshipId, "NullableDate", new { @class = "relDistCode1", maxlength = 3 })</td>
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.RelCodeOrdinal)
</tr>
}

查看

    <script>
$(document).ready(function() {
$(".addCode").click(function () {
$.ajax({
url: '@Url.Action("NewRelationshipCode", "AgentTransmission")',
dataType: 'html',
cache: false,
success: function (html) {
console.log(html);
$("#Experiment > tbody").append(html);
}
})
});
});
</script>
.
.
<fieldset>
<legend>Relationship Codes</legend>
<table id="Experiment">
<thead>
<tr>
<th>Relationship Effective Date</th>
<th>Relationship Dist Code</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.AgentRelationshipCodes)
{
@Html.Partial("AddRelationshipCodePartial", item)
}
</tbody>
</table>
<br/>
<a href="javascript:void(0)" class ="addCode">Add Another</a>
</fieldset>

Controller

    [HandleProcessCorruptedStateExceptions]
public ViewResult NewRelationshipCode()
{
return View("AddRelationshipCodePartial", new AgentRelationshipCodes());
}

代理关系代码

namespace Monet.Models
{
using System;
using System.Collections.Generic;

public partial class AgentRelationshipCodes
{
public int ID { get; set; }
public int RelCodeOrdinal { get; set; }
public string RelationshipId { get; set; }
public Nullable<System.DateTime> EffectiveDate { get; set; }
public System.DateTime LastChangeDate { get; set; }
public string LastChangeId { get; set; }

public virtual AgentTransmission AgentTransmission { get; set; }
}
}

编辑

我已经能够让演示在我现在使用的解决方案之外的项目中运行,所以它显然与这个工作区中的一些 dll 有关。然而,现在我的薪水已经超过了我的薪水等级,因为我不确定如何调试这样的东西。以下是 WinDbg 在 Visual Studio 抛出 AccessViolationException 之前识别的异常。在抛出的异常之间有很多信息,如果有人需要,请告诉我。

*** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\d12f4fda3d1bfabf888342e96983e9a7\mscorlib.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\d12f4fda3d1bfabf888342e96983e9a7\mscorlib.ni.dll

*** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Xaml\9d3572e8c3c314a0f12383d41e8bee78\System.Xaml.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Xaml\9d3572e8c3c314a0f12383d41e8bee78\System.Xaml.ni.dll

*** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\Presentatio5ae0f00f#\8711b01d60a94d6ef6a02d7fd0578493\PresentationFramework.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\Presentatio5ae0f00f#\8711b01d60a94d6ef6a02d7fd0578493\PresentationFramework.ni.dll

*** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\WindowsBase\ac2e26bafa70e93b307087d7fe6b9dd2\WindowsBase.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\WindowsBase\ac2e26bafa70e93b307087d7fe6b9dd2\WindowsBase.ni.dll

*** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\Microsoft.V4e91a071#\207156ac71b58fb31310a2f78c3d0c44\Microsoft.VisualStudio.Web.Application.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\Microsoft.V4e91a071#\207156ac71b58fb31310a2f78c3d0c44\Microsoft.VisualStudio.Web.Application.ni.dll

更新

通过在项目的调试器菜单中选择“ native 代码”选项

enter image description here

我现在收到了一条稍微更详细的错误消息:

enter image description here

最后,按照下面的建议切换到 IIS Express,我仍然收到 AccessViolationException。这是我用来启用 IIS 进行调试的设置(在项目属性下)

enter image description here

这是错误信息

enter image description here

调用堆栈:

enter image description here

最佳答案

在我看来,你的工作比你需要的更努力。

首先,将 foreach 替换为 for 循环,将索引元素传递到编辑器模板中。这将建立您的模板上下文。

<fieldset>
<legend>Relationship Codes</legend>
<table id="Experiment">
<thead>
<tr>
<th>Relationship Effective Date</th>
<th>Relationship Dist Code</th>
</tr>
</thead>
<tbody>
@for (var i = 0; i < Model.AgentRelationshipCodes.Count(); i++)
{
@Html.EditorFor(model => model.AgentRelationshipCodes[i])
}
</tbody>
</table>
<br/>
<a href="javascript:void(0)" class ="addCode">Add Another</a>
</fieldset>

然后创建一个名为 AgentRelationshipCodes.cshtml 的编辑器模板(在 Views/Shared/EditorTemplates 中)

@model Monet.Models.AgentRelationshipCodes
<tr>
<td>@Html.EditorFor(model => model.EffectiveDate, "NullableDate", new { @class = "relCodeDate2" })</td>
<td>@Html.EditorFor(model => model.RelationshipId, "NullableDate", new { @class = "relDistCode1", maxlength = 3 })</td>
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.RelCodeOrdinal)
</tr>

这消除了对似乎导致问题的自定义助手的需要。

最后,为了添加新元素 - 将字段集移动到部分:

<script>
$(document).ready(function() {
$(".addCode").click(function () {
$('#fieldset').load('@Url.Action("NewRelationshipCode", "AgentTransmission")',$('#fieldset').closest('form').serialize());
});
});
</script>

<div id="fieldset">
@Html.Partial("fieldset");
</div>

并从您的 NewRelationshipCode 操作方法返回字段集 View :

[HandleProcessCorruptedStateExceptions]
public ViewResult NewRelationshipCode(YourViewModel model)
{
model.AgentRelationshipCodes.Add(new AgentRelationshipCodes());
return View("fieldset", model);
}

关于c# - AccessViolationException 未处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23437760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com