在服务中使用另一个项目

时间:2017-09-01 10:50:05

标签: c# exception service

我从昨天开始尝试使我的服务工作,但我没有找到解决方案,所以我不得不寻求你的帮助。 我简化了课程,只关注问题。

我的服务:

using test_import;

namespace Hespéride
{
public partial class Hespéride : ServiceBase
{
    private Timer timer = null;

    public Hespéride()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        Debugger.Launch();
        timer = new Timer();
        timer.Interval = 60000; // 60 seconds  
        timer.Elapsed += new ElapsedEventHandler(OnTimer);
        timer.Start();
        OnTimer(null, null);
    }

    public void OnTimer(object sender, ElapsedEventArgs args)
    {
        try
        {
            Voiture v = new Voiture(2);
        }
        catch(Exception e)
        {
            eventLog1.WriteEntry(e.Message);
        }
    }

    protected override void OnStop()
    {
        timer.Stop();
    }
}
}

我还有另一个现有项目test_import,其中包含我尝试使用的班级Voiture。 我将项目test_import添加到解决方案(右键单击解决方案>添加>现有项目> test_import.csproj),然后在服务项目Hespéride中添加了引用(右键单击引用>添加参考>项目> test_import

enter image description here enter image description here

但是当我尝试运行我的服务并进行调试时(我已经将visual studio的调试器附加到进程中),它会一直停止并向我发送此异常:

  

无法启动服务。 System.IO.FileNotFoundException:无法   加载文件或程序集'test_import,Version = 1.0.0.0,Culture = neutral,   PublicKeyToken = null'或其依赖项之一。系统不能   找到指定的文件。文件名:'test_import,Version = 1.0.0.0,   Culture = neutral,PublicKeyToken = null'at   Hespéride.Hespéride.OnTimer(对象发送者,ElapsedEventArgs args)at   Hespéride.Hespéride.OnStart(String [] args)in   C:\ Users \ jpougetoux \ documents \ visual studio   2015 \ Projects \Hespéride\Hespéride\Hespéride.cs:ligne 26 at   System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象   状态)

另一方面,当我将OnTimer中的代码放入我的服务program.cs的{​​{1}}主内容时,它可以正常运行,并且导入时没有错误。< / p>

有人向我建议我需要在服务安装文件夹中添加Hespéride,但我没有dll,因为dll不是库。 我可以将test_project编译为test_project,但之后是否可以稍后修改它,还是需要在每次修改后编译并重新导入它?

因此,如果某人有解决方案或教程解释如何做到这一点,我会非常感激。

0 个答案:

没有答案