【发布时间】:2011-05-14 16:08:19
【问题描述】:
我完全改写(更准确地说)我正在经历失去对 C# Windows 服务中引用的类库的引用。
过程: 创建了一个全新的 C# .Net v4.0 Windows 服务。 在该解决方案中,我创建了一个新的类库,该类库将从服务的 OnStart() 方法中调用,并在 Windows 服务中对类库进行引用。 我导入了 RssToolkit 项目(找到 here)。 RssToolkit 项目框架是 2.0(不过没关系),但仅供参考。引用类库中的 RssToolkit。
所以,我们有 Windows 服务 --> 类库 --> RssToolkit。
Windows 服务:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using ClassLibraryToExecuteRss;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Class1.DoSomeWork();
}
protected override void OnStop()
{
}
}
}
类库:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RssToolkit.Rss;
namespace ClassLibraryToExecuteRss
{
public static class Class1
{
public static void DoSomeWork()
{
//RssDocument rssDocument = new RssDocument();
//rssDocument = RssDocument.Load(new System.Uri("http://www.somerssurl.com"));
}
}
}
如您所见,使用 RssDocument 类的代码行已被注释掉。将这些注释掉后,我就可以很好地编译解决方案了……但它的用处不大。
取消注释后,我会在服务代码中收到以下编译错误:
错误 3 类型或命名空间名称 'ClassLibraryToExecuteRss' 不能 被发现(您是否缺少使用 指令或程序集 参考?)C:\Projects\TestBed\TestingServiceWithXLibs\WindowsService1\WindowsService1\Service1.cs 9 7 WindowsService1
...和...
错误 4 名称 'Class1' 没有 存在于当前 上下文 C:\Projects\TestBed\TestingServiceWithXLibs\WindowsService1\WindowsService1\Service1.cs 22 13 WindowsService1
那么这里发生了什么?我构建了一个工作正常的 TDD 解决方案,但是当将调用该类库的代码从我的单元测试带入解决方案时,我得到了这个。
我没有更改任何名称空间,并将所有内容保留为默认值。
顺便说一句,我确实将 RssToolkit 的目标框架更改为 4.0...没有任何变化,并且我遇到了与在我的类库中使用另一个外部库 (SubSonic) 相同的问题。
有人能解释一下吗?
【问题讨论】:
-
您在这些项目中是否有一个名为 workproject 的类?
-
不,没有名为 WorkProject 的类——即命名空间中的项目名称。我尝试从解决方案中添加其他项目......同样的问题。但是,我能够添加 WCF 服务,对其进行代码引用并毫无问题地编译(虽然不是我真正想要的)
-
试试不同的默认命名空间(试试看)
-
您发布的错误只是由于错误导致类库不再编译导致的后续错误(因此服务不再找到引用)。请检查类库在编译时抛出的错误。
-
单独编译类库不会产生任何错误。
标签: c# .net windows-services namespaces