Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7740409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:46:18+00:00 2026-06-01T08:46:18+00:00

I have a unit test project in Visual Studio 2010 (.NET 4) that utilizes

  • 0

I have a unit test project in Visual Studio 2010 (.NET 4) that utilizes the xUnit.net testing framework, Moq, and the Moles Isolation framework for generating stubs of static methods. I am using xUnit version 1.9 on a 64-bit machine.

To run tests from the command line, I am using the following command:

moles.runner.exe Project.Tests.dll /runner:xunit.console.clr4.exe

However, I get the following exception every time:

instrumenting…started xUnit.net console test runner (64-bit .NET
4.0.30319.1) Copyright (C) 2007-11 Microsoft Corporation.

Unhandled Exception: System.MethodAccessException: Attempt by security
transparent method ‘Xunit.ConsoleClient.Program.Main(System.String[])’
to access security critical method
‘System.AppDomain.add_UnhandledException(System.UnhandledExceptionEventHandler)’
failed. at Xunit.ConsoleClient.Program.Main(String[] args) at
Microsoft.Moles.Runner.MolesRunner.RunnerRunner.Run(String runner,
String[] args) at
Microsoft.Moles.Runner.MolesRunner.RunnerRunner.Run(String runner,
String[] args) at
Microsoft.Moles.Runner.MolesRunner.LaunchRunnerEntryPoint(MolesRunnerOptions
options) at Microsoft.Moles.Runner.MolesRunner.RunnerMain(String[]
args) at Microsoft.Moles.Runner.Program.Main(String[] args)

It looks like the exception is coming from xUnit; however, I am able to run the tests using xunit.console.clr4.exe alone without issue. It only fails when using the xUnit console from the Moles runner.

I found this on a forum post:

In the .NET 4 framework, security tranparency rules prevent any
security transparent code from calling into security critical code.

What can I check to determine the cause of this error? Is there a security setting I need to change to prevent this?

Note: I am also having the same exact problem on a 32-bit workstation.

Update: I decided to download the code from http://xunit.codeplex.com/SourceControl/changeset/changes/600246119dca and debug myself. In the xunit.console project (the output of which is the exe getting invoked from the Moles runner), the main thread of execution looks like this:

[STAThread]
public static int Main(string[] args)
{
    Console.WriteLine("xUnit.net console test runner ({0}-bit .NET {1})", 
        IntPtr.Size * 8, Environment.Version);
    Console.WriteLine("Copyright (C) 2007-11 Microsoft Corporation.");

    if (args.Length == 0 || args[0] == "/?")
    {
        PrintUsage();
        return -1;
    }

    AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

    try
    {
        CommandLine commandLine = CommandLine.Parse(args);

        int failCount = RunProject(commandLine.Project, 
            commandLine.TeamCity, commandLine.Silent);

        if (commandLine.Wait)
        {
            Console.WriteLine();
            Console.Write("Press any key to continue...");
            Console.ReadKey();
            Console.WriteLine();
        }

        return failCount;
    }
    catch (ArgumentException ex)
    {
        Console.WriteLine();
        Console.WriteLine("error: {0}", ex.Message);
        return -1;
    }
    catch (BadImageFormatException ex)
    {
        Console.WriteLine();
        Console.WriteLine("{0}", ex.Message);
        return -1;
    }
}

When I run my tests while debugging the code, everything works fine, as expected (since I never had issues running tests from xUnit alone). I noticed the following line, which appears to be where the exception is thrown based on the error message and stack trace from my original post:

AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

I commented out this line, built xunit.console.exe, and tried using it as the /runner argument when executing the Moles runner again. This time, no exception was thrown.

I am still at a loss as to why a Security exception is being thrown on this line when invoked from moles.runner.exe, but not when I run the xUnit console by itself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:46:19+00:00Added an answer on June 1, 2026 at 8:46 am

    Compiling from the latest source code from CodePlex runs without the MethodAccessException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Visual Studio solution named - UnitTestProject, unit test with xunit framework.
I have a Compact Framework Project that has two Unit Test projects with it
I have a Unit test project for my Application using DUnit framework. This project
HI, I want to have set configuration settings for a unit test project that
I have a simple project on C#.NET and have written it in Visual Studio
Using Visual Studio 2010 C++ with googletest. I'm new to unit testing and I've
Visual studio created a unit test project for me based on a method (right-click
I am writing unit tests in visual studio 2010. For test some functionality, I
I'm trying to start using Unit Testing on my current project in Visual Studio
I've got a CSV file that I added to a Visual Studio Unit Test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.