Using Web Api 2 as Self Console Host in C# - c#

I want to build a self Host of a Web Api 2 Webservice.
I am using the .Net Core 1.0 Framework for my Console App and also for the Web Api 2 Project.
Since there was a name change for the packages and Owin is deprecated, I am not able to rebuild the given samples in the official GitHub repository of ASPnet:
https://github.com/aspnet/Hosting/blob/dev/samples/SampleStartups/StartupHelloWorld.cs
(I tried with this one)
I looked in the project.json file which packages they are using but since I can´t find the AspNetCore.Hosting package only a package named AspNet.Hosting but i isn´t working with both packages.
{
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNetCore.Hosting": "1.0.0-*",
"Microsoft.NETCore.Platforms": "1.0.1-*"
},
Resharper says something like :
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'WebHostBuilder' could not be found (are you missing a using directive or an assembly reference?) WebApiConsoleHost.DNX 4.5.1, WebApiConsoleHost.DNX Core 5.0
Just to be complete, I used the whole Main Function Body of the given sample in the Link:
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseDefaultConfiguration(args)
.UseStartup<StartupHelloWorld>()
.Build();
host.Run();
}
Do someone know where the WebHostBuilder class is defined ? Resharper suggests that it is in AspNet.Hosting, but after adding the using it is still not found!

Try to read the latest docs. It is working for me.

Related

Can't add IdentityServer4 NuGet package to ASP.NET Core project properly and VS Code is not showing intellisense

I've added IdentityServer4 NuGet package to ASP.NET core project using dotnet cli. Then written the following code on Startup class.
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddInMemoryClients(new Client[]
{
new Client
{
ClientId = "react client",
ClientName = "React Client",
AllowedGrantTypes = GrantTypes.Implicit,
RedirectUrls = {"http://localhost:51009/"},
AllowedScopes = { "openid"}
}
});
services.AddMvc();
}
Then when I build the project it shows error: The type or namespace name 'Client' could not be found (are you missing a using directive or an assembly reference?)
Moreover VS Code doesn't show any suggestion for IdentityServer related code.
And when I search IdentityServer4 on NuGet website it shows result like following:
And when I search IdentityServer4 on Visual Studio 2017 it shows result like following:
The IdentityServer4 Client class is located in the IdentityServer4.Models namespace which is included in the nuget-package.
Adding the using-statement where the model/models are needed should resolve the issue
using IdentityServer4.Models
VS Code was not showing intellisense because of an OmniSharp bug. Solution to this problem is restarting OmniSharp.
Ctrl + Shift + P and then type Restart OmniSharp

How do you unit test ASP.NET Core Web Application (.Net Framework)?

I'm building an ASP.NET Core Web Application (.Net Framework) and am having a hard time figuring out how to hook unit tests up to it. I am targeting the .net framework version 4.6.1
If I create a regular "Class Library" project targeting 4.6.1, as I would for previous version of MVC, it lets me add references to my MVC project (which is part of the same solution) but any namespaces I add through a using statement report an error that I might be missing a reference or using statement. If I double click on the reference under the "References" section in the solution explorer it tells me that the project can't be found or hasn't been build yet.
I tried creating a "Class Library (.NET Core)" but that complains since I'm targeting .Net Framework and not .NET Core. I edited the class libaries Project.json to have it target the .net framework and that lets me add my references and doesn't complain when I the namespaces in a using statement but none of my tests are discovered by the test runner. I've tried both XUnit and NUnit and they both behave the same.
Is it possible to unit test an ASP.Net Core Web Application targeting the .Net Framework 4.6.1 or do I need to commit to the .NET Core?
Edit to add my test class
Here is my test class stripped down to the bare minimum. TestBank.Services is the class I want to test.
using System;
using TestBank.Services;
using Xunit;
namespace TestBankUnitTests
{
public class Class1
{
[Fact]
public void TestA()
{
Assert.True(false);
}
}
}
and here is my project.json
{
"version": "1.0.0-*",
"dependencies": {
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-rc2-build10025",
"TestBank": "1.0.0-*"
},
"frameworks": {
"net461": {
}
}
}
Your project.json needs a testRunner setting. Per the project.json documentation, the testRunner setting not only specifies which test framework to use, but also marks the project as a test project.
Try adding it and see if it finds your tests (verified locally that it will not find tests without this setting in the file)
{
"version": "1.0.0-*",
"dependencies": {
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-rc2-build10025",
"TestBank": "1.0.0-*"
},
"frameworks": {
"net461": {
}
},
"testRunner": "xunit"
}
I was having a similar issue and found a solution. I am posting it here in case it helps someone. What I have learned is that using an xproj library targeting net461 to test an Asp.Net Core Project (.Net Framework) project targeting net461 and a Windows class library targeting framework 4.61 works but the setup seems to be very finicky and fragile. The key insight for me came from this thread https://github.com/aspnet/Tooling/issues/245 where #BradRem indicated that the folder structure of the projects seemed to the source of issues.
Initially I tried unsuccessfully to use this folder structure:
src
____Asp.Net Core Project (.Net Framework) project targeting Net461
____Windows class library targeting framework 4.61
test
____Core Library used to run xUnit Tests
But when I tried to run the tests using this folder structure it produced the exception that starts off like this:
Unable to start C:\Program Files\dotnet\dotnet.exe
dotnet-test Error: 0 : [ReportingChannel]: Waiting for message failed System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.Stream.ReadByte()
at System.IO.BinaryReader.ReadByte()
at System.IO.BinaryReader.Read7BitEncodedInt()
at System.IO.BinaryReader.ReadString()
at Microsoft.DotNet.Tools.Test.ReportingChannel.ReadMessages()
But when I changed to the following folder structure I was able to get it to work:
src
____Asp.Net Core Project (.Net Framework) project targeting Net461
____Windows class library targeting framework 4.61
____Core Library used to run xUnit Tests
So the key was putting the folder that housed the testing class library in the src folder where the other projects folders were.
That said, the other thing that seemed to make a big difference was to add the references to the other two projects to the Core Library test project at the same time rather than one at a time.
I have found something that works for me.
Inside my test project, my project.json file looks like this:
{
"version": "1.0.0-*",
"testRunner": "xunit",
"dependencies": {
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Ng2a.WebApi": "1.0.0-*"
},
"frameworks": {
"net452": {
}
}
}
'Ng2a.WebApi' is my web api core project. It targets only net452.
The Ng2a.WebApi project.json file looks like this:
"frameworks": {
"net452": {}
},

proper way to sign .net core assembly

I'm trying to sign a .net core lib, and I'm just not sure if I did everything correctly
1) using VS Command Promp I called sn -k mykey.snk
2) copied mykey.snk to myproject folder
3) in project.json added keyfile
"frameworks": {
"netstandard1.6": {}
},
"buildOptions": {
"outputName": "MyLib",
"keyFile": "mykey.snk"
}
is this correct, is the library (dll) going to be usable on both .net core and full .net 4.6 apps ?
Yes, this is the correct way. If you look into any ASP.NET Core projects, like Logging, you will find
"buildOptions": {
"keyFile": "../../tools/Key.snk"
...
}
in project.json file, and Key.snk in Tools folder. You also may check .NET Core - strong name assemblies issue.

vnext (asp .net 5.0) and oracle

I created a vnext solution in visual studio ultimate 2015 CTP version 14.0.22609.0 D14REL and in the package manager I added the oracle managed driver.
Install-Package odp.net.managed
then in my config.json
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-vnext-237fb18c-c414-44a8-8771-e02d4719d1dc;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"hr": {
"ConnectionString": "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=mydatabase))); User Id=hr; Password=xxxxxxx;", "providerName":"oracle.manaagedatacess.client"
}
},
when I attempted to use it in a class
using System;
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
namespace vnext.Models
{
internal class dataHelper
{
OracleConnection cn = new OracleConnection(ConfigurationManager.ConnectionStrings["hr"].ConnectionString);
}
}
lots of compile errors such as the type or namespace Oracle, Configuration, and OracleConnection could not be found are you missing an assembly reference? project vnext asp.net Core 5.0
Christian Shay is right... But there is a tweak. I didn't tested but just try to comment the dnxcore50 in project.json as follows:
"frameworks": {
"dnx451": { }
},
Since the default web template targets both the full .NET and .NET Core, the oracle managed driver framework is currently not available in .NET Core. so you can comment it out. Now you should be able to build and run the solution with out error.
Update :
Removed
/* dnxcore50": { } */

Can't Add System.IdentityModel package/reference

Long story short I want to create authentication for my WCF service.
Target framework: .NET 4.5.
I'm following this User Name Password Validator.
I'm trying to inherit from UserNamePasswordValidator class but VS2013 keep giving me this error :
My code:
public class UserNamePassValidator : System.IdentityModel.UserNamePasswordValidator
{
}
The error i'm getting:
Error 1 The type or namespace name 'IdentityModel' does not exist in the namespace 'System' (are you missing an assembly reference?)
I've tried a couple of diffrent ways to solve this: (none of them help me)
I've installed Windows Identity Foundation and the WIF SDK.
I've add dll manually with this path: C:\Program Files\Reference Assemblies\Microsoft\Windows Identity Foundation\v3.5\ Microsoft.IdentityModel.dll
I try to search for that package in the Nuget Package Manager but nothing come up, only System.IdentityModel.Token (I reference it too but it didn't help).
What am i missing here ? its probably something obvious that I don't see...
If the Target Framework is 4.5, then there is no package to download in order to use WIF. System.IdentityModel is fully integrated to Framework 4.5 and it is the type used by WIF when targeting Framework 4.5 and higher.
Also, you should inherit from this type: System.IdentityModel.Selectors.UserNamePasswordValidator:
public class UserNamePassValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
return;
}
}​
You can refer to the links below:
1- UserNamePasswordValidator
2- What do I need to build a claims-aware wcf service in VS 2013

Categories

Resources