CZEMacLeod / MSBuild.SDK.SystemWeb

This MSBuild SDK is designed to allow for the easy creation and use of SDK (shortform) projects targeting ASP.NET 4.x using System.Web.
MIT License
151 stars 8 forks source link

IntelliSense errors in Visual Studio 2022 (v17.0.1) #27

Closed stevenvolckaert closed 2 years ago

stevenvolckaert commented 2 years ago

Hello!

We're using MSBuild.SDK.SystemWebv4.0.49` in some of our ASP.NET MVC 5 projects, which are targeting .NET Framework 4.8,

My development team has let me know that when they open CSHTML files, they get IntelliSense errors like the one below.

image

I tried updating MSBuild.SDK.SystemWeb to the latest release (v4.0.63), but no luck. When running or debugging the application, there is no problem at all; everything seems to work.

Would anyone have some ideas on how to fix or workaround this?

In both VS 2019 and VS 2022, the default editor for CSHTML files is HTML Editor:

image

Also, when I open a CSHTML file, the following notification is displayed in the bottom-left corner of the screen:

image

And when I click the notfiication,Visual Studio takes me to the Language Client activation failures; exception details below:

Task Failed - Activating language client HtmlLanguageClient: HtmlLanguageClient failed to initialize.  Please report a problem via Help -> Send Feedback -> Report a Problem.  Status = Sending 'initialize' request.  Exception = StreamJsonRpc.ConnectionLostException: The JSON-RPC connection with the remote party was lost before the request could complete.
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at StreamJsonRpc.JsonRpc.<InvokeCoreAsync>d__149.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at StreamJsonRpc.JsonRpc.<InvokeCoreAsync>d__139`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.LanguageServer.Client.JsonRpcExtensionMethods.<SendMethodRequestAsync>d__1`2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.LanguageServer.Client.RemoteLanguageClientInstance.<SendInitializeRequestAsync>d__101.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.LanguageServer.Client.RemoteLanguageClientInstance.<InitializeAsync>d__100.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.VisualStudio.LanguageServer.Client.RemoteLanguageClientInstance.<InitializeAsync>d__100.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Threading.ThreadingTools.<WithCancellationSlow>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.LanguageServer.Client.RemoteLanguageClientInstance.<ActivateLanguageClientAsync>d__107.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

You can find the csproj of one of the projects below:

<Project Sdk="MSBuild.SDK.SystemWeb/4.0.63"> <!-- Sdk="MSBuild.SDK.System.Web/4.0.49" -->

  <Import Project="..\common.props" />

  <!-- If Visual Studio shows the following warning, copy the assembly binding redirects from the <runtime> element in
       file ./bin/Skarabee.CMS.SiteAdmin.dll.config to ./Web.config - Steven Volckaert, 10 August 2021.

       Warning MSB3276 Found conflicts between different versions of the same dependent assembly. Please set the 
       "AutoGenerateBindingRedirects" property to true in the project file. For more information, see 
       http://go.microsoft.com/fwlink/?LinkId=294190. -->

  <PropertyGroup>
    <EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
    <EnableDefaultNoneItems>false</EnableDefaultNoneItems>
    <RootNamespace>Skarabee.CMS.SiteAdmin</RootNamespace>
    <TargetFramework>net48</TargetFramework>
    <Version>1.0.0-alpha01</Version>
    <UserSecretsId>302f321d-8049-4733-85e7-6170981b1e94</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="System.ComponentModel.DataAnnotations" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="5.0.2" />
    <PackageReference Include="jQuery" Version="1.6.4" />
    <PackageReference Include="jQuery.UI.Combined" Version="1.8.11" />
    <PackageReference Include="jQuery.Validation" Version="1.9.0" />
    <PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.7" />
    <PackageReference Include="Microsoft.AspNet.Razor" Version="3.2.7" />
    <PackageReference Include="Microsoft.AspNet.SignalR.JS" Version="1.0.0" />
    <PackageReference Include="Microsoft.AspNet.SignalR.SystemWeb" Version="1.0.0" />
    <PackageReference Include="Microsoft.AspNet.WebApi" Version="5.2.7" />
    <PackageReference Include="Microsoft.AspNet.WebPages" Version="3.2.7" />
    <PackageReference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" Version="3.6.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
    <PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="5.0.8" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="5.0.0" />
    <PackageReference Include="Microsoft.Web.Infrastructure" Version="1.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
    <PackageReference Include="NLog" Version="4.7.9" />
    <PackageReference Include="NLog.Extensions.Hosting" Version="1.7.2" />
    <PackageReference Include="Skarabee.Extensions.Core" Version="1.2.0-alpha08" />
    <PackageReference Include="Skarabee.Extensions.Microsoft.AspNet" Version="1.1.0-rc06" />
    <PackageReference Include="Skarabee.Extensions.Newtonsoft.Json" Version="1.0.0" />
    <PackageReference Include="Skarabee.Extensions.System.Net.Http" Version="1.1.0-alpha03" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="SauceControl.InheritDoc" PrivateAssets="all" Version="1.3.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Skarabee.CMS.Facade\Skarabee.CMS.Facade.csproj" />
    <ProjectReference Include="..\Skarabee.CMS.Infrastructure\Skarabee.CMS.Infrastructure.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="App_Code\*.cshtml" />
    <Content Include="Content\**" />
    <Content Include="Scripts\**" />
    <Content Include="Views\**" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="appsettings.json" />
    <Content Include="engineConfiguration.config" />
    <Content Include="Global.asax" />
    <Content Include="NLog.config" />
    <Content Include="robots.txt" />
  </ItemGroup>

  <ItemGroup>
    <None Include="Properties\launchSettings.json" />
    <None Include="Web.Debug.config" DependentUpon="Web.config" />
    <None Include="Web.Release.config" DependentUpon="Web.config" />
  </ItemGroup>

</Project>
CZEMacLeod commented 2 years ago

There are a few possibilities here. The main one that springs to mind is if you have the LSP Razor editor enabled. There is an upcoming workaround to ensure you are using the 'legacy' editor which supports ASP.NET 4.x - https://github.com/dotnet/razor-tooling/pull/5781 For now, you can go to Options -> Text Editor -> HTML -> Advanced and select "Use legacy Razor editor for ASP.NET Core" in VS2022. In VS2019 make sure you don't have the option set to use the new editor in Options -> Preview Features. The other thing to check is your web.config file has the appropriate includes and binding redirects for MVC, Razor etc. (check the example project for settings.) There is a feature to overwrite the web.config with the correct binding redirects (per the comment in your project file). https://czemacleod.github.io/MSBuild.SDK.SystemWeb/Binding_Redirects/Autogenerating-Binding-Redirects.html

stevenvolckaert commented 2 years ago

Thanks for these suggestions @CZEMacLeod, I'll try them out in the morning and report back here.

Have a great day!

stevenvolckaert commented 2 years ago

Setting Use legacy Razor editor for ASP.NET Core (requires restart) to True helped, thank you @CZEMacLeod!

I guess on the next Visual Studio release, I could test if the LSP Razor editor is included. If I understood correctly, I could then revert Use legacy Razor editor for ASP.NET Core (requires restart) back to its original setting False?

CZEMacLeod commented 2 years ago

@stevenvolckaert With the new detection code, it should simply be a case of adding

  <ItemGroup>
    <ProjectCapability Include="LegacyRazorEditor" />
  </ItemGroup>

to your project (or to the MSBuild.SDK.SystemWeb itself which is the plan, but I will wait for the release of the tooling and test before updating the SDK). Note - if you use ASP.NET 4.x Razor files in a class library (like with RazorGenerator/RazorGenerator) then you will probably need to add the ProjectCapability manually to those projects too.

stevenvolckaert commented 2 years ago

@CZEMacLeod OK, thank you again for your help! Much appreciated!