Web API/WCF如何保持对象活动
本文关键字:何保持 对象 活动 WCF API Web | 更新日期: 2023-09-27 18:21:11
我有一个引擎(ABBYY FlexiCapture engine),我想将它与REST服务(WebAPI或WCF)一起使用。发动机启动初始化需要30秒。每个请求都太长了。我希望发动机加载服务启动并保持在"预热"状态。如何使用WebAPI或WCF?(最好的方法是保持线程和引擎的活力)。附言:很抱歉我英语不好。
--来自ABBYY示例库
private void ProcessImages()
{
trace("Loading FlexiCapture Engine...");
IEngine engine = LoadEngine();
try
{
string samplesFolder = FceConfig.GetSamplesFolder();
trace("Creating and configuring the FlexiCapture Processor...");
IFlexiCaptureProcessor processor = engine.CreateFlexiCaptureProcessor();
processor.AddDocumentDefinitionFile(samplesFolder + "''SampleProject''Templates''Invoice_eng.fcdot");
// ....
trace("Adding images to process...");
processor.AddImageFile(samplesFolder + "''SampleImages''Invoices_1.tif");
// ....
trace("Recognizing the images and exporting the results...");
while (true)
{
// Recognize next document
IDocument document = processor.RecognizeNextDocument();
// processing recognized document...
}
}
finally
{
UnloadEngine(ref engine);
}
}
private IEngine LoadEngine()
{
// FlexiCapture Engine can be loaded in three diffrent ways:
// 1) Directly, as in this sample
// 2) As an inproc server (using COM infrastructure)
// 3) As an out-of-proc server in a worker process (using COM infrastructure)
IEngine engine;
int hResult = InitializeEngine(FceConfig.GetDeveloperSN(), null, null, out engine);
Marshal.ThrowExceptionForHR(hResult);
return engine;
}
我想要创建引擎。之后,我将创建并配置几个FlexiCapture处理器。(这需要很长时间,我想在服务启动时完成)然后,当用户添加图像时,我将使用其中一个处理器来识别它们。
启动时:创建引擎->创建多个处理器(+配置它们);在方法invoke上:使用一个免费的处理器->识别用户图像。
也许,您需要实现引擎的静态单例实例并使用上下文
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public static class EngineContainer
{
private static Engine _engine { get; set; }
public static Engine GetEngine
{
get { if (_engine == null) Init(); return _engine; }
}
}
如果您的引擎没有任何用户界面(UI),那么您可以使用带有WCF(自主机)的Windows服务。让Windows服务负责您的引擎(即实时对象),并通过WCF服务