0

Error: An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: The calling thread must be STA, because many UI components require this.

  • I just update my VisualStudio to Version 17.3.5.
  • I added some Nuget Package too.
  • I use PostgreSql as my database.
  • All my Nuget Packages are updated.
  • I had no migration problems before. This error is new.

I already have : Microsoft.EntityFrameworkCore.Tools in the project where my DataContext is defined. Ref: An error occurred while accessing the Microsoft.Extensions.Hosting services when do first migrations

Why this error?

enter image description here

dan1st
  • 12,568
  • 8
  • 34
  • 67
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
  • If you create a winform project and look at program.cs you will find STA above the main method. So objects in c# require that they are inside a main with STA. The issue is the libraries you are using and the type of project you are using are not consistent. You can add the STA like in program.cs to fix the error. – jdweng Sep 29 '22 at 15:15
  • @jdweng, thanks for your feedback. My project is a DLL. I don't think I can define Single Thread apartement" (STA) in my DLL. My main project is a WPF project which is a STA project (they are all by default). But I don't think add-migration will ever call my "exe". It just need to instantiate the dll where the DBContext is defined and all its dependencies (here, the other dll where all my domain objects are defined and all the postgresql dlls and microsoft EFCore dlls). – Eric Ouellet Sep 29 '22 at 15:20
  • STA is for a single thread application. I think it is similar to STATIC. The following starts thread in a static method : https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread?view=net-6.0&force_isolation=true – jdweng Sep 29 '22 at 15:58
  • The a in STA is for apartement (https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.apartmentstate?view=net-6.0#system-threading-thread-apartmentstate) It was intoduce for COM (ActiveX) by Microsoft around 1995. It is an abberation but it is mostly to ensure that call will occurs on the UI thread (the thread with the message loop). It is related to the Dispatcher in WPF or Winform. A COM dll had to enter an STA threading apartement for WPF and Winform app but I'm not sure for a not COM dll. – Eric Ouellet Sep 29 '22 at 17:25
  • EIther if a regular DLL would had to enter STA, it is very rare that we have to bother with that because it is hidden to us by the framework - by default. I'm pretty sure that is not related. Also, everything was working fine very recently and I did not add any DLL myself or modify my existing DLLs. – Eric Ouellet Sep 29 '22 at 17:25

0 Answers0