MVC 6: use Assembly - Load from network location

1

I am coding a web app with ASP.NET 5 and MVC 6. I would like to schedule and control tasks with this application. Therefore I decided to use the Windows Task Scheduler and for programming against it I found the following library: Task Scheduler Managed Wrapper

I downloaded and unblocked the .dll-File and added it as reference to my project because fetching it via NuGet didn't worked for me. Because of this assembly I removed .NET Core out of my project and I use .Net 4.5.1.

I wrote a little interface and the apropriate class for my taskscheduler, added this class as a service to the application and tried to use my taskscheduler via dependency injection in some controllers.

Here is the code of my ConfigureService method:


        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"]);
                });
        }

And this is the code inside my Scheduler class:


    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();
        }
    }

So nothing really special yet. I don't get any errors when I am building the app. But when I try to debug the app in IIS express I receive an exception when the webserver is going to start:


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)

followed by


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()

Is that the right way to do, what I intend to do? If yes, how can I use this assembly in my web application? I tried the following things which you can easily find in the world wide web:

  • unblock the .dll File
  • move the file out of the users document folder
  • add <loadFromRemoteSources enabled="true" /> to the config file of the IDE

Unfortunately nothing worked for me.

I am really looking forward to your help!

c#
asp.net
asp.net-mvc
.net-assembly
asp.net-core-mvc
asked on Stack Overflow Nov 19, 2015 by m4xy • edited Nov 20, 2015 by m4xy

1 Answer

0

Sometimes after you unblock an assembly, you will still get this error.

In this case, you should try removing the assembly from your project references, restart visual studio, add a new reference, browse (don't select from the list) for the unblocked assembly, and then clean and rebuild the solution. This usually works for me when the issue still persists after unblocking.

answered on Stack Overflow Nov 19, 2015 by cnsumner

User contributions licensed under CC BY-SA 3.0