我刚刚将我的应用程序拆分为一个 Web 应用程序和一个控制台应用程序 webjob,我通过添加 ado.net 将控制台应用程序与我的机器数据库连接起来,并映射了我的模型。但是,当我作为 webjob 上传到 Azure 时,我认为它可能仍在尝试使用相同的数据库,即使它作为应用程序的 web 作业,因为它适用于我本地机器上的本地数据库,但不是作为 webjob。
基本上我要说的是如何在控制台应用程序中执行 Web 应用程序的发布 SQL 设置和 Web 配置转换的等效操作?
根据亚历克斯的回答,也许这就是你要找的......
我一直在为 Azure WebJobs 项目寻找一个简单的解决方案,以在构建期间转换 App.config,该解决方案可与服务器上的 Visual Studio 和 MSBuild 配合使用。
1. 为项目中的每个配置添加一个 XML 文件。
通常你会有Debug
和Release
配置,所以命名你的文件App.Debug.config
和App.Release.config
. 在我的项目中,我为每种环境创建了一个配置,因此您可能想对此进行试验。
2.卸载项目并打开.csproj文件进行编辑
Visual Studio 允许您直接在编辑器中编辑.csproj文件 - 您只需要先卸载项目。然后右键单击它并选择Edit <ProjectName>.csproj。
3. 将 App.*.config 文件绑定到主 App.config
找到包含所有App.config
和App.*.config
引用的项目文件部分。您会注意到他们的构建操作设置为None
:
<None Include="App.config" />
<None Include="App.Debug.config" />
<None Include="App.Release.config" />
首先,将所有它们的构建操作设置为Content
.
接下来,使所有特定于配置的文件依赖于主文件,App.config
以便 Visual Studio 将它们分组,就像对设计器和代码隐藏文件进行分组一样。
用下面的替换上面的 XML:
<Content Include="App.config" />
<Content Include="App.Debug.config" >
<DependentUpon>App.config</DependentUpon>
</Content>
<Content Include="App.Release.config" >
<DependentUpon>App.config</DependentUpon>
</Content>
4.激活变身魔法
在文件末尾之后
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
并且在决赛之前
</Project>
插入以下 XML:
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
<!-- Generate transformed app config in the intermediate directory -->
<TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
<!-- Force build process to use the transformed configuration file from now on. -->
<ItemGroup>
<AppConfigWithTargetPath Remove="app.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
现在您可以重新加载项目,构建它并享受App.config
转换!
供参考
确保您的App.*.config
文件具有如下正确设置:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--magic transformations here-->
</configuration>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句