我们有一个旧的第三方系统(我们称其为Junksoft®95),我们可以通过PowerShell与之交互(公开一个COM对象),并且我正在将其包装在REST API(ASP.NET Framework 4.8)中和WebAPI 2)。我使用System.Management.Automation
nuget包创建一个PowerShell
,在其中将Junksoft的COM API实例化为dynamic
对象,然后使用:]
不起作用。我说通常是因为,当ASP.NET恰好将传入调用提供给创建了//I'm omitting some exception handling and maintenance code for brevity powerShell = System.Management.Automation.PowerShell.Create(); powerShell.AddScript("Add-Type -Path C:\Path\To\Junksoft\Scripting.dll"); powerShell.AddScript("New-Object Com.Junksoft.Scripting.ScriptingObject"); dynamic junksoftAPI = powerShell.Invoke()[0]; //Now we issue commands to junksoftAPI like this: junksoftAPI.Login(user,pass); int age = junksoftAPI.GetAgeByCustomerId(custId); List<string> names = junksoftAPI.GetNames();
[当我在同一线程上运行所有这些命令时(例如,在控制台应用程序中),这很好。但是,由于某些原因,当我将
junksoftAPI
放入System.Web.Caching.Cache
并从Web应用程序中的其他控制器使用它时,此通常
junksoftAPI
的线程时,此方法实际上起作用。如果不是,那么Junksoft 95会给我一个错误。我有什么办法确保与junksoftAPI
的所有交互都发生在same
注
,我不想将整个Web应用程序变成一个单线程应用程序!控制器和其他地方的逻辑应该像正常情况那样在不同线程上发生。只能是Junksoft特定线程上发生的Junksoft交互,如下所示:[HttpGet]
public IHttpActionResult GetAge(...)
{
//finding customer ID in database...
...
int custAge = await Task.Run(() => {
//this should happen on the Junksoft-specific thread and not the next available thread
var cache = new System.Web.Caching.Cache();
var junksoftAPI = cache.Get(...); //This has previously been added to cache on the Junksoft-specific thread
return junksoftAPI.GetAgeByCustomerId(custId);
});
//prepare a response using custAge...
}
我们有一个旧的第三方系统(我们称其为Junksoft®95),我们可以通过PowerShell与之交互(公开一个COM对象),并且我正在将其包装在REST API(ASP.NET Framework 4.8)中...
如果您没有说这是第三方工具,我会假设它是GUI类。出于实际原因,让多个线程写入它们是一个非常糟糕的主意。 .NET从2.0 onward开始执行严格的“仅创建线程才可以写入”规则。
通常,WebServer尤其是ASP.Net使用很大的线程池。我们说的是每个内核10到100的线程数。这意味着很难将任何请求限定到特定线程。您最好不要尝试。