Web应用程序在托管后不能工作

本文关键字:不能 工作 应用程序 Web | 更新日期: 2023-09-27 18:08:36

我在Visual Studio 2010中使用c#和ASP编写了一个web应用程序。. NET MVC 4(移动模板应用程序)。现在,当我在Visual Studio中运行它时,它工作得很好。但是现在我把它托管在我的网站上,我一直得到这个错误:

'/test'应用程序出现服务器错误。

无法加载一个或多个请求的类型。获取LoaderExceptions属性以获取更多信息。

描述:当前web请求执行过程中出现未处理的异常。请查看堆栈跟踪以获得有关错误及其在代码中的起源位置的更多信息。

异常详细信息:system . reflection . reflectiontypeloadeexception:无法加载一个或多个请求的类型。获取LoaderExceptions属性以获取更多信息。

源错误:

在执行当前web请求期间生成了一个未处理的异常。有关异常的来源和位置的信息可以使用下面的异常堆栈跟踪来识别。

[ReflectionTypeLoadException: Unable to load one or more of the requested types.      Retrieve the LoaderExceptions property for more information.]
System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) +0
System.Reflection.RuntimeModule.GetTypes() +4
System.Reflection.Assembly.GetTypes() +78
System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.<.ctor>b__1(Assembly a) +11
System.Linq.<SelectManyIterator>d__14`2.MoveNext() +238
System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection) +395
System.Data.Entity.ModelConfiguration.Mappers.TypeMapper..ctor(MappingContext mappingContext) +253
System.Data.Entity.DbModelBuilder.MapTypes(EdmModel model) +114
System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +112
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +59
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +62
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +117
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +453
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +56
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15
System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) +67
System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) +150
System.Data.Entity.DbSet`1.Add(TEntity entity) +42
PrayerTimes.Models.MosqueRepository.AddError(String error) in MosqueRepository.cs:74
PrayerTimes.Controllers.MosqueController.Index() in MosqueController.cs:57
lambda_method(Closure , ControllerBase , Object[] ) +40
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +188
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()+28
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult syncResult) +29
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +59
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +240
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +31
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +23
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +128
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50

System.Web.Mvc.Async.AsyncControllerActionInvoker。EndInvokeAction(IAsyncResult asyncResult) +26System.Web.Mvc灵活;> c_ DisplayClass1d。b_18(IAsyncResult asyncResult) +14System.Web.Mvc.Async灵活;> c_ DisplayClass4。b_3(IAsyncResult ar) +25System.Web.Mvc.Async.WrappedAsyncResult 1.End() +55 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +41 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25 System.Web.Mvc.Async.WrappedAsyncResult . end () +55System.Web.Mvc.Controller。endexexecute (IAsyncResult asyncResult) +28System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController。endexexecute (IAsyncResult asyncResult) +10System.Web.Mvc灵活;> c_ DisplayClass8。b_3(IAsyncResult asyncResult) +28System.Web.Mvc.Async灵活;> c_ DisplayClass4。b_3(IAsyncResult ar) +25System.Web.Mvc.Async.WrappedAsyncResult的1.()+ 55岁结束System.Web.Mvc.MvcHandler。EndProcessRequest(IAsyncResult) +30System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler。EndProcessRequest(IAsyncResult result) +9System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () + 8970141System.Web.HttpApplication。ExecuteStep(IExecutionStep step, Boolean&completedSynchronously) + 184

我试着在网上搜索一个解决方案,我遇到了很多,我试过,但似乎没有工作。其中一个解决方案是使用这个:

try
{
 //The code that causes the error goes here.
}
catch (ReflectionTypeLoadException ex)
{
StringBuilder sb = new StringBuilder();
foreach (Exception exSub in ex.LoaderExceptions)
{
    sb.AppendLine(exSub.Message);
    if (exSub is FileNotFoundException)
    {
        FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
        if(!string.IsNullOrEmpty(exFileNotFound.FusionLog))
        {
            sb.AppendLine("Fusion Log:");
            sb.AppendLine(exFileNotFound.FusionLog);
        }
    }
    sb.AppendLine();
 }
 string errorMessage = sb.ToString();
 //Display or log the error based on your application.
 }

我在我的数据库中做了一个错误表,并试图将错误从上面的代码添加到表中,但这也不起作用。有人能帮忙吗?非常感谢

Web应用程序在托管后不能工作

通常托管时MVC错误意味着MVC未安装在服务器上,因此无法加载所需的dll之一。

你可以通过复制所有dll到你的应用程序的bin目录来解决这个问题。

如果你将"Copy Local"标志设置为true,在你的MVC项目中的引用(System.Web. net)。Mvc,包含。路由等),它会将它们复制到bin目录中供您发布。有一个更简单的方法,但是我忘了快捷方式。

编辑

这里是可部署的依赖项。但如果评论是准确的,这个功能是在VS2010中,但在VS2012中删除,所以你仍然需要做"复制本地"的事情。

当你有一个网站项目,你的电脑上一切都很好,有些东西是不正确的托管网站,所以我建议你做以下工作:

  • 检查项目的。net框架版本与主机上web服务器的。net框架版本。

  • 将项目中所有引用的文件复制到托管的网站(本地bin -to->宿主bin)

    • 对于这项工作,您可以将项目中每个引用的"Copy To Local"属性设置为"TRUE"。例如,对于MVC项目,您可以遵循以下指导页面:. NET MVC应用程序"

    • 找到参考文件的所有依赖项并将它们复制到主机上的bin文件夹中(您可以使用添加可部署的依赖项特性)

  • 检查读写权限文件&主机上的文件夹(临时文件夹、日志文件夹、上传文件夹等)

  • 再次检查项目更改本地地址并将其转换为相对地址 (e:'a'b'website'images'1.jpg…=to=> images'1.jpg或http://localhost/images/1.jpg =to=> images/1.jpg

  • 当您更改一些基本设置(例如。net版本)时,重新启动主机上的一些服务(例如web服务器或…)

  • 如果你正在使用一些COM引用,那么检查它们是否安装在目标机器上具有相同的版本

编辑:

  • 检查"ASP的信任级别"。网络应用程序":
    <system.web>
        <securityPolicy>
              <trustLevel name="Full" policyFile="internal"/>
        </securityPolicy>
    </system.web>
编辑:

在这个问题中,您可以使用以下方法(由@BenGripka)测试您的代码:

这个错误没有真正的灵丹妙药。关键是掌握理解问题的所有信息。动态加载的程序集很可能缺少引用的程序集。该程序集需要位于应用程序的bin目录中。

使用此代码确定缺少的内容。

using System.IO;
using System.Reflection;
try
{
    //The code that causes the error goes here.
}
catch (ReflectionTypeLoadException ex)
{
    StringBuilder sb = new StringBuilder();
    foreach (Exception exSub in ex.LoaderExceptions)
    {
        sb.AppendLine(exSub.Message);
        if (exSub is FileNotFoundException)
        {
            FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
            if(!string.IsNullOrEmpty(exFileNotFound.FusionLog))
            {
                sb.AppendLine("Fusion Log:");
                sb.AppendLine(exFileNotFound.FusionLog);
            }
        }
        sb.AppendLine();
    }
    string errorMessage = sb.ToString();
    //Display or log the error based on your application.
}