【问题标题】:Including Json.NET in a Visual Studio extension在 Visual Studio 扩展中包含 Json.NET
【发布时间】:2019-03-04 22:54:51
【问题描述】:
我在 Visual Studio 扩展中使用 JSON.NET,它不包含在分发中 (by design by Microsoft)。
在链接的票证中,他们只是声明应该使用 v9.0.1。并且支持多个 VStudio 版本“使事情变得复杂”。
另一个SO question 给出并回答并非在所有情况下都有效。
我使用一个依赖于 Newtonsoft.Json 10.0.x 的 nuget 包。有什么方法可以让我继续使用 JSON.NET v10.x 而不会给 Visual Studio 带来任何麻烦?
【问题讨论】:
标签:
json.net
visual-studio-extensions
vsix
【解决方案1】:
一种可能的解决方案是使用 AppDomain 并将所有与 Newtonsoft.json 相关的调用包装在新的 AppDomain 中:
myDomain = AppDomain.CreateDomain("myDomain", AppDomain.CurrentDomain.Evidence,
new AppDomainSetup()
{
ApplicationBase = extensionPath,
ConfigurationFile = Path.Combine(extensionPath, configurationFileName)
});
文件 configurationFileName 应随您的扩展程序一起提供,其内容如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>