开发者

Can't access the assembly that installed into the GAC

I am sorry if this is stupid questions, but i've spend two days on this, and can't get it to work. Here's what I did:

  1. Create a new class library project
  2. Added a simple method sayHi() that returns a string
  3. In the project properties checked "sign the assembly" and used a strong name key file
  4. Compiled the application and copied the DLL to c:\windows\assemblies (and i also tried using the gacutil tool with similar results)
  5. Added the following to machine.config <compilation><assemblies><add assembly="gacTestClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31418215e131202e" />. This works just fine, because if i misspell the assembly name, any .net app will fail and complain about that line.
  6. Using the assembly cache viewer in the .NET framework configuration tool i can see the assembly in the cache.
  7. Now, when i try to use that, my app fails when i do either using gacTestClass or gacTestClass.gacTest.sayHi(). I get an error saying that the type or namespace could not be found.

Any help is appreciated.

EDIT:

Ok, i followed a suggestion and I will try to make this work with web.config, rather than machine.config.

I added the following to my web.config <compilation><开发者_如何学运维;assemblies><add assembly="gacTestClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31418215e131202e" /> and made sure that it works by misspelling the name, and watching the app fail. Now, when I try to add using gacTestClass to the top of my .cs file, i get an error saying that the type or namespace could not be found.

Just in case, here's the code the for the gacTestClass

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
[assmbly:AssemblyKeyFile("sayHi.snk")]
namespace gacTestClass {
    public class gacTest {
        public string sayHi() {
            return "Hello there!";
        }
    }
}


Try this:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Namespace" publicKeyToken="token" culture="neutral" />
    <bindingRedirect oldVersion="1.0.0.0" newVersion="1.1.0.0" />
  </dependentAssembly>
</assemblyBinding></runtime>

Move the compilation section from the machine.config to your web.config and give it a try. It should work fine in the machine.config but I assume that if you are GAC-ing an assembly, many applications will be using it. You will want each application to be able to choose which version of the DLL they are going to use (for easier upgrade paths).

Also, seems obvious, but make sure the method (gacTestClass.gacTest.sayHi()) you are trying to access is public.


EDIT: Try right-clicking on your project, clicking Add Reference, and adding your DLL file.

In general, you should never modify machine.config.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜