日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

在部署 C#项目时转换 App.config 配置文件

發(fā)布時間:2023/12/10 C# 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在部署 C#项目时转换 App.config 配置文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

問題

部署項目時,常常需要根據(jù)不同的環(huán)境使用不同的配置文件。例如,在部署網(wǎng)站時可能希望禁用調(diào)試選項,并更改連接字符串以使其指向不同的數(shù)據(jù)庫。在創(chuàng)建 Web 項目時,Visual Studio 自動生成了?Web.config、Web.Debug.config、Web.release.config這3個不同的配置文件,并提供了轉(zhuǎn)換工具,用于在部署項目時自動轉(zhuǎn)換配置文件內(nèi)容。具體可以參考這2篇文章:如何:在部署 Web 應用程序項目時轉(zhuǎn)換 Web.config?和?用于 Web 應用程序項目部署的 Web.config 轉(zhuǎn)換語法?。

然而在其他項目類型中(如控制臺應用程序、Windows 服務),并沒有現(xiàn)成的配置文件的轉(zhuǎn)換功能。

臨時解決方案

準備2個配置文件:App.config?和?App.Release.config?,然后修改項目?.csproj?文件,更新?AfterBuild?生成事件:

<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release' "><Delete Files="$(TargetDir)$(TargetFileName).config" /><Copy SourceFiles="$(ProjectDir)\app.$(Configuration).config" DestinationFiles="$(TargetDir)$(TargetFileName).config" /> </Target>

這樣在選擇 Release 配置時,執(zhí)行生成操作會刪除?App.config?文件,然后用?App.Release.config文件替換。雖然這樣也可以實現(xiàn)根據(jù)環(huán)境來選擇配置文件,但是這種方法需要保證這2個配置文件內(nèi)容保持同步,特別是要保證 assemblyBinding 標簽內(nèi)容一致, 這個標簽的作用是程序集版本重定向,如果不一致會拋出 “未能加載文件或程序集” 這個異常。

直到找到這篇文章?Enable app.debug.config app.release.config?時才完美解決配置文件轉(zhuǎn)換的問題。

正式做法

  • 我們在項目中添加?App.config、App.Debug.config、App.Release.config?這3個配置文件。
  • 打開項目所在目錄,用記事本或其他文本編輯器打開?.csproj?文件。
  • 在?PropertyGroup?標簽下添加如下內(nèi)容:

    <PropertyGroup><ProjectConfigFileName>App.config</ProjectConfigFileName> </PropertyGroup>
  • 在?ItemGroup?標簽中找到和?App.config、App.Debug.config、App.Release.config相關的項目,替換為

    <None Include="App.config" /> <None Include="App.Debug.config"><DependentUpon>App.config</DependentUpon> </None> <None Include="App.Release.config"><DependentUpon>App.config</DependentUpon> </None>
  • 在最后一個?Import?標簽后面添加:

    <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
  • 在?Import?標簽后面添加?Target?標簽:

    <Target Name="AfterBuild"><TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" /> </Target>
  • 切換到 Visual Studio , 重新加載項目。

  • 這時查看 Visual Studio 可以看到?App.config?的組織方式和?Web.config?一樣了。

    App.config

    現(xiàn)在就可以使用?用于 Web 應用程序項目部署的 Web.config 轉(zhuǎn)換語法?這篇文章中提到的轉(zhuǎn)換語法了。

    例如需要替換?connectionStrings?,?App.config?有如下配置:

    <?xml version="1.0" encoding="utf-8" ?> <configuration><connectionStrings><add name="connString" connectionString="Server=debug;Database=test;Uid=root;Pwd=123456;CharSet=utf8;"providerName="MySql.Data.MySqlClient" /></connectionStrings> </configuration>

    只需要修改?App.Release.config?為如下內(nèi)容即可:

    <?xml version="1.0" encoding="utf-8"?><!-- 有關使用 web.config 轉(zhuǎn)換的詳細信息,請訪問 http://go.microsoft.com/fwlink/?LinkId=125889 --><configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"><connectionStrings><add name="connString"connectionString="Server=release;Database=test;Uid=root;Pwd=654321;CharSet=utf8;"xdt:Transform="SetAttributes" xdt:Locator="Match(name)" /></connectionStrings> </configuration>

    這樣在選擇?Release?配置時,connectionStrings?會自動替換成?App.Release.config?中的值。查看?bin\Release?目錄下的 config 文件可以進行驗證。

    完整代碼

    <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /><PropertyGroup><Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration><Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform><ProjectGuid>{8196CA4E-AD25-4F90-BB80-D27512BF4BD4}</ProjectGuid><OutputType>Exe</OutputType><AppDesignerFolder>Properties</AppDesignerFolder><RootNamespace>App.Config轉(zhuǎn)換</RootNamespace><AssemblyName>App.Config轉(zhuǎn)換</AssemblyName><TargetFrameworkVersion>v4.0</TargetFrameworkVersion><FileAlignment>512</FileAlignment></PropertyGroup><PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "><PlatformTarget>AnyCPU</PlatformTarget><DebugSymbols>true</DebugSymbols><DebugType>full</DebugType><Optimize>false</Optimize><OutputPath>bin\Debug\</OutputPath><DefineConstants>DEBUG;TRACE</DefineConstants><ErrorReport>prompt</ErrorReport><WarningLevel>4</WarningLevel></PropertyGroup><PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "><PlatformTarget>AnyCPU</PlatformTarget><DebugType>pdbonly</DebugType><Optimize>true</Optimize><OutputPath>bin\Release\</OutputPath><DefineConstants>TRACE</DefineConstants><ErrorReport>prompt</ErrorReport><WarningLevel>4</WarningLevel></PropertyGroup><PropertyGroup><ProjectConfigFileName>App.config</ProjectConfigFileName></PropertyGroup><ItemGroup><Reference Include="System" /><Reference Include="System.configuration" /><Reference Include="System.Core" /><Reference Include="System.Xml.Linq" /><Reference Include="System.Data.DataSetExtensions" /><Reference Include="Microsoft.CSharp" /><Reference Include="System.Data" /><Reference Include="System.Xml" /></ItemGroup><ItemGroup><Compile Include="Program.cs" /><Compile Include="Properties\AssemblyInfo.cs" /></ItemGroup><ItemGroup><None Include="App.config" /><None Include="App.Debug.config"><DependentUpon>App.config</DependentUpon></None><None Include="App.Release.config"><DependentUpon>App.config</DependentUpon><SubType>Designer</SubType></None></ItemGroup><Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /><Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" /><!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets.<Target Name="BeforeBuild"></Target><Target Name="AfterBuild"></Target>--><Target Name="AfterBuild"><TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" /></Target> </Project>

    示例項目下載:
    App.Config轉(zhuǎn)換.zip

    參考鏈接

    如何:在部署 Web 應用程序項目時轉(zhuǎn)換 Web.config
    用于 Web 應用程序項目部署的 Web.config 轉(zhuǎn)換語法
    Enable app.debug.config app.release.config

    總結(jié)

    以上是生活随笔為你收集整理的在部署 C#项目时转换 App.config 配置文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。