I would like to run GET method in controller in STA mode. For Web API this worked perfectly: http://ryanhaugh.com/archive/2014/05/24/supporting-sta-threads-in-web-api/ Can it be translated to .NET Core?
Asked
Active
Viewed 1,779 times
0
-
Try it and see. Bare in mind that code isn't really playing `async` nice with its `.Result` in `() => base.InvokeActionAsync(context, cancellationToken).Result`. That will make at least some aspect of it synchonous – Oct 26 '18 at 23:13
-
Why do you even want this? I cannot see a better way to kill performance – Camilo Terevinto Oct 26 '18 at 23:33
-
1I have old application written in COM and I would like to allow users to use it via GET / POST requests. – M_T Oct 26 '18 at 23:36
-
1Create a Windows Service, communicate it with the ASP.NET Core app with WCF or SignalR and done. Don't kill the server unnecessarily – Camilo Terevinto Oct 26 '18 at 23:37
-
@MickyD Yeah, of course, those were just samples. You can use WCF with named pipes anyway – Camilo Terevinto Oct 27 '18 at 00:35
-
@CamiloTerevinto This is true :) – Oct 27 '18 at 01:15
2 Answers
3
I do it the same way now, except I create my thread differently.
Example example = new Example();
Thread thread = = new Thread(
delegate () //Use a delegate here instead of a new ThreadStart
{
example.StaRequired(); //Whatever you want to call with STA
}
)
{
IsBackground = false,
Priority = ThreadPriority.Normal
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start(); //Start the thread
thread.Join(); //Block the calling thread
The IsBackground
and Priority
part is optional of course.
Hope this helps someone

Luc
- 57
- 1
- 8
0
To use STA I created new thread:
ComClass c = new ComClass();
Thread t = new Thread(new ThreadStart(() =>
{
c.StaRequired();
}));
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();

M_T
- 71
- 1
- 10
-
Web apps generally should not be in the business of spinning up explicit threads that web servers aren't aware of. – Oct 27 '18 at 00:33
-
I moved the thread start to ComClass as a new method. Does it make any sense? – M_T Oct 28 '18 at 11:07