How to resolve the Testserver problem in XUnit Test? - asp.net-core

I am using xunit for testing my project. I was added all dependencies but i am getting following error.
The type or namespace name 'TestServer' could not be found (are you missing a using directive or an assembly reference?)
My test3.Tests.csproj dependencies
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
How can i resolve the problem.Any one try to help me thank you..

Try to install Microsoft.AspNetCore.Mvc.Testing.
Infrastructure components, such as the test web host and in-memory test server (TestServer), are provided or managed by the Microsoft.AspNetCore.Mvc.Testing package. Use of this package streamlines test creation and execution.
Test app prerequisites

Related

ASP.NET Core Controllers Fail to Register with Authorization Package

My asp.net core 3.1 project throws an exception if I include this package (context: I’m trying to create a custom Authorization policy):
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
This is the exception and method where it is thrown in startup.cs:
services.AddControllers();
System.ArgumentNullException: 'Value cannot be null. (Parameter 'configure')'
This is the complete list of packages. Is there some conflict that I'm not aware of? The package was suggested by VS when I created an AuthorizationHandler class.
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.0.1" />
<PackageReference Include="Azure.Identity" Version="1.2.3" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.1.0" />
<PackageReference Include="CorrelationId" Version="3.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.15.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.9" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.2" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.2.0" />
<PackageReference Include="NSwag.AspNetCore" Version="13.8.2" />
Oops, looks like the answer was already available with a little more effort on my part. I resolved this by manually downgrading the package version to 3.1.12 as described here. Looks like VS gave me the .net version of the package instead of the .net core version.
I was using an authorization handler form a .NET Standard 2 library where v.6.0.x was installed. Downgrading was not an option for me.
Exchanging services.AddControllers() with services.AddMvcCore() in my Core 3.1 project solved the problem for me.

Unable to add migration on dotnet

Hello everyone I am trying to add migration on my project by following code
add-migration InitialCreate
I am the facing issue while adding migration. The error says
Method not found:
'Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder
Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.HasIndex
What's going on here. How to solve this????
Packages installed on my project
There might problen of version mismatching. Please update your Openiddict to 2.0.1 like
<PackageReference Include="OpenIddict" Version="2.0.1" />
<PackageReference Include="OpenIddict.EntityFrameworkCore" Version="2.0.1" />
<PackageReference Include="OpenIddict.Mvc" Version="2.0.1" />

Azure DevOps build pipline constantly giving version conflict on every package

I have a simple .NET Core 2.2 website along with Microsoft.Identity.Web added as a .NET Core assembly. The following .csproj files make up my solution:
Main project:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<WebProject_DirectoryAccessLevelKey>1</WebProject_DirectoryAccessLevelKey>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Graph" Version="1.14.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.Identity.Web\Microsoft.Identity.Web.csproj" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services" />
</ItemGroup>
</Project>
Microsoft.Identity.Web project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.2.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="3.0.6-preview" />
</ItemGroup>
</Project>
NOTE: I can build the solution locally without issue and have never had an issue with it building or running -ever.
I created a new Azure DevOps pipeline and pointed it at my Github repo. During the NuGetCommand process it fails telling me:
[error]The nuget command failed with exit code(1) and error(NU1607: Version conflict detected for Microsoft.AspNetCore.Antiforgery. Reference the package directly from the project to resolve this issue.
Based on this I thought I needed to install that package directly so I went to NuGet package manager, searched for the package, and installed it. Then I get a different error:
[error]The nuget command failed with exit code(1) and error(NU1607: Version conflict detected for Microsoft.AspNetCore.Authentication.Cookies. Reference the package directly from the project to resolve this issue.
Tried installing that package and I get another error:
[error]The nuget command failed with exit code(1) and error(NU1607: Version conflict detected for Microsoft.AspNetCore.Authorization.Policy. Reference the package directly from the project to resolve this issue.
..and so on and so on...
So installing these erroneous packages is not likely the right way forward so I also tried:
Changing my main .csproj file to use <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" /> but that just gave more of the same above errors (different ones) and according to Nate here we shouldn't be including explicit versions.
At one point after installing all these packages it seemed to be hung on the <PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.2.0" /> reference so following guidance from a colleague I changed that version to 2.1.0 but again, no resolution; just more red herrings.
I've been fighting with this for four days now and I'm completely lost on how to solve it. Any guidance would be appreciated.
Update:
Initially I had set up my DevOps build pipeline by following the 'wizard' steps within DevOps itself. I chose my repo and it selected my master branch automatically including setting up a yaml file. I ran the build as the last step in the wizard and that's where my problems started.
I thought I would take a different approach in troubleshooting by setting up my Azure resource group first, then my app service, dependent PaaS services, and finally configure the 'deployment options' section. I followed the steps there to choose Azure DevOps as my build/deploy pipeline, selected my organization, repo, and clicked finish. It set up the build and deploy without issue.
I'll leave this post here for suggestions and hopefully have a definitive answer to why it didn't work. I feel like the errors with my first approach were red herrings all together though...

Installed NuGet package, Visual Studio claims assembly not referenced

you can see that I have referenced it through nuget, but is still complaining. This is in a .net 4.6.1 framework class library in an embedded view component.
I'm also using a .net framework asp.net core web app.
I'm having a bunch of issues trying to get razor to work, but this one is a new one. MenuViewPage inherits RazorPage and is located in another assembly.
I think I should just install .net core 2, and aspnetcore.all ;)\
Anyone have any ideas why this is happening?
So after hours of trying to sort this issue out I've come to a solution:
When creating a web class library, whether you are using it for application parts or you are creating your core project with base classes I advise you covert it from a normal .net class library to a web enabled one.
To convert it to a web class library you need to change the project to use the following .csproj, then make sure to run dotnet restore.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
<PreserveCompilationContext>true</PreserveCompilationContext>
<ApplicationIcon />
<OutputTypeEx>library</OutputTypeEx>
<StartupObject />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile></DocumentationFile>
<NoWarn>1701;1702;1705;1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor">
<Version>1.1.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime">
<Version>1.1.2</Version>
</PackageReference>
<PackageReference Include="System.Net.Http" Version="4.3.2" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
</ItemGroup>
</Project>
Make sure you are making the output type a library.
I ran into issues with the assembly info file, it seems to be generated during build now, so I commented out the assembly.info stuff by double clicking "Properties"
I hope this helps someone in the future.

Dotnet watcher - Illegal characters in path -- what could cause this error?

Can anyone help us understand why dotnet watch is causing our applications to crash?
I'm using dotnet new mvc and yo aspnetcore-spa to create apps in visual studio code. Both work fine until I try to add a reference to DotNet Watcher tools using example at https://github.com/aspnet/DotNetTools/tree/dev/src/Microsoft.DotNet.Watcher.Tools to insert reference in csproj file:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" />
</ItemGroup>
After adding to csproj and dotnet restore, the application throws an error and fails and is unrecoverable. It has to be deleted and I have to start from scratch.
Full csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>
netcoreapp1.1
</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" />
</ItemGroup>
</Project>
The error thrown is below and happens no matter where we create the application on disk or what it is named.
C:\Program Files\dotnet\sdk\1.0.1\Sdks\Microsoft.NET.Sdk.Publish\build\netstandard1.0\Microsoft.NET.Sdk.Publish.targets(90,5): error MSB4184: The expression "[System.IO.Path]::GetFullPath(C:\test\obj\Debug\ [C:\test\test.csproj]
C:\Program Files\dotnet\sdk\1.0.1\Sdks\Microsoft.NET.Sdk.Publish\build\netstandard1.0\Microsoft.NET.Sdk.Publish.targets(90,5): error MSB4184: netcoreapp1.1 [C:\test\test.csproj]
C:\Program Files\dotnet\sdk\1.0.1\Sdks\Microsoft.NET.Sdk.Publish\build\netstandard1.0\Microsoft.NET.Sdk.Publish.targets(90,5): error MSB4184: \PubTmp)" cannot be evaluated. Illegal characters in path. Parameter name: path [C:\test\test.csproj]
Our environment is:
.NET Command Line Tools (1.0.1)
Microsoft .NET Core Shared Framework
Host Version : 1.1.0
Build: 928f77c4bc3f49d892459992fb6e1d5542cb5e86
Visual Studio Code v 1.10.2
Windows 10
Problem solved. An extension named "XML Formatter" was rewriting the csproj file on save and adding hard returns and tabs. Removing this extension, restoring csproj to it's original state, adding back in the DotNetCliToolReference code block and rebuilding via dotnet restore solved the issue.