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)

3
  • 1
    Does this source code useful to you ? Commented Dec 21, 2021 at 9: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 github.com/NetCoreTemplates/selfhost/blob/master/MyApp/…). I hoped I could simply add some nuget package(s) directly. Commented Dec 22, 2021 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". Commented Dec 22, 2021 at 10:42

3 Answers 3

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()}");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.