c# - dll无法正常工作

时间:2017-03-27 08:10:26

标签: c# dll

我不知道要设置哪个标题,因为我不确切地知道问题是什么。 我有一个工作c#代码,我把它包装到dll中以将客户端与其他功能分开,但现在它并没有像预期的那样工作。它是udp以上的客户端 - 服务器。这是我的代码:

HandlerFactory factory = new HandlerFactory();
        factory.RegisterHandler(RequestType.Options, () => new OptionsHandler());
        factory.RegisterHandler(RequestType.Unauthorized, () => new UnauthorizedHandler());
        factory.RegisterHandler(RequestType.Ok, () => new OkHandler());
        factory.RegisterHandler(RequestType.Invite, () => new InviteHandler());
        factory.RegisterHandler(RequestType.Bye, () => new ByeHandler());
        factory.RegisterHandler(RequestType.Error, () => new ErrorHandler());

        Application.SetCompatibleTextRenderingDefault(false);


        Client client = new Client(factory);
        client.Start();
        client.Register();

在客户端:

    private void Listen()
    {
        while (!_stopping)
        {
            try
            {
                var asyncResult = MyUdpClient.BeginReceive(HandleIncomingUdpRequest, null);
                WaitHandle.WaitAny(new[] { _stopHandle, asyncResult.AsyncWaitHandle });
            }
            catch (Exception ex)
            {
                using (var sw = new StreamWriter("log.txt"))
                {
                    sw.WriteLine("Date:" + DateTime.Now + "\r\nError:" + ex.Message + "\r\n");
                }
            }
        }
        MyUdpClient.Close();
    }

private void HandleIncomingUdpRequest(IAsyncResult ar)
        {
            var received = MyUdpClient.EndReceive(ar, ref _serverAddress);
            var requestString = Encoding.ASCII.GetString(received);
            var type = Helper.GetRequestType(requestString);
            if (type != RequestType.Trying && type != RequestType.Unknown)
            {
                ProcessRequest(type, requestString);
            }
        }

所以,我将它包装成dll,现在就像这样使用它:

        var factory = new Factory.HandlerFactory();
        factory.RegisterHandler(RequestType.Options, ()=> new OptionsHandler());
        factory.RegisterHandler(RequestType.Unauthorized, () => new UnauthorizedHandler());
        factory.RegisterHandler(RequestType.Ok, () => new OkHandler());
        factory.RegisterHandler(RequestType.Invite, () => new InviteHandler());
        factory.RegisterHandler(RequestType.Bye, () => new ByeHandler());
        factory.RegisterHandler(RequestType.Error, () => new ErrorHandler());
        Client cl = new Client(factory);
        cl.Start();
        cl.Register();
        cl.Call("2");

问题是,在cl.Call("2")我得到"未处理的类型' System.ObjectDisposedException'发生在System.dll"在

                var received = MyUdpClient.EndReceive(ar, ref _serverAddress);

在Register()中,客户端应该发送注册请求,获得他未被授权的响应并调用UnauthorizedHandler,但这不会发生。

2 个答案:

答案 0 :(得分:1)

只是从指定的行和您的描述中,我会检查ref _serverAddress是否已正确初始化,因为它似乎是全局变量,最有可能在您的主应用程序中进行,但您已将用法移至dll

答案 1 :(得分:0)

问题是 - 我不知道它是怎么发生的 - 在这段代码中:

private void Listen()
    {
        while (!_stopping)
        {
            try
            {
                var asyncResult = MyUdpClient.BeginReceive(HandleIncomingUdpRequest, null);
                WaitHandle.WaitAny(new[] { _stopHandle, asyncResult.AsyncWaitHandle });
            }
            catch (Exception ex)
            {
                using (var sw = new StreamWriter("log.txt"))
                {
                    sw.WriteLine("Date:" + DateTime.Now + "\r\nError:" + ex.Message + "\r\n");
                }
            }
            MyUdpClient.Close();
        }
    }
在循环中调用了

MyUdpClient.Close()。我必须更改try-catch块并不小心将其放入循环中,当我问这个问题时,我从不同来源对其进行了复制。