Problem with .NET web application using Powershell SDK - c#

I don't know what else to test hence my question here.
The goal is to continue using my Visual Studio project set to<Project Sdk="Microsoft.NET.Sdk.Web"> and being able to obtain a Powershell session via the Powershell.SDK nuget package.
I noticed that running "powerShell.Invoke()" with a Visual studio project set to <Project Sdk="Microsoft.NET.Sdk"> will give me a session as expected. However using Sdk.Web always returns an error => "An error occured that Powershell could not handle".
I even added a project reference to a console application in order to get the session working but the result is the same as explained above.
Am I doing something wrong or is there any workaround?
Could I change my .net web application to a console app and still host it on IIS?
Thank you.

Was able to fix this by adding
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
and
<PackageReference Include="Microsoft.Management.Infrastructure" Version="1.0.0" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.4" />
<PackageReference Include="System.Management.Automation" Version="6.0.4" />
to csproj file as stated here: Issue
Also I had to set the RuntimeIdentifier to win7-x64 in my publish profile:
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>

Related

Cannot Migrate Blazor App to .Net 5 Due to System.Runtime Error

I have been working through an awesome tutorial within Udemy to learn more about Blazor (https://www.udemy.com/course/programming-in-blazor-aspnet-core/), but have hit a stumbling block that I'm not entirely sure what to do with.
Short Version
When upgrading to .Net 5 from .Net Standard 2.1, I end up with this error when trying to run this sample Blazor application as soon as it loads up (so it's not hitting any of my code): System.TypeLoadException: Could not resolve type with token 01000014 from typeref (expected class 'System.Threading.Tasks.Task' in assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') I see a similar problem with this SO link, but it didn't really give me much to go off of.
Detailed Version
With prior versions of .Net, you installed the latest, then Visual Studio picked that up, you switched projects and away you went - everything was seamless and just worked. With some of the newer stuff though, Microsoft's messaging has been extremely confusing and the problem I'm hitting now is inside that Udemy tutorial I need to utilize the IJSObjectReference interface to do something. When I first added that to the code, the type reference couldn't be resolved so a quick search pointed me to needing to move the project to .Net 5 by changing this:
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
to this (because Visual Studio doesn't always show .Net 5 as an option):
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
Seemed simple enough, so I changed the Client Blazor project to this and tried to compile. That gives me this error: Project BlazorMovies.Client is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Project BlazorMovies.Client supports: net5.0 (.NETCoreApp,Version=v5.0). I figured okay, I'll bump Server to 5.0 next and then everything compiles fine, but as soon as I pull it up, I get this error: System.TypeLoadException: Could not resolve type with token 01000014 from typeref (expected class 'System.Threading.Tasks.Task' in assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'). Then I remembered not recalling if I'd installed .Net 5 yet, so I went to check and (via learn.microsoft.com) I only have 4.8.03752 installed. I then did some searches to try and find the .Net installers and there were multiple (see here) - even the layout of the page is really overwhelming, with ~20 install links scattered throughout. I knew I needed at least x64, so I first installed the SDK since it said Visual Studio support and that went significantly faster than I expected (based on prior installs of .Net), but now VS is showing .Net 5 which seemed promising! I re-checked the registry though, and it still says 4.8.03752 and when I went to Add/Remove programs, .Net 5 doesn't show up like all the other versions. I next installed the Hosting Bundle which said it was successful, but the sample app still has the exact same error.
Any advice? I know Blazor is quite new, but with Microsoft's extremely confusing messaging between .Net Framework, .Net Standard, .Net Core and now a migration back into .Net 5 that seems to need multiple installers, I don't really know where to go next. That error is entirely generated from within the Web Assembly code according to the stack trace, so it doesn't appear to be anything related to what I'm doing. Here's a screenshot of everything Chrome shows me in the console:
To migrate Blazor Webassembly Application from netstandard2.1 to .NET 5, you could refer the following steps:
Install or update Visual Studio 2019 to version 16.8.0+, and install the .NET 5 SDK.
Change the Blazor Webassembly project setting.
Create a Blazor Webassembly project (When create the application, select .net core 3.1, then, the TargetFrameWork will be .netstandard2.1), Open the application .csproj file:
Update the SDK from Microsoft.NET.Sdk.Web to Microsoft.NET.Sdk.BlazorWebAssembly
Set the TargetFramework to net5.0
In case you have it, remove the package reference to Microsoft.AspNetCore.Components.WebAssembly.Build.
After updating, the .csproj file content as below:
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
</ItemGroup>
</Project>
Update the Nuget dependencies version.
The result like this:
Then, the final .csproj file content looks as below:
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.2" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
</ItemGroup>
</Project>
Clean the entire solution, otherwise the build engine won’t be able to re-generate all the required files with the updated framework.
Then running the application, the website works well.
Have you changed the header node in the *.csproj too?
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
...
</Project>

Intalling Microsoft.Net.Test.Sdk did not fix "Unable to findtesthost.dll. Please publish your test project and retry." - Anything else I could do?

UPDATE: I didn't realize that I was using Class Library (.NET Standard) instead of Class Library (.NET Framework) or that the two are different to be honest. I'm very new with C#. So when I changed to .NET Framework from .NET Standard, my code started working properly.
I want to create a UI automation framework using C# and Selenium and I followed some tutorials online on how to do this. I installed the following using NuGet Manager in Visual Studio:
Selenium.Webdriver
NUnit
NUnit3TestAdapter
I proceeded to create a very simple test:
[Test]
public void SampleTest()
{
IWebdriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://sample.com");
}
After building the solution, I ran the test and got this error:
Unable to find ....\testhost.dll. Please publish your test project and retry.
I searched for a solution and many people solved it by installing Microsoft.Net.Test.Sdk so I did the same but the issue still exists.
Anything else I can try?
Here are are my project references:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
</ItemGroup>
</Project>
Got this error, when trying to debug a unit test. Below are the steps I tried.
Step 1: Installed Microsof.TestPlatform.TestHost and tried to run the test but no luck.
Step 2: Changed Target framework from .NET Core 2.0 to 2.1 and tried to run the test but no luck.
Step 3: Closed and opened VS2017 and tried to run. Yay!!! it worked :-)
Never miss to try the last step ;-)
Hope this helps someone like me.

Restoring dependencies in Visual Studio Code is using the wrong source/feed

I'm not sure how to set the default nuget feed for my .net core project in Visual Studio Code to https://api.nuget.org/v3/index.json
When I attempt to add a package (and subsequently, restore dependencies), I get the following errors...
C:\Program Files\dotnet\sdk\2.1.403\NuGet.targets(114,5): error : Unable to load the service index for source https://smartassessor.pkgs.visualstudio.com/_packaging/SANuget/nuget/v3/index.json. [c:\Users\Matthew.OConnor\Desktop\Important Documents\Programming\DatingApp\DatingApp.API\DatingApp.API.csproj]
C:\Program Files\dotnet\sdk\2.1.403\NuGet.targets(114,5): error : Response status code does not indicate success: 401 (Unauthorized). [c:\Users\Matthew.OConnor\Desktop\Important Documents\Programming\DatingApp\DatingApp.API\DatingApp.API.csproj]
This source https://smartassessor.pkgs.visualstudio.com/_packaging/SANuget/nuget/v3/index.json has nothing to do with my current project, however it is used for other projects that are typically run using full blown Visual Studio. Those projects are saved in a completely different place to this project.
I simply want to be able to add nuget packages from nuget.org in my .net core project. How do I do this in VS code?
I don't currently have a nuget.config file in this project.
The package source mentioned in the error appears to be coming from a package source I have setup whilst using Visual Studio
This is my csproj file...
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.4"/>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="4.0.1"/>
<PackageReference Include="CloudinaryDotNet" Version="1.3.1"/>
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.1.0-preview1-final"/>
</ItemGroup>
</Project>
CloudinaryDotNet is the package that generated the errors above.
I think VS Code is just running a dotnet restore, and the reason you're seeing this source being used is because it's configured in your User/Computer nuget configuration file (located on windows, which you seem to be running, at %appdata%\NuGet\NuGet.Config & %ProgramFiles(x86)%\NuGet\Config respectively). The VS configuration editor you showed is just a nice GUI for this configuration file.
If you want to keep this general setting, you should be able to use a nuget.config file in your VS Code project (which you mentioned you don't have at the moment). There is more info on this here -Add custom package source to Visual Studio Code.
Also, if you're trying to restore manually, you can use one of these 2 flags -
dotnet restore --source https://api.nuget.org/v3/index.json
dotnet restore --ignore-failed-sources
These are pretty self explanatory, but you can see the full documentation here - https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-restore?tabs=netcore2x
Hope this helps (:

Error on published ASP.NET core site: Cannot find compilation library location for package 'Microsoft.AspNet.WebApi.Client'

My webb app works fine when run from Visual Studio, but when I publish and try to load a page, I get:
InvalidOperationException: Cannot find compilation library location for package 'Microsoft.AspNet.WebApi.Client'
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List assemblies)
I've been stuck on this for quite a while. I've attempted to apply the various workarounds in the thread https://github.com/dotnet/core-setup/issues/2981, but none of them have worked.
My csproj file is pasted below. I'm not sure what other information would be useful:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<UserSecretsId>aspnet-CrowdQuery2-8C668DB3-5C80-4D9E-851D-2434D0CDA7E9</UserSecretsId>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>
<PropertyGroup>
<RuntimeFrameworkVersion>2.1.2</RuntimeFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.6" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.3" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<Folder Include="ViewModels\" />
</ItemGroup>
</Project>
I've been having the same issue using Microsoft Azure.
The solution in this case was to clean up the wwwroot folder in our web app using Kudu (Development Tools -> Advanced Options), because there were some old DLLs still left from before the upgrade to .NET Core 2.1, because .NET Core 1 publishes the DLLs to the wwwroot folder, whereas in 2.1, the DLLs are loaded from a global store.
After having completely emptied the wwwroot folder, and redeploying the app, the error was resolved and the app ran as expected.
I had to
(1)
Edit .csproj and add
<PropertyGroup>
<MvcRazorExcludeRefAssembliesFromPublish>False</MvcRazorExcludeRefAssembliesFromPublish>
</PropertyGroup>
(2)
Change the publish "Target Runtime" to "win-x64" (it was previously x86). I don't know why this is required, because my project properties -> Platform target is "Any CPU".
(I have two other similar websites in the same solution, neither of those .csproj files require that line, and both are still publishing to x86).

System.ServiceModel not found in .NET Core project

I have a .NET Core xUnit project. I'm trying to call a WCF service from it but get the following exception:
System.InvalidOperationException occurred
HResult=0x80131509
Message=An error occurred while loading attribute 'ServiceContractAttribute' on type 'IMyContract'. Please see InnerException for more details.
Inner Exception 1:
FileNotFoundException: Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
It works with a Framework 4.7 project with the same Nuget Package System.ServiceModel.Http.4.3.0.
Microsoft has made available the relevant assemblies as packages on NuGet now.
System.ServiceModel.Primitives is the base package; add the others if necessary to your project.
Update (April 28th, 2022):
WCF has been partially ported to .NET 5+ with an official library called CoreWCF: https://devblogs.microsoft.com/dotnet/corewcf-v1-released/
If you are using .NET Standard 2.0 (that's what I tested with), you can install compatible NuGet packages.
The basic service model is available in System.ServiceModel.Primitives (currently v4.4.0).
If required, install System.ServiceModel.Http as well.
The Microsoft WCF Web Service Reference Provider wraps SvcUtil.exe and will generate a .NET Standard project from your endpoint. Look in the project file and you'll see the ServiceModel references that will work for you.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.ServiceModel.Duplex" Version="4.3.0" />
<PackageReference Include="System.ServiceModel.Http" Version="4.3.0" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.3.0" />
<PackageReference Include="System.ServiceModel.Security" Version="4.3.0" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
</ItemGroup>
</Project>
When I needed to do this I was able to use the generated class library in my .NET Core project.
I had same issue, i had to add .asmx web service into my class library asp.net core project, i tried to add reference directly by add connected service option but i wan unable to see any config file and lots of references errors also, below are the steps by which i solve my problem.
1-Create library project name Service Mapping
2-Create folder Web References and then create inside folder ClientHotel
3-Open terminal in visual studio from view ---> terminal --- command prompt.
4-Integrate asmx service by command
svcutil http://abctest.asmx (.asmx URL)
inside inside ClientHotel folder.
5-it will create .cs class and output.config file with references in it.
6-Now i had errors of references like
the type or namespace servicecontractattribute does not exist in namespace System.ServiceModel etc.
7-Install Package System.ServiceModel.Primitives
Then all references errors will be gone.
That is how i was able to add .asmx service to my class library project.

Categories