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

时间:2015-08-06 14:09:02

标签: c# remoteobject

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

我正在尝试将对象用作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>

EDIT2:

错误消息和堆栈跟踪:

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: 

1 个答案:

答案 0 :(得分:1)

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

并且“typeFilterLevel = full”是解决方案,但我的服务器/客户端* .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>

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

我知道.NET Remoting现在并不常用,因为WCF,但我只是想知道它是如何工作的,然后才进入WCF。

干杯!