我正在使用 TeamCity 中的 MSBuild 运行程序来构建 ASP.net Web API 并运行单元测试。一切正常,直到我升级到“Microsoft Build Tools 2017 15.7.2”。
msbuild 突然从“C:\Program Files (x86)\ISS\Microsoft Web Deploy V3”或“C:\Program Files\ISS”复制旧版本的 Newtonsoft.Json.dll(版本 6.0.4.17603)\Microsoft Web Deploy V3”在构建解决方案时添加到输出文件夹。所有项目都使用 NuGet 引用 9.0.1 版本。
在构建运行时监视输出文件夹,我可以看到 .dll 在 6.0.4 和 9.0.1 之间来回切换,直到构建结束,而 6.0.4 版本仍然存在。
我找到了 this question当我将 Web 部署文件夹中的 Newtonsoft.Json.dll 文件重命名为 Newtonsoft.Json_old.dll”时,msbuild 没有替换我的 9.0.1 版本并且一切正常。
我已检查所有引用 Newtonsoft.Json 的项目都引用 9.0.1 版本并在 .csproj 文件中使用正确的提示路径。
有没有人知道如何解决这个问题?我的解决方案似乎更像是一种解决方法,我想知道为什么 msbuild 首先要复制此文件。
最佳答案
总结
当 MSBuild 解析程序集时,它会在一些非常奇怪的目录中搜索,包括 Web Deploy 文件夹,具体取决于您安装的内容。根据 MSBuild 引用,我认为这是遗留行为。您可以使用项目文件中定义的 MSBuild 属性来阻止它执行此操作。
在受影响的项目文件中,找到以下行:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
并在下面添加:
<PropertyGroup>
<AssemblySearchPaths>$(AssemblySearchPaths.Replace('{AssemblyFolders}', '').Split(';'))</AssemblySearchPaths>
</PropertyGroup>
这将导致 MSBuild 在解析程序集时不再查找有问题的文件夹。
全文
当我们迁移到 Visual Studio 2019 时,我的团队遇到了类似的问题。我们的一些项目仍然以 .NET Framework 4.0 为目标,并且在我们的构建代理上安装 Visual Studio 2019 后,我们开始遇到一个神秘的项目错误引用了我们的一些核心库:
The primary reference "OurCoreLibrary, Version=3.4.2.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx, processorArchitecture=MSIL" could not be resolved because it has an indirect dependency on the assembly "Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" which was built against the ".NETFramework,Version=v4.5" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.0".
将项目切换到目标 4.5 后问题就消失了,但由于我不会在这里详述的原因,我们无法对每个受影响的项目都这样做,所以我决定深入挖掘一下。
事实证明,您的问题让我们对正在发生的事情有了一些了解。我们引用的 Newtonsoft.Json 版本与“C:\Program Files (x86)\IIS\Microsoft Web Deploy V3”中的版本匹配,当我删除该文件时,构建成功。
我们的具体问题是 Web Deploy 文件夹中的 Newtonsoft.Json 副本是同一版本 (9.0.0.0),但框架错误(4.5 而不是 4.0),无论出于何种原因,解析逻辑都不会检查目标框架,导致构建时不匹配。更新到 VS2019 涉及更新 Web Deploy,这也将 Newtonsoft.Json 的副本更新到 9.0.0.0,导致我们发生冲突。
为了了解为什么甚至开始查看该程序集,我将 MSBuild 项目构建输出详细程度设置为诊断并查看发生了什么。搜索有问题的路径表明,在 ResolveAssemblyReferences 任务中,MSBuild 正在通过一些意想不到的地方来寻找匹配项:
1> For SearchPath "{AssemblyFolders}". (TaskId:9)
1> Considered "C:\Program Files (x86)\Microsoft.NET\ADOMD.NET\140\OurCoreLibrary.winmd", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Microsoft.NET\ADOMD.NET\140\OurCoreLibrary.dll", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Microsoft.NET\ADOMD.NET\140\OurCoreLibrary.exe", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\OurCoreLibrary.winmd", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\OurCoreLibrary.dll", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\OurCoreLibrary.exe", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\OurCoreLibrary.winmd", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\OurCoreLibrary.dll", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\OurCoreLibrary.exe", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files\IIS\Microsoft Web Deploy V3\OurCoreLibrary.winmd", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files\IIS\Microsoft Web Deploy V3\OurCoreLibrary.dll", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files\IIS\Microsoft Web Deploy V3\OurCoreLibrary.exe", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Microsoft SQL Server\140\SDK\Assemblies\OurCoreLibrary.winmd", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Microsoft SQL Server\140\SDK\Assemblies\OurCoreLibrary.dll", but it didn't exist. (TaskId:9)
1> Considered "C:\Program Files (x86)\Microsoft SQL Server\140\SDK\Assemblies\OurCoreLibrary.exe", but it didn't exist. (TaskId:9)
进一步挖掘表明,搜索的路径作为 AssemblySearchPaths 传入,定义在 Microsoft.Common.CurrentVersion.targets 中:
<AssemblySearchPaths Condition=" '$(AssemblySearchPaths)' == ''">
{CandidateAssemblyFiles};
$(ReferencePath);
{HintPathFromItem};
{TargetFrameworkDirectory};
$(AssemblyFoldersConfigFileSearchPath)
{Registry:$(FrameworkRegistryBase),$(TargetFrameworkVersion),$(AssemblyFoldersSuffix)$(AssemblyFoldersExConditions)};
{AssemblyFolders};
{GAC};
{RawFileName};
$(OutDir)
</AssemblySearchPaths>
根据MSBuild Task Reference for the ResolveAssemblyReferences task , SearchPaths 参数定义为:
Specifies the directories or special locations that are searched to find the files on disk that represent the assemblies. The order in which the search paths are listed is important. For each assembly, the list of paths is searched from left to right. When a file that represents the assembly is found, that search stops and the search for the next assembly starts.
...它定义了一些特殊常量,包括我们的 friend {AssemblyFolders}:
- {AssemblyFolders}: Specifies the task will use the Visual Studio.NET 2003 finding-assemblies-from-registry scheme.
因为目录是按顺序检查的,您可能希望 {HintPathFromItem} 优先,在大多数情况下确实如此。但是,如果您的依赖项依赖于旧版本的 Newtonsoft.Json,则不会有 HintPath 该版本,因此它将继续存在,直到它解决。
Later on in Microsoft.Common.CurrentVersion.targets我们可以看到有些情况下这个常量被显式删除,这就是上面答案的来源:
<PropertyGroup Condition="'$(_TargetFrameworkDirectories)' == '' and '$(AssemblySearchPaths)' != '' and '$(RemoveAssemblyFoldersIfNoTargetFramework)' == 'true'">
<AssemblySearchPaths>$(AssemblySearchPaths.Replace('{AssemblyFolders}', '').Split(';'))</AssemblySearchPaths>
</PropertyGroup>
删除此常量会从考虑中删除有问题的文件夹,老实说,我想不出我希望程序集隐式解析为 Newtonsoft.Json 在 Web 部署中闲逛的任何版本的情况,或者SQL Server SDK 文件夹。话虽这么说,但我确信在某些情况下关闭此功能会导致某些人出现问题,因此请记住这一点。
关于c# - MSBuild 正在用旧版本替换 Newtonsoft.Json.dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50638711/
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
如果我使用ruby版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":