FIX: "System.Security.SecurityException" Error Message Occurs When You Invoke a Method on a Transparent Proxy (327441)



The information in this article applies to:

  • Microsoft Windows .NET Framework 1.1
  • Microsoft .NET Framework 1.0

This article was previously published under Q327441

SYMPTOMS

When a method is called through an interface on a transparent proxy, you may get the following error message:
An unhandled exception of type 'System.Security.SecurityException' occurred in TestClass.exe

Additional information: System.Security.Permissions.SecurityPermission
This error message occurs although the permission is not required.

RESOLUTION

To resolve this problem, you can use an abstract class instead of an interface.

STATUS

This bug was corrected in .NET Framework (2003|1.1).

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Microsoft C# Console Application and name it TestClass.
  2. Paste the following code to the Class1.cs file:
    using System;
    using System.Reflection;
    using System.Security.Permissions;
    
    // To refuse the UnmanagedCode permission
    [assembly: SecurityPermission(SecurityAction.RequestRefuse, UnmanagedCode=true)]
    
    interface ITest
    {
    	void Test();
    }
    
    class TestClass: MarshalByRefObject, ITest
    {
    	public static void Main(String[] args)
    	{
    		AppDomain ad = AppDomain.CreateDomain("NewDomain", null, null);
    
    		TestClass proxy = (TestClass)ad.CreateInstance("TestClass", "TestClass").Unwrap();
    
    		ITest i = proxy as ITest;
    		i.Test();
    	}
    	public void Test()
    	{
    		Console.WriteLine("Test");
    	}
    }
    					
  3. Compile the code and then run the code. The SecurityException is thrown.
  4. You can use the following code sample to demonstrate the use of an "abstract class", which resolves this problem:
    using System;
    using System.Reflection;
    using System.Security.Permissions;
    
    // To refuse the UnmanagedCode permission
    [assembly: SecurityPermission(SecurityAction.RequestRefuse, UnmanagedCode=true)]
    
    abstract class CTest : MarshalByRefObject
    {
    	public abstract void Test();
    }
    
    class TestClass: CTest
    {
    	public static void Main(String[] args)
    	{
    		AppDomain ad = AppDomain.CreateDomain("NewDomain", null, null);
    
    		TestClass proxy = (TestClass)ad.CreateInstance("TestClass", "TestClass").Unwrap();
    
    		CTest i = proxy as CTest;
    		i.Test();
    	}
    	public override void Test()
    	{
    		Console.WriteLine("Test");
    	}
    }
    					

Modification Type:MinorLast Reviewed:5/28/2003
Keywords:kbfix kbbug kbpending kbRemoting KB327441 kbAudDeveloper