开发者

C# Windows loses reference to class library when those classes use certain objects?

I'm completely rephrasing (more accurately) I am experiencing with losing reference to a referenced class library from a C# Windows service.

Process: Created a brand new C# .Net v4.0 Windows service. In that solution I created a new class library that will be called from the service's OnStart() method, and reference is made in the Windows service to the cla开发者_如何学Goss library. I import the RssToolkit project (found here). The RssToolkit project framework is 2.0 (doesn't matter though), but FYI. Reference is made to the RssToolkit from the class library.

So, we have Windows service --> class library --> RssToolkit.

Windows Service:

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()
        {
        }
    }
}

Class Library:

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"));
        }

    }
}

As you can see, lines of code using the RssDocument class are commented out. With these commented out I can compile the solution just fine... but it's of little use.

Once uncommented, I receive the following compile errors in the service code:

Error 3 The type or namespace name 'ClassLibraryToExecuteRss' could not be found (are you missing a using directive or an assembly reference?) C:\Projects\TestBed\TestingServiceWithXLibs\WindowsService1\WindowsService1\Service1.cs 9 7 WindowsService1

...and...

Error 4 The name 'Class1' does not exist in the current context C:\Projects\TestBed\TestingServiceWithXLibs\WindowsService1\WindowsService1\Service1.cs 22 13 WindowsService1

So what's going on here? I built a TDD solution that works fine, but when taking the code that calls that class library from my unit tests into the solution, I get this.

I haven't changed any of the name spaces, and left everything as default.

Incidentally, I did change the target framework of the RssToolkit to 4.0... no change, and I experienced the same issue as I'm using another external library (SubSonic) in my class library.

Can anyone please shed some light on this?


  1. Your server project needs to reference both your class library project and the RssToolkit assembly.
  2. Make sure your service project is targeting the proper framework. Right-click on the project and choose Properties. On the Application tab, make sure you are targeting the proper framework. For instance, perhaps you are targeting the Client Profile when you actually need the whole framework.


After further digging, this MSDN article accurately targets this issue. However the errors presented are really unclear, and a suggestion has been made at Microsoft Connect.

FROM MSDN:

The .NET Framework 4 Client Profile is a subset of the .NET Framework 4 that is optimized for client applications. It provides functionality for most client applications, including Windows Presentation Foundation (WPF), Windows Forms, Windows Communication Foundation (WCF), and ClickOnce features. This enables faster deployment and a smaller install package for applications that target the .NET Framework 4 Client Profile.

**

Note: If you are targeting the .NET Framework 4 Client Profile, you cannot reference an assembly that is not in the .NET Framework 4 Client Profile. Instead you must target the .NET Framework 4. For more information, see Troubleshooting .NET Framework Targeting Errors.

**

I think that the Note, however does not go far enough. I was referencing an external assembly just fine. The issue was that that assembly itself was referencing non-4.0 assemblies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜