Visual Studio 版本 17.6.1 与 Blazor .NET 6 项目不兼容,出现(虚假?)错误。

huangapple go评论57阅读模式
英文:

Visual Studio Version 17.6.1 breaks Blazor .NET 6 Project with (false?) Errors

问题

I am working on a ASP.NET Blazor Server Project, .NET 6 (LTS).
Everything worked fine in the previous VS 2022 Version (even before 17.4.4 through 17.5.*) but the new Update to 17.6.1 breaks the code: The Project won't compile anymore. I have copied and tested the exact same code across different Computers and VS2022 Versions.

It seems like that:
The first Bug is created by MyParent=this, which makes the Parent give its own instance as Parameter to the Child. (This trick is often used in examples and used to work in previous versions.) This will create the first two lines of error (CS0400 and CS1503).

When wrapping the Child component into the Wrapper, this will create the CS1662 Error.

Removing all in-going parameters lets the project compile.

@*this compiles*@
<Wrapper>
    <Child></Child>
</Wrapper>

I have also successfully recreated the (two) Bugs within the default Blazor Server Template.

Parent.razor:

@page "/parent"
<h3>Parent</h3>

<div>
	<h6>This will create both bugs:</h6>
	<p>"namespace bug" (CS0400 and CS1503)</p>
	<p>"lambda expression bug"(CS1662)</p>
	<Wrapper>
    	<Child MyParent=this></Child>
	</Wrapper>
    @*
    alt version (longer)
	<Wrapper Context="MyContext">
		<Child MyParent=this MyObj=MyContext.MyData></Child>
	</Wrapper>
    *@
</div>


@*
<!-- the followling block would create only the first bug (with two errors). to see this, comment out the block above.-->

<div>
	<h6>This will create only the first ("namespace") bug:</h6>
	<p>"namespace bug" (CS0400 and CS1503)</p>
	<Child MyParent=this></Child>
</div>
*@

@code {
	public string MyName { get; set; } = "I am the parent.";
}

Wrapper.razor:

@using VS17._6._1BUG_Test.Data;
<h3>Wrapper</h3>


@code {
	[Parameter]
	public RenderFragment<MyClass> ChildContent { get; set; }
}

Child.razor:

<h3>Child2</h3>

<h6>my text:</h6>
<p>
	@MyObj.ToString()
</p>
<h6>parent data:</h6>
<p>@MyParent.MyName</p>

@code {
	[Parameter]
	public object MyObj { get; set; }
	[Parameter]
	public Parent MyParent { get; set; }
}

MyClass.cs:

namespace VS17._6._1BUG_Test.Data
{
	public class MyClass
	{
		public object MyData { get; set; } = "default data";
	}
}

I am hoping on a timely Bugfix from Microsoft. If anybody has any other suggestions, they are welcome also. If this problem persits I would probably think of downgrading VS 2022 to 17.5, although that seems to be my last resort and also probably kind of difficult. Even a complete re-install to an earlier Version if probably a bit more complicated, since I don't think that can be easily done with the VS-installer.

Update:

I have noticed, that the VS-installer actually now provides an option to rollback to previous version. So I'm hesitant to update to 17.6.2 to test if its fixed (then I would only be able to roll back to 17.6.1), but I will probably roll back to 17.5 for now.

Update:

It seems that in the preview version 17.7.0 preview 1 this bug is not longer surfacing, although I have not found a specific fix for it in the release notes (did not search too deep).

英文:

I am working on a ASP.NET Blazor Server Project, .NET 6 (LTS).
Everything worked fine in the previous VS 2022 Version (even before 17.4.4 through 17.5.*) but the new Update to 17.6.1 breaks the code: The Project won't compile anymore. I have copied and tested the exact same code across different Computers and VS2022 Versions.

Visual Studio 版本 17.6.1 与 Blazor .NET 6 项目不兼容,出现(虚假?)错误。

It seems like that:
The first Bug is created by MyParent=this, which makes the Parent give its own instance as Parameter to the Child. (This trick is often used in examples and used to work in previous versions.) This will create the first two lines of error (CS0400 and CS1503).

When wrapping the Child component into the Wrapper, this will create the CS1662 Error.

Removing all in-going parameters lets the project compile.

@*this compiles*@
<Wrapper>
    <Child></Child>
</Wrapper>

I have also successfully recreated the (two) Bugs within the default Blazor Server Template.
Visual Studio 版本 17.6.1 与 Blazor .NET 6 项目不兼容,出现(虚假?)错误。

Visual Studio 版本 17.6.1 与 Blazor .NET 6 项目不兼容,出现(虚假?)错误。

Parent.razor:

@page "/parent"
<h3>Parent</h3>

<div>
	<h6>This will create both bugs:</h6>
	<p>"namespace bug" (CS0400 and CS1503)</p>
	<p>"lambda expression bug"(CS1662)</p>
	<Wrapper>
    	<Child MyParent=this></Child>
	</Wrapper>
    @*
    alt version (longer)
	<Wrapper Context="MyContext">
		<Child MyParent=this MyObj=MyContext.MyData></Child>
	</Wrapper>
    *@
</div>


@*
<!-- the followling block would create only the first bug (with two errors). to see this, comment out the block above.-->

<div>
	<h6>This will create only the first ("namespace") bug:</h6>
	<p>"namespace bug" (CS0400 and CS1503)</p>
	<Child MyParent=this></Child>
</div>
*@

@code {
	public string MyName { get; set; } = "I am the parent.";
}

Wrapper.razor:

@using VS17._6._1BUG_Test.Data;
<h3>Wrapper</h3>


@code {
	[Parameter]
	public RenderFragment<MyClass> ChildContent { get; set; }
}

Child.razor:

<h3>Child2</h3>

<h6>my text:</h6>
<p>
	@MyObj.ToString()
</p>
<h6>parent data:</h6>
<p>@MyParent.MyName</p>

@code {
	[Parameter]
	public object MyObj { get; set; }
	[Parameter]
	public Parent MyParent { get; set; }
}

MyClass.cs:

namespace VS17._6._1BUG_Test.Data
{
	public class MyClass
	{
		public object MyData { get; set; } = "default data";
	}
}

I am hoping on a timely Bugfix from Microsoft. If anybody has any other suggestions, they are welcome also. If this problem persits I would probably think of downgrading VS 2022 to 17.5, although that seems to be my last resort and also probably kind of difficult. Even a complete re-install to an earlier Version if probably a bit more complicated, since I don't think that can be easily done with the VS-installer.

Update:

I have noticed, that the VS-installer actually now provides an option to rollback to previous version. So I'm hesitant to update to 17.6.2 to test if its fixed (then I would only be able to roll back to 17.6.1), but I will probably roll back to 17.5 for now.

Update:

It seems that in the preview version 17.7.0 preview 1 this bug is not longer surfacing, although I have not found a specific fix for it in the release notes (did not search too deep).

答案1

得分: 0

I've run into the same problem with Visual Studio - my code works on 15.X, and doesn't work on 16.X. I managed to track it down to a razor base class with generic parameters:

ElementBase.razor

@typeparam TElementType where TElementType : Element

@code {
    [Parameter]
    public TElementType Model { get; set; } = default!;

    public void Foo()
    {
        // ...
    }

    public void Bar()
    {
        // ...
    }
}

Since this is a base class, it doesn't define any markup - anything it defines would be overridden by child classes anyway. I changed it to be a plain C# class instead, and the error went away:

public abstract class ElementBase<TElementType> : ComponentBase
{
    [Parameter]
    public TElementType Model { get; set; } = default!;

    public void Foo()
    {
        // ...
    }

    public void Bar()
    {
        // ...
    }
}
英文:

I've run into the same problem with Visual Studio - my code works on 15.X, and doesn't work on 16.X. I managed to track it down to a razor base class with generic parameters:

ElementBase.razor

@typeparam TElementType where TElementType : Element

@code {
    [Parameter]
    public TElementType Model { get; set; } = default!;

    public void Foo()
    {
        // ...
    }

    public void Bar()
    {
        // ...
    }
}

Since this is a base class, it doesn't define any markup - anything it defines would be overridden by child classes anyway. I changed it to be a plain C# class instead, and the error went away:

public abstract class ElementBase&lt;TElementType&gt; : ComponentBase
{
    [Parameter]
    public TElementType Model { get; set; } = default!;

    public void Foo()
    {
        // ...
    }

    public void Bar()
    {
        // ...
    }
}

答案2

得分: 0

"As of Release Version 17.6.3 the bug seems to be resolved. (VS is able to build again and the 'errors' are no longer shown.)

As a workaround Preview 17.7.0 (preview 1 and 2) also seem to work."

英文:

As of Release Version 17.6.3 the bug seems to be resolved. (VS is able to build again and the "errors" are no longer shown.)

As a workaround Preview 17.7.0 (preview 1 and 2) also seem to work.

huangapple
  • 本文由 发表于 2023年5月26日 01:29:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定