tags:

views:

157

answers:

2

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!";
        }
    }
}
+3  A: 

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

In general, you should never modify machine.config.

SLaks
Why not? I have multiple apps, and I don't want to have to add a reference every time a new app is installed on the server.
Pasha
Because if, next year, you get a new server, or if you move to .Net 4, you'll probably forgot to re-modify `machine.config`.
SLaks
Also, so that multiple applications from different people can live on the same server. **Don't use global state to solve a local problem.** http://blogs.msdn.com/oldnewthing/archive/2008/12/11/9193695.aspx http://blogs.msdn.com/oldnewthing/archive/2009/10/15/9907370.aspx
SLaks
Thank you, I didn't realized that machine.config modifications are a bad thing, but it makes perfect sense. I tried adding this to web.config, but it still doesn't work.
Pasha
+2  A: 

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.

sestocker
I tried moving the code to web.config, but it didn't work. Yes, the assembly is GACed, and the class and the method are both public.
Pasha
Where does that go?
Pasha
Take that back - I think it is sharepoint only...
sestocker
That's what i thought. I am still stuck though. It seems like such a simple problem
Pasha
Nope, adding the <runtime>... didn't help either. I don't understand, why is this so hard? I've read a dozen articles on how to do this, and every single one says the same thing.
Pasha