在C#中关闭/处置ServiceHost线程的正确方法?

Mil*_*son 3 c# wcf servicehost

我正在构建一个非常简单的服务器,它必须在.NET中使用命名管道,并将在Windows窗体GUI之后运行.我已经能够在'Server'类(下面)中实现ServiceHost,并使用'Client'类与它进行通信.我遇到的问题是找出关闭在线程上运行的ServiceHost的正确方法,以及在窗体关闭时处理线程.我是线程和命名管道的新手,所以要好!


这是启动我的服务器/客户端的表单:

public partial class MyForm : Form
{
    Thread server;
    Client client;

    public MyForm()
    {
        InitializeComponent();

        server = new Thread(() => new Server());
        server.Start();

        client = new Client();
        client.Connect();
    }
}

private void MyForm_FormClosed(object sender, FormClosedEventArgs e)
{
    // Close server thread and disconnect client.
    client.Disconnect();

    // Properly close server connection and dispose of thread(?)
}
Run Code Online (Sandbox Code Playgroud)



这是Server类:

class Server : IDisposable
{
    public ServiceHost host;
    private bool disposed = false;

    public Server()
    {
        host = new ServiceHost(
            typeof(Services),
                new Uri[]{
                new Uri("net.pipe://localhost")
            });

        host.AddServiceEndpoint(typeof(IServices), new NetNamedPipeBinding(), "GetData");
        host.AddServiceEndpoint(typeof(IServices), new NetNamedPipeBinding(), "SubmitData");
        host.Open();

        Console.WriteLine("Server is available.");
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    { 
        if(!this.disposed)
        {
            if(disposing)
            {
                host.Close();
            }

            disposed = true;
        }
    }

    ~Server()
    {
        Dispose(false);
    }
}
Run Code Online (Sandbox Code Playgroud)




使用IDisposable是一个很好的方法,当我完成我的线程时如何调用Dispose()?

Ern*_*ieL 8

你正在变得比它需要的更难.

  1. 不要创建一个线程来启动ServiceHost.ServiceHost将在内部管理自己的线程以进行服务调用.创建一个新线程只是为了启动它没有任何好处ServiceHost.Connect()ServiceHost初始化并运行之前,您的客户端调用也不会成功.所以需要server.Start()和之间的同步client.Connect().新线程没有保存任何东西.

  2. 除非你有充分的理由,否则不要在C#中使用Finalizer.~Server()是个坏主意.

所以Server彻底摆脱这个阶级.包装器不会为您购买任何东西(除非您正在进行未在发布的代码中显示的配置管理).

创建服务主机MyForm()和呼叫host.Close()MyForm_FormClosed().完成.