我在scriptcs中添加了一个应用程序,并添加了一些对版本为v2.0.50727的程序集的引用。因此,在运行scriptcs文件时,它返回为混合模式程序集是针对运行时的版本“v2.0.50727”构建的,无法在4.0运行时加载。app.config中的属性useLegacyV2RuntimeActivationPolicy =“true”可能会解决asp中的问题.net web应用程序。但在scriptcs中它不起作用。进一步搜索显示上面的属性useLegacyV2RuntimeActivationPolicy =“true”应该添加为scriptcs.exe.config。我有一个名为FMUpgrade.csx的应用程序文件,我们如何在FMUpgrade.csx文件中引用此scriptcs.exe.config文件。文档中没有太多关于scriptcs.exe.config的说明。还在app中添加了program.exe.config .config但仍未成功。
答案 0 :(得分:0)
经过大量研究后,我得到了解决上述问题的解决方法。 通过使用类ExeConfigurationFileMap,我们能够从app.config获取键值,它无法绕过由混合模式汇编错误引起的支持的运行时错误。 Server server = new Server(new ServerConnection(con)); server.ConnectionContext.ExecuteNonQuery(脚本); 执行语句ExecuteNonQuery时会导致该错误。 所以在执行语句之前
if(RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully) server.ConnectionContext.ExecuteNonQuery(脚本);
解决方案如下 使用System.Runtime.CompilerServices; 使用System.Runtime.InteropServices; public static class RuntimePolicyHelper { public static bool LegacyV2RuntimeEnabledSuccessfully {get;私人集; }
static RuntimePolicyHelper()
{
ICLRRuntimeInfo clrRuntimeInfo =
(ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
Guid.Empty,
typeof(ICLRRuntimeInfo).GUID);
try
{
clrRuntimeInfo.BindAsLegacyV2Runtime();
LegacyV2RuntimeEnabledSuccessfully = true;
}
catch (COMException)
{
// This occurs with an HRESULT meaning
// "A different runtime was already bound to the legacy CLR version 2 activation policy."
LegacyV2RuntimeEnabledSuccessfully = false;
}
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
private interface ICLRRuntimeInfo
{
void xGetVersionString();
void xGetRuntimeDirectory();
void xIsLoaded();
void xIsLoadable();
void xLoadErrorString();
void xLoadLibrary();
void xGetProcAddress();
void xGetInterface();
void xSetDefaultStartupFlags();
void xGetDefaultStartupFlags();
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void BindAsLegacyV2Runtime();
}
} using System.Runtime.CompilerServices;
使用System.Runtime.InteropServices; public static class RuntimePolicyHelper { public static bool LegacyV2RuntimeEnabledSuccessfully {get;私人集; }
static RuntimePolicyHelper()
{
ICLRRuntimeInfo clrRuntimeInfo =
(ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
Guid.Empty,
typeof(ICLRRuntimeInfo).GUID);
try
{
clrRuntimeInfo.BindAsLegacyV2Runtime();
LegacyV2RuntimeEnabledSuccessfully = true;
}
catch (COMException)
{
// This occurs with an HRESULT meaning
// "A different runtime was already bound to the legacy CLR version 2 activation policy."
LegacyV2RuntimeEnabledSuccessfully = false;
}
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
private interface ICLRRuntimeInfo
{
void xGetVersionString();
void xGetRuntimeDirectory();
void xIsLoaded();
void xIsLoadable();
void xLoadErrorString();
void xLoadLibrary();
void xGetProcAddress();
void xGetInterface();
void xSetDefaultStartupFlags();
void xGetDefaultStartupFlags();
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void BindAsLegacyV2Runtime();
}
}