如何调试通过 IIS 部署的 MVC5 库

Posted

技术标签:

【中文标题】如何调试通过 IIS 部署的 MVC5 库【英文标题】:How to debug a MVC5 Library deployed via IIS 【发布时间】:2021-10-30 20:54:30 【问题描述】:

我有一个使用 .NET Framework 4.7.2 的 MVC5 项目 它是作为一个库构建的 - 所以没有 exe 文件。它是通过 IIS 部署的。

所以通常这些东西可以正常工作 - 虽然我不知道如何。但现在我想迁移到现代应用程序使用的新 csproj 模式。一切都摆脱了每个项目文件的手动导入,我想你知道它是怎么回事。

问题是每当我尝试将它作为 IIS 进行调试时,它就会失败,因为它无法运行 DLL 文件。它只是没有配置...

构建没问题,其他一切都迁移得很好。我什至没有切换目标框架,我想要的只是更干净的 csproj 格式。所以请帮助我,因为我已经尝试了几个小时,而谷歌并没有真正的帮助

根据要求: 新的 csproj:

<Project Sdk="Microsoft.NET.Sdk" 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')" Sdk="Microsoft.NET.Sdk"  />


    <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
        <ProjectGuid>881DFF8A-F36B-4948-A62E-9E3A9BD6D9C9</ProjectGuid>
        <ProjectTypeGuids>349c5851-65df-11da-9384-00065b846f21;fae04ec0-301f-11d3-bf4b-00c04f79efbc</ProjectTypeGuids>
        <WcfConfigValidationEnabled>True</WcfConfigValidationEnabled>
        <SchemaVersion>2.0</SchemaVersion>
        <UseIISExpress>false</UseIISExpress>
        <IISExpressSSLPort>443</IISExpressSSLPort>
        <IISExpressAnonymousAuthentication>disabled</IISExpressAnonymousAuthentication>
        <IISExpressWindowsAuthentication>enabled</IISExpressWindowsAuthentication>
        <IISExpressUseClassicPipelineMode />
        <TargetFrameworkProfile />
        <SccProjectName>SAK</SccProjectName>
        <SccLocalPath>SAK</SccLocalPath>
        <SccAuxPath>SAK</SccAuxPath>
        <SccProvider>SAK</SccProvider>
      <OutputType>Library</OutputType>
      <OutputPath>bin\</OutputPath>
      <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
      <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="Microsoft.AspNet.Cors" Version="5.2.7" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.7" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Cors" Version="5.2.7" />
    <PackageReference Include="Microsoft.AspNet.WebApi.WebHost" Version="5.2.7" />
    <PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
    <PackageReference Include="NLog" Version="4.1.0" />
    <PackageReference Include="Serilog" Version="2.10.0" />
    <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
    <PackageReference Include="System.Data.Common" Version="4.3.0" />
    <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
    <PackageReference Include="System.IO" Version="4.3.0" />
    <PackageReference Include="System.Net.Http" Version="4.3.4" />
    <PackageReference Include="System.Runtime" Version="4.3.1" />
    <PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.1" />
    <PackageReference Include="System.Security.Cryptography.Encoding" Version="4.3.0" />
    <PackageReference Include="System.Security.Cryptography.Primitives" Version="4.3.0" />
    <PackageReference Include="System.Security.Cryptography.X509Certificates" Version="4.3.2" />
    <PackageReference Include="System.ServiceModel.Web" Version="1.0.0" />
    <PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" />
  </ItemGroup>


  <ItemGroup>
    PROJECT REFERENCES CENSORED
  </ItemGroup>


  <ItemGroup>
    <Reference Include="System.IdentityModel" />
    <Reference Include="System.ServiceModel" />
    <Reference Include="System.ServiceModel.Web" />
    <Reference Include="System.Web" />
  </ItemGroup>

    <PropertyGroup>
        <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
        <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    </PropertyGroup>

    <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" Sdk="Microsoft.NET.Sdk"  />

    <ProjectExtensions>
        <VisualStudio>
            <FlavorProperties GUID="349c5851-65df-11da-9384-00065b846f21">
                <WebProjectProperties>
                    <UseIIS>True</UseIIS>
                    <AutoAssignPort>True</AutoAssignPort>
                    <DevelopmentServerPort>51599</DevelopmentServerPort>
                    <DevelopmentServerVPath>/</DevelopmentServerVPath>
                    <IISUrl>http://localhost/Server</IISUrl>
                    <NTLMAuthentication>False</NTLMAuthentication>
                    <UseCustomServer>False</UseCustomServer>
                    <CustomServerUrl>
                    </CustomServerUrl>
                    <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
                </WebProjectProperties>
            </FlavorProperties>
        </VisualStudio>
    </ProjectExtensions>
</Project>

旧的 csproj:

<?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>
    <ProductVersion>
    </ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>881DFF8A-F36B-4948-A62E-9E3A9BD6D9C9</ProjectGuid>
    <ProjectTypeGuids>349c5851-65df-11da-9384-00065b846f21;fae04ec0-301f-11d3-bf4b-00c04f79efbc</ProjectTypeGuids>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <WcfConfigValidationEnabled>True</WcfConfigValidationEnabled>
    <UseIISExpress>false</UseIISExpress>
    <IISExpressSSLPort>443</IISExpressSSLPort>
    <IISExpressAnonymousAuthentication>disabled</IISExpressAnonymousAuthentication>
    <IISExpressWindowsAuthentication>enabled</IISExpressWindowsAuthentication>
    <IISExpressUseClassicPipelineMode />
    <TargetFrameworkProfile />
    <SccProjectName>SAK</SccProjectName>
    <SccLocalPath>SAK</SccLocalPath>
    <SccAuxPath>SAK</SccAuxPath>
    <SccProvider>SAK</SccProvider>
    <UseGlobalApplicationHostFile />
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
    <Use64BitIISExpress />
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <PlatformTarget>AnyCPU</PlatformTarget>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <PlatformTarget>AnyCPU</PlatformTarget>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>
    <Reference Include="NLog, Version=4.1.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
      <HintPath>..\..\packages\NLog.4.1.0\lib\net45\NLog.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Serilog.2.9.0\lib\net46\Serilog.dll</HintPath>
    </Reference>
    <Reference Include="Serilog.Sinks.File, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Serilog.Sinks.File.4.1.0\lib\net45\Serilog.Sinks.File.dll</HintPath>
    </Reference>
    <Reference Include="System.ComponentModel.Composition" />
    <Reference Include="System.Data" />
    <Reference Include="System.IdentityModel" />
    <Reference Include="System" />
    <Reference Include="System.IO, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\..\packages\System.IO.4.3.0\lib\net462\System.IO.dll</HintPath>
      <Private>True</Private>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll</HintPath>
      <Private>True</Private>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll</HintPath>
    </Reference>
    <Reference Include="System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll</HintPath>
      <Private>True</Private>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.Runtime.Serialization" />
    <Reference Include="System.Security.Cryptography.Algorithms, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net463\System.Security.Cryptography.Algorithms.dll</HintPath>
      <Private>True</Private>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.Security.Cryptography.Encoding, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
      <Private>True</Private>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.Security.Cryptography.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
      <Private>True</Private>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
      <Private>True</Private>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.ServiceModel" />
    <Reference Include="System.ServiceModel.Web" />
    <Reference Include="System.Web" />
    <Reference Include="System.Web.Cors, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Microsoft.AspNet.Cors.5.2.7\lib\net45\System.Web.Cors.dll</HintPath>
    </Reference>
    <Reference Include="System.Web.Http, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Microsoft.AspNet.WebApi.Core.5.2.7\lib\net45\System.Web.Http.dll</HintPath>
    </Reference>
    <Reference Include="System.Web.Http.Cors, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Microsoft.AspNet.WebApi.Cors.5.2.7\lib\net45\System.Web.Http.Cors.dll</HintPath>
    </Reference>
    <Reference Include="System.Web.Http.WebHost, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.7\lib\net45\System.Web.Http.WebHost.dll</HintPath>
    </Reference>
    <Reference Include="System.Web.Services" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  TONS OF CONTENT INCLUDE PER FILE IN SOLUTION 
  <ItemGroup>
   PROJECT REFERENCES CENSORED 
  </ItemGroup>
  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
  <ProjectExtensions>
    <VisualStudio>
      <FlavorProperties GUID="349c5851-65df-11da-9384-00065b846f21">
        <WebProjectProperties>
          <UseIIS>True</UseIIS>
          <AutoAssignPort>True</AutoAssignPort>
          <DevelopmentServerPort>51599</DevelopmentServerPort>
          <DevelopmentServerVPath>/</DevelopmentServerVPath>
          <IISUrl>http://localhost/Server</IISUrl>
          <NTLMAuthentication>False</NTLMAuthentication>
          <UseCustomServer>False</UseCustomServer>
          <CustomServerUrl>
          </CustomServerUrl>
          <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
        </WebProjectProperties>
      </FlavorProperties>
    </VisualStudio>
  </ProjectExtensions>
  <Import Project="..\..\packages\Fody.1.29.4\build\dotnet\Fody.targets" Condition="Exists('..\..\packages\Fody.1.29.4\build\dotnet\Fody.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is 0.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\..\packages\Fody.1.29.4\build\dotnet\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Fody.1.29.4\build\dotnet\Fody.targets'))" />
  </Target>
</Project>

我尝试将所有内容从旧到新...我还尝试使用不同的项目 sdk,例如普通的 .NET Sdk 和 Sdk.Web

【问题讨论】:

你需要一些应用程序调用这个dll,否则你不能直接运行一个dll 这个应用程序应该是 IIS。通常在调试 IIS 时,调试器会附加到 IIS 进程 w3w.exe 左右,与应用程序池相关联,然后可以进行调试,并且当这个应用程序使用旧的 csproj 模式时它是可能的(如果有必要,我可以发布旧的与新的csproj 格式),但我现在不知道如何配置它。因为我们的项目没有入口点,所以它有一个 global.asax 那么您应该提供有关您如何调试以及进行了哪些更改的完整信息 调试没有什么秘密我只需点击播放按钮^^ .NET Framework 项目应保留旧项目格式。时期。所有尝试使用 SDK 风格的项目格式都只是浪费时间。 【参考方案1】:

您是否尝试过手动将调试器附加到 IIS 进程?我们以类似的方式工作(托管在 IIS 中的库,对所有项目使用新的 CSProj 样式),但我从不使用“播放”按钮进行调试。应用程序已构建,我们将 IIS 中站点的“基本设置”中的“物理路径”值设置为项目的文件夹,点击该站点,然后在 Visual Studio 中的“调试”菜单下选择“附加到进程”。 ..”:

然后选择“w3wp.exe”(您可能必须先选中“显示所有用户的进程”并单击“附加”。然后,您的断点应该被命中。请注意,您需要以管理员身份运行 Visual Studio为了附加到 w3wp 进程。

【讨论】:

这是可能的,但我担心如果它使调试变得更加复杂,人们将不会接受我的 PR——即使只是一点点,因为在迁移之前我们可以使用播放按钮。现在所有人都需要习惯于处理:S,但仍然非常感谢您的回答! :)【参考方案2】:

所以,首先您需要在 IIS 中创建 AppPool 和 WebSite。 将 WebSite 文件夹设置为您的 Web 项目(放置 web.config 的文件夹,而不是 bin 文件夹)。

然后您可以使用 2 个选项进行调试:

    按“播放”按钮,Visual Studio 会打开浏览器,您可以调试前端+后端。 只需构建您的项目并按菜单调试->附加到进程->选择“w3wp.exe”。使用此选项,您可以只调试后端部分。

【讨论】:

这个问题的重点是我不能按“播放按钮”。但我需要按下这个播放按钮 :( 关键是切换到 SDK csproj 模式后,播放按钮不再起作用。我的意思是你也可以很容易地重现它。创建一个 WCF Web 应用程序项目,编辑 CSPROJ 并更改 ... 模式的一些行。您也可以尝试删除 .Web 以获得正常的控制台应用程序,但仍然。使用播放按钮进行调试不再工作:( 您使用的是 Microsoft 升级助手吗? github.com/dotnet/upgrade-assistant 还是全部手工制作? 我是手工完成的......我们的项目不是任何助手都可以迁移的 所以,这个助手只迁移项目类型(不要更改源)。请尝试迁移并比较结果。可能是某些参数未应用并且所有工作都不正确。您的大多数项目迁移正确(或只需要手动进行一些更改) [15:25:45 ERR] 对 Web 窗体的支持仅限于 .NET Full Framework。要了解更多信息,请阅读:aka.ms/migrate-web-forms [15:25:45 ERR] 对 WCF 服务器端服务的支持仅限于 .NET Full Framework。要了解更多信息,请阅读:aka.ms/migrate-wcf-to-grpc [15:25:45 ERR] Project D:\AHP One\AhpWCFServices\ahpFrontendWCFService\ahpFrontendWCFService.csproj 使用不受支持的功能。

以上是关于如何调试通过 IIS 部署的 MVC5 库的主要内容,如果未能解决你的问题,请参考以下文章

如何确保使用 ASP.NET MVC5 和 IIS 7.5 express 刷新 CSS 文件

ASP.NET MVC5/IIS Express 无法调试 - 代码未运行

mvc 5部署到win10 IIS失败[重复]

Asp.net MVC5 angular4未捕获引用错误:部署到IIS后系统未在systemjs.config.js中定义

c# 在本地调试可以的,但部署到iis上到就报错。

ASP.NET Core Web 应用程序开发期间部署到IIS自定义主机域名并附加到进程调试