简体   繁体   English

从 Windows 服务 (c#) 运行基于 COM 的 c# 控制台应用程序

[英]Run a COM based c# console application from Windows Service (c#)

I have a problem with running ac# console application from windows service application by c# code.我在通过 c# 代码从 windows 服务应用程序运行 ac# 控制台应用程序时遇到问题。

I use windows service as a background worker that runs a task (Quartz library) every time interval.我使用 Windows 服务作为后台工作者,每个时间间隔运行一个任务(Quartz 库)。

Here a code of job that runs other c# console app这是运行其他 c# 控制台应用程序的作业代码

ps = new ProcessStartInfo(batchRunnerPath);
ps.UseShellExecute = false;
ps.CreateNoWindow = true;
ps.RedirectStandardError = true;
ps.RedirectStandardOutput = true;
ps.Verb = "runas";
var process = Process.Start(ps);
string error = process.StandardError.ReadToEnd();
process.WaitForExit();

Console application is using COM objects and it renders PDF's and Images but it doenst matter.控制台应用程序正在使用 COM 对象,它呈现 PDF 和图像,但它并不重要。

When i run console app exe file manualy it works perfectly but when i try to run it from this worker i get an exception当我手动运行控制台应用程序 exe 文件时,它运行良好,但是当我尝试从该工作程序运行它时,出现异常

System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLSID {22FBECF5-10A3-11D2-9194-204C4F4F5020} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
   at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
   at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
   at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at JDA.Intactix.Automation.SpacePlanning..ctor(Boolean bIKSBRVMenuParam)
   at ReportRunner.Program.Main(String[] args) in E:\CFT\PgRunner\BatchRunner\Program.cs:line 98

From what i see this worker is starting this console app (because i log some info with log4net in console app) but on line 98 where i create new COM object it gives error.从我看到的这个工作人员正在启动这个控制台应用程序(因为我在控制台应用程序中使用 log4net 记录了一些信息)但是在我创建新 COM 对象的第 98 行它给出了错误。

heres how i run windows service enter image description here这是我如何运行 Windows 服务在此处输入图像描述

and heres a code that gives arror - line = new SpacePlaning(false) gives arror这是一个给出 arror 的代码 - line = new SpacePlaning(false) 给出 arror

      SpacePlanning sp = null;
        ProSpace.Project currentProject = null;
        ProSpace.PrintSetup printSetup = null;

        var plnLogic = Container.Resolve<Strategix.sCentralPlugin.SPPlugin.SPLogic.IPlanogramLogic>();

        try
        {
            sp = new SpacePlanning(false);

Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)) HRESULT 异常:0x80080005 (CO_E_SERVER_EXEC_FAILURE))

might come from a not well defined Security/Identity configuration of the COM component you want use.可能来自您想要使用的 COM 组件的未明确定义的安全/身份配置。 You need enough rights in order to instanciate a COM component from a Windows service (rights management).您需要足够的权限才能从 Windows 服务实例化 COM 组件(权限管理)。

  • Launch DCOMCNFG.exe, Component Services -> Computers -> My Computer -> DCOM Config -> [Your COM component]启动 DCOMCNFG.exe,组件服务 -> 计算机 -> 我的电脑 -> DCOM 配置 -> [你的 COM 组件]
  • Right click, properties右键,属性

Check "Security" tab and "Identity" tab configurations.检查“安全”选项卡和“身份”选项卡配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM