An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll

Paweł Poręba picture Paweł Poręba · Aug 6, 2015 · Viewed 7.6k times · Source

my problem is the error mentioned in the title.

I am trying to use an objects as CAO type. I've creasted proper assembly, serwer and the client.

It occurs when I try to use the line:

JamesBondCar[] cars = { new JamesBondCar("one", 100, true, true), 
                                      new JamesBondCar("Two", 100, true, true), 
                                      new JamesBondCar("Three", 100, true, true) };
CarProvider cp=new CarProvider(cars);

This works when I use default constructor. What might be the problem?

Edit: This is the code for CarProvider:

 public class CarProvider : MarshalByRefObject 
    {
        private ArrayList theJBCars = new ArrayList(); 
        public CarProvider()
        {
            Console.WriteLine("Utworzono CarProvider.");
            theJBCars.Add(new JamesBondCar("QMobile", 140, true, true));
            theJBCars.Add(new JamesBondCar("FLyer", 140, true, false));
            theJBCars.Add(new JamesBondCar("Swimmer", 140, false, true));
            theJBCars.Add(new JamesBondCar("BasicJBC", 140, false, false));
        }
        public CarProvider(JamesBondCar[] theCars)
        { 
            Console.WriteLine("Utworzono JamesBondCar za pomocą niestandardowego konstruktora.");
            theJBCars.AddRange(theCars);
        }
        public ArrayList GetAllAutos() 
        { return theJBCars; }
        public JamesBondCar GetJBCByIndex(int i)
        { return (JamesBondCar)theJBCars[i]; }
    }

App.config for the serwer:

<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <activated
            type="CarGeneralAsm.CarProvider,CarGeneralAsm"/>
      </service>
        <channels>
          <channel ref="http" port="32469"/>
        </channels>
    </application>
  </system.runtime.remoting>
</configuration>

And the App.config for the client:

<configuration>
  <system.runtime.remoting>
    <application name="TestowanieTypówCAO">
      <client
        url="http://localhost:32469">
        <activated
          type="CarGeneralAsm.CarProvider,CarGeneralAsm"/>
      </client>
      <channels>
        <channel ref="http"/>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

Edit2:

Error msg and stack trace:

System.Security.SecurityException was unhandled
  _HResult=-2146233078
  _message=Żądanie nie powiodło się.
  HResult=-2146233078
  IsTransient=false
  Message=Żądanie nie powiodło się.
  RemoteStackTrace=
Server stack trace: 
   w System.Array.InternalCreate(Void* elementType, Int32 rank, Int32* pLengths, Int32* pLowerBounds)
   w System.Array.CreateInstance(Type elementType, Int32 length)
   w System.Runtime.Serialization.Formatters.Soap.ObjectReader.ParseArray(ParseRecord pr)
   w System.Runtime.Serialization.Formatters.Soap.ObjectReader.ParseObject(ParseRecord pr)
   w System.Runtime.Serialization.Formatters.Soap.ObjectReader.Parse(ParseRecord pr)
   w System.Runtime.Serialization.Formatters.Soap.SoapHandler.StartChildren()
   w System.Runtime.Serialization.Formatters.Soap.SoapParser.ParseXml()
   w System.Runtime.Serialization.Formatters.Soap.SoapParser.Run()
   w System.Runtime.Serialization.Formatters.Soap.ObjectReader.Deserialize(HeaderHandler handler, ISerParser serParser)
   w System.Runtime.Serialization.Formatters.Soap.SoapFormatter.Deserialize(Stream serializationStream, HeaderHandler handler)
   w System.Runtime.Remoting.Channels.CoreChannel.DeserializeSoapRequestMessage(Stream inputStream, Header[] h, Boolean bStrictBinding, TypeFilterLevel securityLevel)
   w System.Runtime.Remoting.Channels.SoapServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)

Exception rethrown at [0]: 

  Source=mscorlib
  GrantedSet=""
  PermissionState=<PermissionSet class="System.Security.PermissionSet"
version="1"
Unrestricted="true"/>

  RefusedSet=""
  Url=""
  StackTrace:
    Server stack trace: 
       w System.Array.InternalCreate(Void* elementType, Int32 rank, Int32* pLengths, Int32* pLowerBounds)
       w System.Array.CreateInstance(Type elementType, Int32 length)
       w System.Runtime.Serialization.Formatters.Soap.ObjectReader.ParseArray(ParseRecord pr)
       w System.Runtime.Serialization.Formatters.Soap.ObjectReader.ParseObject(ParseRecord pr)
       w System.Runtime.Serialization.Formatters.Soap.ObjectReader.Parse(ParseRecord pr)
       w System.Runtime.Serialization.Formatters.Soap.SoapHandler.StartChildren()
       w System.Runtime.Serialization.Formatters.Soap.SoapParser.ParseXml()
       w System.Runtime.Serialization.Formatters.Soap.SoapParser.Run()
       w System.Runtime.Serialization.Formatters.Soap.ObjectReader.Deserialize(HeaderHandler handler, ISerParser serParser)
       w System.Runtime.Serialization.Formatters.Soap.SoapFormatter.Deserialize(Stream serializationStream, HeaderHandler handler)
       w System.Runtime.Remoting.Channels.CoreChannel.DeserializeSoapRequestMessage(Stream inputStream, Header[] h, Boolean bStrictBinding, TypeFilterLevel securityLevel)
       w System.Runtime.Remoting.Channels.SoapServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)
    Exception rethrown at [0]: 
       w System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       w System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       w System.Runtime.Remoting.Activation.IActivator.Activate(IConstructionCallMessage msg)
       w System.Runtime.Remoting.Activation.LocalActivator.DoRemoteActivation(IConstructionCallMessage ctorMsg)
       w System.Runtime.Remoting.Activation.LocalActivator.Activate(IConstructionCallMessage ctorMsg)
       w System.Runtime.Remoting.Activation.AppDomainLevelActivator.Activate(IConstructionCallMessage ctorMsg)
       w System.Runtime.Remoting.Messaging.ClientContextTerminatorSink.SyncProcessMessage(IMessage reqMsg)
       w System.Runtime.Remoting.Activation.ActivationServices.Activate(RemotingProxy remProxy, IConstructionCallMessage ctorMsg)
       w System.Runtime.Remoting.Proxies.RemotingProxy.InternalActivate(IConstructionCallMessage ctorMsg)
       w System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(IMessage reqMsg)
       w System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       w CarGeneralAsm.CarProvider..ctor(JamesBondCar[] theCars)
       w CarProviderClient.CarClient.Main(String[] args) w c:\Users\Paweł\Documents\Visual Studio 2013\Projects\CarProviderClient\CarProviderClient\CarClient.cs:wiersz 31
       w System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       w System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       w System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Answer

Paweł Poręba picture Paweł Poręba · Aug 9, 2015

I guess I finally figured it out. @Greg Burghardt's suggested posts made me read more about it in MSDN and I found that it is connected with changes made in .NET 4.0 regarding security.

And "typeFilterLevel=full" was the solution, but there were other mistakes in my server/client *.config files. Below, I present their proper versions:

Client

<configuration>
  <system.runtime.remoting>
    <application name="TestowanieTypówCAO">
      <channels>
        <channel ref="http">
          <clientProviders>
            <formatter ref="soap"/>
          </clientProviders>
          <serverProviders>
            <formatter ref="soap"  typeFilterLevel="Full"/>
          </serverProviders>
        </channel>
      </channels>
      <client url="http://localhost:32469">
        <activated type="CarGeneralAsm.CarProvider,CarGeneralAsm"/>
      </client>    
    </application>
  </system.runtime.remoting>
</configuration>

Serwer

<configuration>
  <system.runtime.remoting>
    <application>
      <channels>
          <channel ref="http" port="32469">
             <serverProviders>
                <formatter ref="soap" typeFilterLevel="Full"/>
             </serverProviders>
             <clientProviders>
               <formatter ref="soap"/>
             </clientProviders>
            </channel>
        </channels>
      <service>
        <activated
            type="CarGeneralAsm.CarProvider,CarGeneralAsm"/>
      </service>    
    </application>
  </system.runtime.remoting>
</configuration>

I know .NET Remoting is now not often used, cause of WCF, but I just wanted to know how all of it works, before stepping into WCF.

Cheers!