没有 GAC 的公共\共享程序集

发布于 2024-12-07 01:33:29 字数 152 浏览 5 评论 0原文

我有两个程序集,这个程序集被许多应用程序使用。我不必在 GAC 中拥有这个。

有没有办法在不使用 GAC 的情况下共享许多应用程序可公开访问的程序集

我的客户不想将这些 dll 转储到 GAC 中,因为这些程序集被三个应用程序使用所以我不想保留在 GAC 中

I have two assemblies and this 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-12-14 01:33:29

有一些方法可以在应用程序之间共享程序集。尽管最推荐的是通过 GAC,但您还可以使用以下其他选项:

探测

您可以在应用程序基目录下指定一个子目录来搜索程序集。我认为这对您无效,因为这对不同的应用程序无效。

CodeBase

您可以指定共享程序集所在的 Uri。在您的情况下,这些程序集必须具有强命名,以便它们可以位于共享的公共目录中。
另请注意,CodeBase 标记只能在计算机配置或发布者策略文件中使用。

<?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

这是迄今为止定位共享程序集最简单的机制。基本上,当应用程序域解析程序集失败时,它会引发一个事件。您的应用程序可以捕获此事件,以便从任何位置手动返回所需的程序集。

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

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;        
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文