Вопрос

I would like to embed depencency dll into my HttpHandler dll for easier deployment, so I was thinking to add it to solution as embedded resource and use AppDomain.CurrentDomain.AssemblyResolve event to resolve it.
Problem is, that if I subscribe to event in void ProcessRequest(HttpContext context) method, looks like it fails to resolve before it reaches event subscription.
So question would be where should I subscribe to that event, to be able to load dll from embedded resource.

Это было полезно?

Решение

You could use the static constructor of your handler. For example:

public class Handler1: IHttpHandler
{
    static Handler1()
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
        {
            if (string.Equals(e.Name, "ClassLibrary1", StringComparison.OrdinalIgnoreCase))
            {
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Handler.ClassLibrary1.dll"))
                {
                    var buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    return Assembly.Load(buffer);
                }
            }
            return null;
        };
    }

    public void ProcessRequest(HttpContext context)
    {
        var mtd = Type.GetType("ClassLibrary1.Class1, ClassLibrary1").GetMethod("Hello", BindingFlags.Static | BindingFlags.Public);
        var result = (string)mtd.Invoke(null, null);
        context.Response.ContentType = "text/plain";
        context.Response.Write(result);
    }

    public bool IsReusable
    {
        get { return true; }
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top