【问题标题】:MVC 6: use Assembly - Load from network locationMVC 6:使用程序集 - 从网络位置加载
【发布时间】:2015-11-19 21:50:24
【问题描述】:

我正在用 ASP.NET 5 和 MVC 6 编写一个 Web 应用程序。我想用这个应用程序安排和控制任务。因此,我决定使用 Windows 任务计划程序并针对它进行编程,我发现了以下库:Task Scheduler Managed Wrapper

我下载并解锁了 .dll 文件并将其添加为对我的项目的引用,因为通过 NuGet 获取它对我不起作用。 由于这个程序集,我从我的项目中删除了 .NET Core,并使用了 .Net 4.5.1。

我为我的任务调度器编写了一个小接口和适当的类,将这个类作为服务添加到应用程序中,并尝试通过在某些控制器中的依赖注入来使用我的任务调度器。

这是我的 ConfigureService 方法的代码:


        public void ConfigureServices(IServiceCollection services)
        {
            // Add MVC services to the services container.
            services.AddMvc();

            // Add TaskScheduler Service
            services.AddSingleton<IScheduler, Scheduler>();

            // Add Entity Framework
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext(options =>
                {
                    options.UseSqlServer(Configuration["Data:ConnectionString"]);
                });
        }

这是我的调度程序类中的代码:


    public class Scheduler : IScheduler
    {
        static TaskService ts = new TaskService();

        public ModuleExecution GetStatus(Module mod)
        {
            throw new NotImplementedException();
        }

        public void Put(Module mod)
        {
            throw new NotImplementedException();
        }

        public void Remove(Module mod)
        {
            throw new NotImplementedException();
        }
    }

所以还没有什么特别的。 我在构建应用程序时没有收到任何错误。但是当我尝试在 IIS express 中调试应用程序时,当网络服务器将要启动时,我收到一个异常:


NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.
System.Reflection.RuntimeAssembly.nLoadFile(String path, Evidence evidence)

紧随其后


FileLoadException: Could not load file or assembly 'Microsoft.Win32.TaskScheduler, Version=2.5.4.0, Culture=neutral, PublicKeyToken=0d013ddd5178a2ae' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
SomeName.Models.Scheduler..cctor()

这是正确的做法吗,我打算做什么? 如果是,我如何在我的 Web 应用程序中使用这个程序集? 我尝试了以下您可以在万维网上轻松找到的方法:

  • 取消阻止 .dll 文件
  • 将文件移出用户文件夹
  • &lt;loadFromRemoteSources enabled="true" /&gt;添加到IDE的配置文件中

不幸的是,对我没有任何帮助。

我真的很期待你的帮助!

【问题讨论】:

  • 假设您已经在 Windows 资源管理器中解锁了该程序集,您是否尝试过从您的项目中删除该程序集?在您的项目引用中,删除程序集,然后重新启动 Visual Studio 并重新添加它,当您重新添加它时,请确保您浏览它而不是从列表中选择它。让我知道这是否有效。
  • 我已经完成了所有这些步骤。不幸的是没有成功。
  • 这不是这个问题的解决方案,但我通过让 NuGet 再次工作来解决这个问题。然后我在 NuGet 的帮助下添加了库,并且我很喜欢。

标签: c# asp.net asp.net-mvc .net-assembly asp.net-core-mvc


【解决方案1】:

有时在您取消阻止程序集后,您仍然会收到此错误。

在这种情况下,您应该尝试从项目引用中删除程序集,重新启动 Visual Studio,添加新引用,浏览(不要从列表中选择)以获取未阻止的程序集,然后然后清理并重建解决方案。当解锁后问题仍然存在时,这通常对我有用。

【讨论】:

  • 我已经完成了所有这些步骤。不幸的是没有成功。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 2012-07-18
  • 1970-01-01
相关资源
最近更新 更多