简体   繁体   English

调用后.net远程关闭与服务器的连接

[英].net remoting close connection to server after call

I am very new to .NET remoting. 我对.NET远程处理非常陌生。 We have an MVC website making call to a Windows application using TCP connection (.NET remoting). 我们有一个MVC网站,使用TCP连接(.NET远程处理)调用Windows应用程序。 There is a timer running every 30 seconds which makes a call via TCP, but after it finishes, the connection still remains established. 有一个计时器每30秒运行一次,该计时器通过TCP进行呼叫,但完成后,连接仍保持建立状态。 As a result, after a few days, the server is throwing an exception because all ports are used up. 结果,几天后,服务器抛出异常,因为所有端口都用完了。 Then we have to restart the app pool to resolve the issue. 然后,我们必须重新启动应用程序池以解决该问题。 I am not sure how can we close the port after use, so that we can use it again. 我不确定使用后如何关闭端口,以便我们可以再次使用它。 We have a lot of users for the site. 该网站有很多用户。

Below is the client side code to register a channel 以下是注册频道的客户端代码

        bool Registered = false;

        foreach (IChannel ic in ChannelServices.RegisteredChannels)
        {
            if (ic.ChannelName == ChannelNameRemoting)
            {
                return ic;
            }
        }

        // Channel not found yet


        IDictionary channelConfig = new Hashtable();
        channelConfig["name"] = ChannelNameRemoting;
        channelConfig["secure"] = false;

        BinaryClientFormatterSinkProvider defaultClientSink = new BinaryClientFormatterSinkProvider();

        if (remotingSinkProvider == null)
        {
            remotingSinkProvider = new CustomClientChannelSinkProvider();

            remotingSinkProvider.EncodingDecodingProviderEvent
                += new CustomClientChannelSinkProvider.EncodingDecodingProviderDelegate(remotingSinkProvider_EncodingDecodingProviderEvent);
        }

        defaultClientSink.Next = remotingSinkProvider;

        IChannel channel = new TcpChannel(channelConfig, defaultClientSink, null);

        if (!Registered)
        {
            ChannelServices.RegisterChannel(channel, false);

        }

        return channel;

Below call will create a connection to connect to the server in timer. 下面的调用将创建一个连接,以便在计时器中连接到服务器。

var connection = (testConnect)Activator.GetObject(
                                      typeof(testConnect),
                                      "tcp://" + _remotingUrl + ":" + _remotingPort + "/test/test4"
                              );

connection.FunctionCall();

在此处输入图片说明

I guess you need to UnRegister you channel. 我想您需要取消注册频道。 Please check the link 请检查链接

So ultimately we create the instance of TcpChannel and register it to channel service, and then finally return it to parent method, so that can use and can make the call. 因此,最终我们创建了TcpChannel的实例并将其注册到通道服务,然后最终将其返回给父方法,以便可以使用并进行调用。 in the file/class where you have method that return the channel instance should also have another method to unregister the tcp channel, and once you are done with the call, you need to call this new methd to unregister the channel, and that will close the connection. 在具有返回通道实例的方法的文件/类中,还应该具有另一个方法来注销tcp通道,一旦完成调用,您需要调用此新方法来取消注册该通道,然后该方法将关闭连接。

public void UnregisterMyTcpChannel(TcpChannel yourChannelInstance)
{
   ChannelServices.UnregisterChannel(yourChannelInstance);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM