I am writing a C # class library component that will act as a TCP server. It will listen and receive XML through a specific port, deserialize it, and then raise events containing the resulting object as an argument to the event.
The class library itself will be consumed by the VB6 application, which will receive and process events and related COM objects of the visible class.
The TcpServer class terminates the functionality of TcpListener and is not COM visible. This handles the connections and raises events related to the connection, disconnection and received data.
sealed class TcpServer : IDisposable { private readonly TcpListener tcpListener; private bool disposed = false; public TcpServer(int port) { tcpListener = new TcpListener(IPAddress.Any, port); tcpListener.Start(); tcpListener.BeginAcceptSocket(EndAcceptSocket, tcpListener); } ~TcpServer() { Dispose(false); }
WUServer is the COM visibility class that is created and used by the VB6 application. He completes the TcpServer class and is responsible for deserializing any received data and raising the corresponding event with the corresponding event arguments.
public class WUServer : IWUServer { private TcpServer tcpServer; public WUServer() { tcpServer = new TcpServer(12345); tcpServer.DataReceived += new EventHandler<DataReceivedEventArgs>(tcpServer_DataReceived); tcpServer.SocketConnected += new EventHandler<IPEndPointEventArgs>(tcpServer_SocketConnected); tcpServer.SocketDisconnected += new EventHandler<IPEndPointEventArgs>(tcpServer_SocketDisconnected); } }
The problem I am facing is that the TcpServer application is not being disposed of properly by the VB6 application. Installing a WUServer instance in Nothing before closing the application will not remove the TcpServer class, it will continue to hang on the socket and cause errors if I try to run the VB6 application again.
If I use the WUServer class from a C # application, everything is fine, Dispose is called on TcpServer and the socket is closed.
How should I make sure that the TcpServer class is correctly deleted when it is created indirectly by the VB6 application?
c # vb6 dispose com-interop
Andrew
source share