.NET Console应用程序连接字符串转换

[英].NET Console Application Connection String Transformation


I have just split my app into a web app and a console application webjob, I got the console application connected with my machines database through addition of ado.net and it mapped my models. However when I upload as a webjob to Azure I think it may still be trying to use the same database even though it as as web job for the application as it works for my local database on my local machine though not as a webjob.

我刚刚将我的应用程序拆分为Web应用程序和控制台应用程序webjob,我通过添加ado.net将控制台应用程序与我的机器数据库连接,并映射了我的模型。然而,当我作为webjob上传到Azure时,我认为它可能仍在尝试使用相同的数据库,即使它像应用程序的Web作业一样,因为它适用于我本地计算机上的本地数据库,但不是作为webjob。

Basically what I'm saying is how do I do the equivalent of the publish SQL settings and web config transformations of a web app in a console app?

基本上我所说的是如何在控制台应用程序中完成Web应用程序的发布SQL设置和Web配置转换?

1 个解决方案

#1


0  

Based on Alex's answer, maybe this is what you are looking for...

根据Alex的回答,也许这就是你要找的......

I was looking for an easy solution for Azure WebJobs project to transform App.config during build that works with Visual Studio and MSBuild on the server.

我正在寻找一个简单的Azure WebJobs项目解决方案,在构建过程中转换App.config,与服务器上的Visual Studio和MSBuild一起使用。

1. Add an XML file for each configuration to the project.

1.将每个配置的XML文件添加到项目中。

Typically you will have Debug and Release configurations so name your files App.Debug.config and App.Release.config. In my project, I created a configuration for each kind of environment, so you might want to experiment with that.

通常,您将具有Debug和Release配置,因此将文件命名为App.Debug.config和App.Release.config。在我的项目中,我为每种环境创建了一个配置,因此您可能希望对其进行试验。

2. Unload project and open .csproj file for editing

2.卸载项目并打开.csproj文件进行编辑

Visual Studio allows you to edit .csproj files right in the editor—you just need to unload the project first. Then right-click on it and select Edit <ProjectName>.csproj.

Visual Studio允许您在编辑器中编辑.csproj文件 - 您只需要先卸载项目。然后右键单击它并选择Edit .csproj。

3. Bind App.*.config files to main App.config

3.将App。*。配置文件绑定到主App.config

Find the project file section that contains all App.config and App.*.config references. You'll notice their build actions are set to None:

找到包含所有App.config和App。*。config references的项目文件部分。您会注意到他们的构建操作设置为None:

<None Include="App.config" />
<None Include="App.Debug.config" />
<None Include="App.Release.config" />

First, set build action for all of them to Content.
Next, make all configuration-specific files dependant on the main App.config so Visual Studio groups them like it does designer and code-behind files.

首先,将所有这些内容的构建操作设置为Content。接下来,使所有特定于配置的文件依赖于主App.config,以便Visual Studio将它们分组,就像设计器和代码隐藏文件一样。

Replace XML above with the one below:

将上面的XML替换为下面的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. Activate transformations magic

4.激活变形魔法

In the end of file after

在文件结束之后

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

and before final

在决赛之前

</Project>

insert the following XML:

插入以下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>

Now you can reload the project, build it and enjoy App.config transformations!

现在您可以重新加载项目,构建它并享受App.config转换!

FYI

Make sure that your App.*.config files have the right setup like this:

确保您的App。*。config文件具有如下正确的设置:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
     <!--magic transformations here-->
</configuration>
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2018/03/27/359fafc45ec4395795923febfb1ac7d0.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告