7

我正在为我的项目处理一组常量,我想使用在源代码级别验证其中的一些。为了实现这一点,我使用以下代码段将整个解决方案加载到带有and的AppDomain 中,即使用x86 平台目标启用远程处理IsFullyTrusted == trueIsHomogenous == true

// load workspace, i.e. solution from Visual Studio
var workspace = Roslyn.Services.Workspace.LoadSolution(solutionFile);

使用 Roslyn 的 x86 平台的 NCrunch 和 NUnit 测试运行器

但是,在使用具有平台配置 x86 的 1.45 或 2.6.2nunit-console-x86.exe作为测试运行器时,我不断得到以下信息System.Security.SecurityException

System.Security.SecurityException : Type System.Runtime.Remoting.ObjRef and the types derived from it (such as System.Runtime.Remoting.ObjRef) are not permitted to be deserialized at this security level.

Server stack trace: 
   at System.Runtime.Serialization.FormatterServices.CheckTypeSecurity(Type t, TypeFilterLevel securityLevel)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.CheckSecurity(ParseRecord pr)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObject(ParseRecord pr)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Remoting.Channels.CoreChannel.DeserializeBinaryRequestMessage(String objectUri, Stream inputStream, Boolean bStrictBinding, TypeFilterLevel securityLevel)
   at System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Roslyn.Utilities.RemoteServices.Initialize(Int32 clientProcessId)
   at Roslyn.Utilities.RemoteServices.StartRemoteServicesProcess()
   at Roslyn.Utilities.RemoteServices.get_Instance()
   at Roslyn.Utilities.RemoteServices.CreateInstance[T]()
   at Roslyn.Services.Host.TemporaryStorageServiceFactory.CreateService(IWorkspaceServiceProvider workspaceServices)
   at Roslyn.Services.Host.WorkspaceServiceProviderFactory.<>c__DisplayClass6.<OnImportsSatisfied>b__1(IWorkspaceServiceProvider wsp)
   at Roslyn.Services.Host.WorkspaceServiceProvider.ConstructService(Type type)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Roslyn.Services.Host.WorkspaceServiceProvider.GetService[TWorkspaceService]()
   at Roslyn.Services.SolutionServices..ctor(IWorkspaceServiceProvider workspaceServices)
   at Roslyn.Services.Solution..ctor(SolutionId id, String filePath, VersionStamp version, VersionStamp latestProjectVersion, IWorkspaceServiceProvider workspaceServices)
   at Roslyn.Services.Host.SolutionFactoryServiceFactory.SolutionFactoryService.CreateSolution(SolutionId id)
   at Roslyn.Services.Host.TrackingWorkspace.CreateNewSolution(ISolutionFactoryService solutionFactory, SolutionId id)
   at Roslyn.Services.Host.TrackingWorkspace..ctor(IWorkspaceServiceProvider workspaceServiceProvider, Boolean enableBackgroundCompilation, Boolean enableInProgressSolutions)
   at Roslyn.Services.Host.HostWorkspace..ctor(IWorkspaceServiceProvider workspaceServiceProvider, Boolean enableBackgroundCompilation, Boolean enableInProgressSolutions, Boolean enableFileTracking)
   at Roslyn.Services.Host.LoadedWorkspace..ctor(IWorkspaceServiceProvider workspaceServiceProvider, IDictionary`2 globalProperties, Boolean enableBackgroundCompilation, Boolean enableFileTracking)
   at Roslyn.Services.Host.LoadedWorkspace.LoadSolution(String solutionFileName, String configuration, String platform, Boolean enableFileTracking)
   at Roslyn.Services.Workspace.LoadSolution(String solutionFileName, String configuration, String platform, Boolean enableFileTracking)

NCrunch 论坛上有一个讨论但我尝试了以下所有选项但没有成功

  • 添加[assembly: AllowPartiallyTrustedCallers]到 AssemblyInfo.cs
  • 添加[assembly: SecurityRules(SecurityRuleSet.Level1)]到 AssemblyInfo.cs
  • 添加<NetFx40_LegacySecurityPolicy enabled="true" />到 app.config
  • 以管理员身份运行 VS2012
  • 装饰单元测试和实现[SecuritySafeCritical]
  • 更新:创建新的AppDomain并提供
    • PermissionState.Unrestricted,SecurityPermissionFlag.AllFlagsDataProtectionPermissionFlags.AllFlags
    • 添加主机证据SecurityZone.MyComputerSystem.Security.Policy.HashSystem.Security.Policy.StrongName
    • fullTrustAssemblies在创建时添加所有程序集(我的和 Roslyn CTP)AppDomain

更新#2

此异常仅在我在 x86 配置下运行测试时发生,在我切换到 x64 平台配置后,一切似乎都工作正常

问题

在 x86 配置下运行时,是否有任何其他属性或配置更改app.configAppDomain有助于在 .NET Framework 远程处理中启用反序列化?System.Runtime.Remoting.ObjRef

临时解决方案

仅为 unittest 项目切换到 x64 构建配置

源代码

附加信息

我也注意到...

  1. Roslyn.Services.dll在所有测试完成后,很多实例都在后台挂起。
  2. NCrunch 缺乏主机证据:System.Security.Policy.HashSystem.Security.Policy.StrongName带有测试运行程序程序集名称
  3. MSIL(内部应该是 x64)和 2.6.2nunit-console.exe测试运行器工作正常,所以看起来像 Roslyn configuration/remoting/security configuration issue
4

2 回答 2

4

看起来正在以部分信任的方式执行测试,而 Resharper 正在以完全信任的方式运行它们。

Roslyn 尚未在部分信任方案中进行测试。可能会访问需要完全信任的 API。

我没有使用 ncrun,但也许有一种方法可以配置它以在完全信任下运行测试?

于 2013-05-06T15:17:08.717 回答
1

我想补充一点!

在升级一个 NUnit 实例后,我设法运行到软件的 2.6.2 和 2.6.3,我的团队遇到了与 Akim 看到的这个确切的 System.Security 异常类似的问题。

我们正在使用一些未使用正确信任设置创建的自定义 NUnit 逻辑创建 I​​pcChannel,因此我们必须更改如下所示的内容:

IpcChannel channel = new IpcChannel(string.Format("localhost:{0}", portNum));

至 -

BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();

var properties = new System.Collections.Hashtable();
properties["name"] = "ipc";
properties["priority"] = "20";
properties["portName"] = string.Format("localhost:{0}", portNum);

IpcChannel channel = new IpcChannel(properties, clientProvider, serverProvider);

只是一个快速修复我注意到我想我会转发给任何人看到类似的东西,不能只是切换他们的平台设置。公平地说,我花了大约四个小时才弄清楚,所以我不想浪费知识。

于 2013-11-13T15:23:32.490 回答