admin管理员组文章数量:1315785
I am creating an ASP.NET Web API gateway which needs to communicate to legacy COM component business logic written in C++. So I am creating another Web API which internally gets data from COM component through C# CLI layer.
Hence C++ COM component is not thread safe, I am considering to span multiple instances of WEB API per user request. The outer Web API Gateway can be instantiate the inner Web API per user and request to it.
Do any API gateways like Ocelot have any feature to achieve spanning multiple instance of same Web API? Or how to achieve spanning multiple instances and make request?
I am creating an ASP.NET Web API gateway which needs to communicate to legacy COM component business logic written in C++. So I am creating another Web API which internally gets data from COM component through C# CLI layer.
Hence C++ COM component is not thread safe, I am considering to span multiple instances of WEB API per user request. The outer Web API Gateway can be instantiate the inner Web API per user and request to it.
Do any API gateways like Ocelot have any feature to achieve spanning multiple instance of same Web API? Or how to achieve spanning multiple instances and make request?
Share Improve this question edited Jan 30 at 4:38 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 30 at 0:54 srajeshnklsrajeshnkl 9053 gold badges16 silver badges52 bronze badges 4 |1 Answer
Reset to default 1 +50Like already mentioned by the comment thread, you can load your COM model in a single-thread appartment state. If it's a legacy COM component, you have two major options to achieve it: either via registry or via your code.
A. If the COM component is correctly deployed and registered, you can change the appartment state via the registry with ThreadingModel 'Apartment' being a single-thread apartment.
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID
{CLSID}
InprocServer32
(Default) = path
ThreadingModel = Apartment
B. The other way is programmatically instantiate the COM component in a single-thread apartment, which would look somehow like this in your code:
using System.Runtime.InteropServices;
// create a new thread in single-thread apartment state
var staThread = new Thread(LoadCOMComponent);
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
static void LoadCOMComponent()
{
try
{
var comType = Type.GetTypeFromProgID("yourobject");
// activate COM object
var comObject = Activator.CreateInstance(comType);
// use COM object
[your code]
// release COM object
Marshal.ReleaseComObject(comObject);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
本文标签: cAPI Gateway with multiple instances of a same Web APIStack Overflow
版权声明:本文标题:c# - API Gateway with multiple instances of a same Web API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741990829a2409073.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ThreadingModel
value which will force all calls to any instance of this object for the process to be serialized to the initial (STA) thread that created the first COM object of this type – Simon Mourier Commented Jan 30 at 8:39