AppDomain +命名管道序列化性能

时间:2013-04-11 06:57:38

标签: c# .net

请考虑以下情况:在条形码扫描器下扫描订单,订单号将被发送到Windows服务应用程序,该应用程序将计算并发回价格。它将折扣券考虑在内,并且折扣在单独的组件中处理。 我想要做的是能够在运行时卸载程序集,以便可以更换DLL而无需停止服务。 (该服务需要30分钟才能启动) 所以我提出了创建一个新的AppDomain的想法,它将加载程序集并在那里执行代码。通过命名管道和序列化完成通信。 从功能上来说它运行良好,但生产性能却非常低。有没有人对如何尽可能快地运行下面的代码有任何建议?

代码说明: 对于每个带折扣的订单,都会调用DoAction。它首先启动一个充当命名管道客户端的线程。该线程收到发送回客户端的价格。 然后加载新的AppDomain(如果它还没有),并在该AppDomain的上下文中执行AppDomainCallback。在那里启动命名管道服务器,并在客户端连接时加载并调用包含折扣代码的程序集,并将结果反序列化回客户端线程并从DoAction返回。所以很多线程等待和序列化正在进行中,但我没有看到让它更快的方法。

[Serializable]
internal class ActionLoader
{
    private const string DOMAINNAME = "Actions";

    private const string PIPE_TO = "PipeTo";
    private const string PIPE_BACK = "PipeBack";

    private string assemblyName;
    private string className;
    private string methodName;
    private List<object> parameters;

    private static BinaryFormatter formatter = new BinaryFormatter();

    public ActionLoader(string assemblyName, string className, string methodName, List<object> parameters)
    {
        this.assemblyName = assemblyName;
        this.className = className;
        this.methodName = methodName;
        this.parameters = parameters;
    }

    private static AppDomain domain = AppDomain.CreateDomain(DOMAINNAME);

    public OrderPrice DoAction()
    {
        // after clientThread is done it fills RetVal
        ThreadedExecuter<OrderPrice> clientThread = new ThreadedExecuter<OrderPrice>(NamedPipeClient, parameters);
        clientThread.Start();

        if (domain == null) // domain can be unloaded by ropsrefresh so check if it should be created again
        {
            domain = AppDomain.CreateDomain(DOMAINNAME);
        }
        // AppDomainCallback runs in the context of appdomain so dll's get loaded in there and not in CurrentDomain
        domain.DoCallBack(AppDomainCallback);

        clientThread.Thread.Join();

        return clientThread.RetVal; // return price deseralized from AppDomain
    }

    public static void UnloadAppDomain() // called by ropsrefresh => refresh config
    {
        if (domain != null)
        {
            AppDomain.Unload(domain);
            domain = null;
        }
    }

    private void AppDomainCallback()
    {
        OrderPrice price = null;

        Assembly assembly = Assembly.LoadFrom(assemblyName);

        object action = assembly.CreateInstance(className);
        MethodInfo mi = action.GetType().GetMethod(methodName);

        // using pipes to communicate between AppDomains
        using (NamedPipeServerStream stream = new NamedPipeServerStream(PIPE_TO))
        {
            stream.WaitForConnection();

            List<object> parameters = (List<object>)DeserializeFromStream(stream);

            Type t = action.GetType();

            if (mi != null)
                price = (OrderPrice)mi.Invoke(action, parameters.ToArray());
        }

        // server becomes client to serialize data back
        using (NamedPipeClientStream stream = new NamedPipeClientStream(PIPE_BACK))
        {
            stream.Connect();
            SerializeToStream(stream, price);
        }
    }

    private static OrderPrice NamedPipeClient(object parameters)
    {
        OrderPrice price = null;

        // using pipes to communicate between AppDomains
        using (NamedPipeClientStream stream = new NamedPipeClientStream(PIPE_TO))
        {
            stream.Connect();
            SerializeToStream(stream, parameters); // serialize function parameters to pipe stream
        }

        using (NamedPipeServerStream stream = new NamedPipeServerStream(PIPE_BACK))
        {
            stream.WaitForConnection();

            price = (OrderPrice)DeserializeFromStream(stream);
        }

        return price; // returns deserialized price to ThreadedExecutor
    }

    private static object DeserializeFromStream(Stream stream)
    {
        return formatter.Deserialize(stream);
    }

    private static void SerializeToStream(Stream stream, object parameters)
    {            
        formatter.Serialize(stream, parameters);
    }
}

1 个答案:

答案 0 :(得分:1)

重写代码以使用Task库。这样做效果更好,因为它使用了Windows ThreadPool。 task.Result将阻塞,直到任务完成,因此不再需要加入。

public OrderPrice DoAction()
{
    Task<OrderPrice> task = Task<OrderPrice>.Factory.StartNew(NamedPipeClient);

    if (domain == null)
        domain = AppDomain.CreateDomain(DOMAINNAME);
    domain.DoCallBack(AppDomainCallback);

    return task.Result; 
}