mscorlib.dll 中发生了 'System.Security.SecurityException' 类型的未处理异常

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

我的问题是标题中提到的错误。

我正在尝试使用 objects 作为 CAO 类型。我已经完成了正确的装配、服务器和客户端。

当我尝试使用线路时发生:

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

这在我使用默认构造函数时有效。 可能是什么问题?

编辑: 这是 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 服务器:

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

而客户的 App.config:

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

编辑2:

错误消息和堆栈跟踪:

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: 

我想我终于明白了。 @Greg Burghardt 的建议帖子让我在 MSDN 中阅读了更多相关信息,我发现它与 .NET 4.0 中有关安全性的更改有关。

"typeFilterLevel=full" 是解决方案,但我的 server/client *.config 文件中还有其他错误。下面,我展示他们的正确版本:

客户端

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

服务器

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

我知道 .NET Remoting 现在不常使用,这是 WCF 的原因,但我只是想在进入 WCF 之前了解它是如何工作的。

干杯!