question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Minimal Console errors out with: Object reference not set to an instance of an object.

See original GitHub issue

Error

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.Owin.Hosting
  StackTrace:
   at Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor()
   at Microsoft.Owin.Hosting.Utilities.SettingsLoader.<LoadFromConfig>b__0()
   at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Func`1 valueFactory)
   at Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary`2 settings)
   at Microsoft.Owin.Hosting.Engine.StartContext..ctor(StartOptions options)
   at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider services, StartOptions options, Action`1 startup)
   at Program.<Main>$(String[] args) in C:\Projects\personal\Delete\TestServer\Program.cs:line 7

Just run the following code.

using Microsoft.Owin.Hosting;
using Owin;

string BaseUri = @"http://localhost:19000";
List<string> payloads = new();
var builder = WebApp.Start(BaseUri, app =>
{
    app.Run(async context =>
    {
        using StreamReader reader = new(context.Request.Body, System.Text.Encoding.UTF8);
        context.Request.Body.Position = 0;
        payloads.Add(await reader.ReadToEndAsync());
        context.Response.ContentType = "application/json";
        await context.Response.WriteAsync(@"{""Success"":true}");
    });
});

Console.In.ReadLine();
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.9" />
  </ItemGroup>

</Project>

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Tratchercommented, Oct 5, 2022

For migration you’re better off using the OWIN shim in AspNetCore to run on the servers there. That way you don’t need to bring in extra libraries like HttpListener which we know aren’t compatible.

1reaction
CZEMacLeodcommented, Oct 5, 2022

@vandanv Selfhost doesn’t work on net5/6. You can host it under kestrel in the asp.net core pipeline as shown in my example. It should also work under IIS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object Reference Not Set to an Instance of an Object
This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a...
Read more >
c# - Object reference not set to an instance of an ...
Consider this code: String s = null; Console.WriteLine(s.Length);. This will throw a NullReferenceException in the second ...
Read more >
Error: "Assign: Object reference not set to an instance of ...
Hi, so basically I want to loop through every sheet that exists in a workbook. Then, I found this specific solution: How to...
Read more >
Fix: "Object Reference Not Set to an Instance of ...
This error code will show when a Microsoft Visual Studio object is missing, categorized as null, or cannot be accessible.
Read more >
Backups fail with error: Object reference not set to an ...
I only have full system backups. Would you like me to try a new backup job with a small set of files to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found