开发者

Public\Shared assembly without GAC

开发者 https://www.devze.com 2023-04-09 10:22 出处:网络
I have two assemblies and t开发者_如何学JAVAhis assembly was used by many applications. I dont have to have this in GAC.

I have two assemblies and t开发者_如何学JAVAhis assembly was used by many applications. I dont have to have this in GAC.

Is there a way to share the assembly publicly accessible by many applications without the use of GAC

My customer dont want to dump these dlls in GAC since these assemblies used by three applications So i dont want to hold in GAC


There are some ways to share assemblies among applications. Although the most recommended is through the GAC, you have also these other options:

Probing

You can specify a subdirectory under the application base directory to search for assemblies. I think this is not valid for you because this is not valid for different apps.

CodeBase

You can specify a Uri where the shared assemblies are located. In your case, those assemblies must be strong named so that they can be located in a shared common directory. Also take into account that the CodeBase tag only can be used in the Machine configuration or publisher policy files.

<?xml version="1.0" encoding="utf?8" ?>
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas?microsoft?com:asm.v1">
            <dependentAssembly>
                <codeBase version="1.0.0.0" 
                  href= "file:///c:\Shared Assemblies\MySharedAssembly.dll"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

AssemblyResolver

This is by far the least complicated mechanism to locate shared assemblies. Basically, the ApplicationDomain raises an event when it fails in the resolution of an assembly. This event can be caught by your application to manually return desired assembly from any location.

class Example
{
    public static void Main()
    {            
        AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;

        // Uses MySharedAssembly
        Foo();
        ...
    }

    private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
    {
        // I guess requested assemblyName is located in this file.
        var file = @"c:\Shared Assemblies\MySharedAssembly.dll";

        try
        {
            return Assembly.LoadFile(file);
        }
        catch (Exception)
        {
           ...
        }
        return null;        
    }
}
0

精彩评论

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

关注公众号