MVC 6:使用Assembly - 从网络位置加载

时间:2015-11-19 21:50:24

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

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

我下载并解锁了.dll-File并将其添加为我的项目的参考,因为通过NuGet获取它并没有为我工作。 由于这个程序集,我从我的项目中删除了.NET Core,并使用.Net 4.5.1。

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

以下是我的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"]);
                });
        }

这是我的Scheduler类中的代码:


    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中调试应用程序时,我会在Web服务器启动时收到异常:


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文件
  • 将文件移出用户文档文件夹
  • <loadFromRemoteSources enabled="true" />添加到IDE的配置文件

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

我真的很期待你的帮助!

1 个答案:

答案 0 :(得分:0)

有时在解锁程序集后,您仍会收到此错误。

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