NAnt extension getting Inheritance security rules violated while overriding member error

Topics: Errors
Mar 6, 2015 at 1:06 PM
Hi, I originally posted this in the issues but now that I look at it I think it belongs in the discussion unless it becomes something we need a bug for. Unfortunately it doesn't look like I can remove my issue... Anyway...

I'm trying to write a NAnt extension that uses this library but I get this exception on my first method that loads the taskscheduler objects.

System.TypeLoadException: Inheritance security rules violated while overriding member: 'Microsoft.Win32.TaskScheduler.TaskService.System.Runtime.Serialization.ISerializab
le.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'. Security accessibility of the overriding method must mat
ch the security accessibility of the method being overriden.

I'm assuming this is related to NAnt dynamically loading the extension dll but I'm not sure how to get around it. Previously we were using the taskscheduler 1.0 library via NAnt without any issues.
        public void test()
            using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService())
                // Create a new task definition and assign properties
                Microsoft.Win32.TaskScheduler.TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = "Does something";

                // Create a trigger that will fire the task at this time every other day
                td.Triggers.Add(new Microsoft.Win32.TaskScheduler.DailyTrigger { DaysInterval = 2 });

                // Create an action that will launch Notepad whenever the trigger fires
                td.Actions.Add(new Microsoft.Win32.TaskScheduler.ExecAction("notepad.exe", "c:\\test.log", null));

                // Register the task in the root folder
                ts.RootFolder.RegisterTaskDefinition(@"Test", td);

Mar 6, 2015 at 7:33 PM
I believe this is related to a change in the security model for .NET 4.0. I have added the SecurityCritical attribute to the method in your exception and have posted a .NET 4.0 build under the issue you posted. Please check it and confirm it resolves your issue and then I'll get it added into the next release (2.3.1).
Marked as answer by dahall on 9/1/2015 at 10:02 AM