使用 Visual Studio web.config 转换进行调试 [英] Use Visual Studio web.config transform for debugging

查看:26
本文介绍了使用 Visual Studio web.config 转换进行调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
如何才能我在内置的 Visual Studio 调试器服务器中使用 Web.debug.config?

我想使用 Web.config 转换,它既适用于发布也适用于调试.

当我发布 Web 应用程序时,Visual Studio 会根据我当前的构建配置自动转换 Web.config.当我开始调试时,如何告诉 Visual Studio 做同样的事情?在调试启动时,它只使用默认的 Web.config 而不进行转换.

有什么想法吗?

解决方案

OK,理解 web.debug.config &web.release.config 仅用于打包/发布.我想出了一种方法来启用您正在尝试执行的操作.我在 https://devblogs.microsoft.com/aspnet/asp-net-web-projects-web-debug-config-web-release-config/.这是摘要.

现在让我们看看如何启用提问者想要做的事情.

概括地说,当他基于特定配置构建时,他希望将特定转换应用于 web.config.所以很明显你不想维护 web.config 文件,因为它会被覆盖.

所以我们需要做的是创建一个新文件web.template.config,它只是web.config 的一个副本.然后只需使用 Windows 资源管理器删除 web.config(不要使用 Visual Studio 删除,因为我们不想从项目中删除它).

注意:如果您使用的是集成到 Visual Studio 中的源代码管理提供程序,那么您可能希望从源代码管理中删除 web.config.

此外,我们不想使用 web.debug.configweb.release.config,因为它们已经在 Web 发布管道中具有明确定义的角色所以我们不想打扰.因此,我们将在与项目和 web.template.configweb.dev.debug.configweb.xml 相同的文件夹中创建两个新文件.dev.release.config.

这个想法是,当您从 Visual Studio 调试或运行应用程序时,这些将是应用的转换.现在我们需要连接到构建/打包/发布过程来完成这一切.Web 应用程序项目 (WAP) 有一个扩展点,您可以在同一文件夹中创建一个名为 {ProjectName}.wpp.targets 其中 {ProjectName} 的项目文件> 是项目的名称.如果此文件与 WAP 位于磁盘上的同一文件夹中,则它将自动导入到项目文件中.所以我创建了这个文件.我已经放置了以下内容:

<项目工具版本=4.0";xmlns=http://schemas.microsoft.com/developer/msbuild/2003"><!-- 确保 web.config 将在那里,即使是包/发布--><目标名称=CopyWebTemplateConfig"BeforeTargets=构建"><Copy SourceFiles="web.template.config";DestinationFiles="web.config"/></目标><属性组><PrepareForRunDependsOn>$(PrepareForRunDependsOn);更新WebConfigBeforeRun;</PrepareForRunDependsOn></PropertyGroup><!-- 该目标将在您在 Visual Studio 中运行您的应用程序之前运行--><目标名称=UpdateWebConfigBeforeRun"><Message Text="Configuration: $(Configuration): web.dev.$(Configuration).config"/><TransformXml Source="web.template.config";Transform="web.dev.$(Configuration).config";目的地=web.config"/></目标><!-- 从创建的包中排除配置模板文件--><目标名称=ExcludeCustomConfigTransformFiles"BeforeTargets="ExcludeFilesFromPackage"><项目组><ExcludeFromPackageFiles Include="web.template.config;web.dev.*.config"/></项目组><Message Text="ExcludeFromPackageFiles: @(ExcludeFromPackageFiles)";重要性=高"/></目标></项目>

让我解释一下.我创建了 CopyWebTemplateConfig 目标,它会在构建时始终将 web.template.config 复制到 web.config,即使您没有在 Visual Studio 中调试应用程序.>

这是需要的,因为我们仍然需要支持 Visual Studio 的打包/发布过程.然后我扩展了属性 PrepareForRunDependsOn 以包含 UpdateWebConfigBeforeRun 目标.此属性用于标识在从 Visual Studio 运行任何托管项目之前需要执行的目标列表.

在这个目标中,我使用 TransformXml 任务来转换 web.template.config,使用正确的 web.dev.***.config 文件.之后,您的应用程序将根据您的构建配置使用正确的 web.config 启动.之后,我有另一个目标 ExcludeCustomConfigTransformsFiles,我通过属性 BeforeTargets="ExcludeFilesFromPackage" 将其注入到包/发布过程中.这是必需的,因为我们不希望在打包或发布应用程序时包含这些文件.所以这就是它的全部内容.

针对此场景多解释一下打包/发布过程.当您打包/发布 web.debug.configweb.release.config 时,仍将使用,具体取决于构建配置.但最终它要转换的文件是 web.template.config,因此您可能需要根据该文件中的内容进行调整.问题/意见?

Possible Duplicate:
How can I use Web.debug.config in the built-in visual studio debugger server?

I want to use the Web.config transformation that works fine for publish also for debugging.

When I publish a web app, Visual Studio automatically transforms the Web.config based on my currenctbuild configuration. How can I tell Visual Studio to do the same when I start debugging? On debug start it simply uses the default Web.config without transformation.

Any idea?

解决方案

OK, with the understanding that web.debug.config & web.release.config are for package/publish only. I have come up with a way in which to enable what you are trying to do. I've blogged about it at https://devblogs.microsoft.com/aspnet/asp-net-web-projects-web-debug-config-web-release-config/. Here is the summary.

Now let’s see how we can enable what the question asker wants to do.

To recap, when he builds on a particular configuration he wants a specific transform to be applied to web.config. So obviously you do not want to maintain a web.config file, because it is going to be overwritten.

So what we need to do is to create a new file web.template.config, which is just a copy of web.config. Then just delete web.config by using Windows Explorer (don’t delete using Visual Studio because we do not want to delete it from the project).

Note: If you are using a source control provider which is integrated into Visual Studio then you probably want to delete web.config from source control.

Also with this we do not want to use web.debug.config or web.release.config because these already have a well defined role in the Web Publishing Pipeline so we do not want to disturb that. So instead we will create two new files, in the same folder as the project and web.template.config, web.dev.debug.config and web.dev.release.config.

The idea is that these will be the transforms applied when you debug, or run, your application from Visual Studio. Now we need to hook into the build/package/publish process to get this all wired up. With Web Application Projects (WAP) there is an extensibility point that you can create a project file in the same folder with the name {ProjectName}.wpp.targets where {ProjectName} is the name of the project. If this file is on disk in the same folder as the WAP then it will automatically be imported into the project file. So I have created this file. And I have placed the following content:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- Make sure web.config will be there even for package/publish -->
  <Target Name="CopyWebTemplateConfig" BeforeTargets="Build">
    <Copy SourceFiles="web.template.config"
          DestinationFiles="web.config"/>
  </Target>
  
  <PropertyGroup>
    <PrepareForRunDependsOn>
      $(PrepareForRunDependsOn);
      UpdateWebConfigBeforeRun;
    </PrepareForRunDependsOn>
  </PropertyGroup>

  <!-- This target will run right before you run your app in Visual Studio -->
  <Target Name="UpdateWebConfigBeforeRun">
    <Message Text="Configuration: $(Configuration): web.dev.$(Configuration).config"/>
    <TransformXml Source="web.template.config"
              Transform="web.dev.$(Configuration).config"
              Destination="web.config" />
  </Target>

  <!-- Exclude the config template files from the created package -->
  <Target Name="ExcludeCustomConfigTransformFiles" BeforeTargets="ExcludeFilesFromPackage">
    <ItemGroup>
      <ExcludeFromPackageFiles Include="web.template.config;web.dev.*.config"/>
    </ItemGroup>
    <Message Text="ExcludeFromPackageFiles: @(ExcludeFromPackageFiles)" Importance="high"/>
  </Target>
</Project>

Let me explain this a bit. I have created the CopyWebTemplateConfig target which will always copy web.template.config to web.config on build, even if you are not debugging your application in Visual Studio.

This is needed because we still need to support the package/publish process of Visual Studio. Then I extended the property PrepareForRunDependsOn to include the UpdateWebConfigBeforeRun target. This property is used to identify the list of targets which needs to be executed before any managed project is run from Visual Studio.

In this target I am using the TransformXml task to transform web.template.config, using the correct web.dev.***.config file. After that your app starts up using the correct web.config based on your build configuration. After that I have another target ExcludeCustomConfigTransformsFiles, which I inject into the package/publish process via the attribute BeforeTargets="ExcludeFilesFromPackage". This is needed because we do not want these files to be included when the application is packaged or published. So that is really all there is to it.

To explain the package/publish process a bit more for this scenario. When you package/publish web.debug.config or web.release.config, depending on build configuration, will still be used. But ultimately the file that it is transforming is web.template.config, so you may have to adjust depending on what you have in that file. Questions/Comments?

这篇关于使用 Visual Studio web.config 转换进行调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆