2

I would like to add a simple Web API to an already existing .net backend process. The project is already updated to .net 6.0 and I would like to stay at 6.0. I can't figure out how to add the correct references to my project to be able to self-host a web api within my process.

The goal is to have a single executable (mostly) to copy to a small embedded linux system within which the backend and a webserver (serving the static files and acting as a backend for the served frontend).

The 'old' tutorials (.net 5.0) suggest to add a reference to the nuget package "Microsoft.AspNet.WebApi.OwinSelfHost" but it seems as if that package didn't make the transistion to 6.0. (I get errors on installing it complaining about the target framework being unsupported)

Tobias Schulte
  • 716
  • 5
  • 18
  • 1
    Does [this source code](https://github.com/NetCoreTemplates/selfhost) useful to you ? – Jason Pan Dec 21 '21 at 09:33
  • @JasonPan That's the only source with at least some information on this topic I found as well, but it's basically coming down to changing your sdk projecttype from "Microsoft.NET.Sdk" to "Microsoft.NET.Sdk.Web" (see https://github.com/NetCoreTemplates/selfhost/blob/master/MyApp/MyApp.csproj). I hoped I could simply add some nuget package(s) directly. – Tobias Schulte Dec 22 '21 at 10:41
  • One more thing: after changing the projecttype this way and starting to debug your application once a launchSettings.json file ist created containing additional settings for IIS Express, these settings can simply be removed to prevent running IIS Express. Simply keep the section with your project name within "profiles". – Tobias Schulte Dec 22 '21 at 10:42
  • Does my answer useful to you? If my reply is helpful, please accept it as answer(click on the mark option beside the reply to toggle it from greyed out to fill in.), see https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Jason Pan Dec 23 '21 at 06:14

3 Answers3

4

You can use Microsoft.AspNetCore.Owin in .Net6.

enter image description here

Test Result

enter image description here


Program.cs

using Microsoft.AspNetCore.Hosting;

namespace selfhost
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseUrls("http://*:5000")
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace selfhost
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

Controller file

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace selfhost.Controller
{
    public class SayHiController : ControllerBase
    {
        [Route("sayhi/{name}")]
        public IActionResult Get(string name)
        {
            return Ok($"Hello {name}");
        }
        [Route("getguid")]
        public IActionResult GetGuid(string name)
        {
            return Ok($"{Guid.NewGuid().ToString()}");
        }
    }
}
Jason Pan
  • 15,263
  • 1
  • 14
  • 29
0

I think you should be able to do this without the owin nuget via the WebHost.UseKestrel method

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();

builder.WebHost.UseKestrel(x =>
    x.ListenAnyIP(6969)
);

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseAuthorization();

app.MapControllers();

app.Run();

See the related answer here

Matthew Peterson
  • 1,055
  • 12
  • 21
0

Jason Pan I'm not sure but, it looks like you're not using Owin in project. You have installed package but you don't use Owin namespace and Owin has IAppBuilder not IApplicationBuilder

Correct me please if I'm wrong.

Piotr
  • 21
  • 2