Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用已知名称注册通道,然后通过该已知名称检查并注销该通道,而不终止其他应用程序通道_C#_.net_Tcp_Remoting - Fatal编程技术网

C# 使用已知名称注册通道,然后通过该已知名称检查并注销该通道,而不终止其他应用程序通道

C# 使用已知名称注册通道,然后通过该已知名称检查并注销该通道,而不终止其他应用程序通道,c#,.net,tcp,remoting,C#,.net,Tcp,Remoting,我在应用程序消息传递方面遇到了问题,…在回答了一些问题后,我使用了System.Runtime命名空间下的远程处理命名空间 它工作得很好,但问题是当应用程序异常终止时。。。 如果服务器以非方式停止,则我注册的通道将保持注册状态 我没有太多的远程或其他相关的知识。。。 但我检查了一些东西,但没有起作用 我读过的文章是通过这种方式注册的,没有注销应用程序,我在服务中使用它,似乎服务应用程序主机不会关闭,只是服务停止了 这是我在服务应用程序中使用的客户端类 using System; using Sy

我在应用程序消息传递方面遇到了问题,…在回答了一些问题后,我使用了System.Runtime命名空间下的远程处理命名空间

它工作得很好,但问题是当应用程序异常终止时。。。 如果服务器以非方式停止,则我注册的通道将保持注册状态

我没有太多的远程或其他相关的知识。。。 但我检查了一些东西,但没有起作用

我读过的文章是通过这种方式注册的,没有注销应用程序,我在服务中使用它,似乎服务应用程序主机不会关闭,只是服务停止了

这是我在服务应用程序中使用的客户端类

using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Remoting
{
    public class Client
    {
        private RemotableObject remoteObject;

        /// <summary>
        /// Configure Client Class
        /// </summary>
        /// <param name="url">Something like: tcp://localhost:8080/HelloWorld</param>
        public Client(string url)
        {
            TcpChannel chan = new TcpChannel();
            ChannelServices.RegisterChannel(chan);

            remoteObject = (RemotableObject)Activator.GetObject(typeof(RemotableObject), url);
        }


        public void SetMessage(string message)
        {
            remoteObject.SetMessage(message);
        }
    }
}
使用系统;
使用System.Runtime.Remoting.Channels;
使用System.Runtime.Remoting.Channels.Tcp;
命名空间远程处理
{
公共类客户端
{
私有远程对象远程对象;
/// 
///配置客户端类
/// 
///比如:tcp://localhost:8080/HelloWorld
公共客户端(字符串url)
{
TCPCchannel chan=新的TCPCchannel();
ChannelServices.注册频道(chan);
remoteObject=(RemotableObject)Activator.GetObject(typeof(RemotableObject),url);
}
公共无效设置消息(字符串消息)
{
SetMessage(message);
}
}
}
正如我所注意到的,这篇文章没有给频道提供任何信息,所以我很想取消注册,我该如何找到它

在服务器应用程序中,文章终于完成了

using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Remoting
{
    public abstract class Server:IObserver
    {
        private RemotableObject remotableObject;

        /// <summary>
        /// Configure Server Class
        /// </summary>
        /// <param name="port">port number like: 8080</param>
        /// <param name="url">Object url like: HelloWorld</param>
        public Server(int port, string url)
        {
            remotableObject = new RemotableObject();

            TcpChannel channel = new TcpChannel(port);
            ChannelServices.RegisterChannel(channel);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotableObject), url, WellKnownObjectMode.Singleton);

            Cache.Attach(this);
        }

        public abstract void Notify(string message);
    }
}
使用System.Runtime.Remoting;
使用System.Runtime.Remoting.Channels;
使用System.Runtime.Remoting.Channels.Tcp;
命名空间远程处理
{
公共抽象类服务器:IObserver
{
私有远程对象远程对象;
/// 
///配置服务器类
/// 
///端口号:8080
///对象url,如:HelloWorld
公共服务器(int端口、字符串url)
{
remotableObject=新的remotableObject();
TCP通道=新的TCP通道(端口);
信道服务。注册信道(信道);
RegisterWellKnownServiceType(typeof(RemotableObject)、url、WellKnownObjectMode.Singleton);
缓存。附加(此);
}
公开摘要作废通知(字符串消息);
}
}
但是,即使我想通过运行相同端口号的应用程序的服务器启动我的应用程序,我也不想取消注册其他应用程序通道。。。我该怎么办

顺便说一句,大多数问题是关于注册频道的客户端,没有任何信息。。。我如何检测它?在服务尝试这样做之前注销它,然后异常终止

如果有帮助,这是我已经收到的错误:

事件类型:错误
事件源:事件读取器服务
事件类别:无
事件ID:0
日期:2012年8月20日
时间:下午5:23:14
用户:不适用
计算机:HF-SERVER-PC
说明:
服务无法启动。 System.Runtime.Remoting.RemotingException:通道“tcp”为 已经注册了。在 System.Runtime.Remoting.Channels.ChannelServices.RegisterChannelInternal(IChannel chnl,布尔值(安全性)at System.Runtime.Remoting.Channel.ChannelServices.RegisterChannel(IChannel chnl)at Remoting.Client..ctor(字符串url)at FileEventReaderService.Services.Logger.EventLogger..ctor(字符串 源、字符串日志名、字符串url)位于 FileEventReaderService.EventReader..ctor(字符串sqlServer,字符串 实例,字符串integratedSecurityType,字符串用户名,字符串 密码,字符串(数据库)位于 FileEventReaderService.EventReaderService.OnStart(字符串[]args)位于 System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象 (州)

有关更多信息,请参阅帮助和支持中心,网址为 go.microsoft.com/fwlink/


在我的例子中,问题是在同一个实例中有多个注册tcp通道,所以我创建了一个singleton类,只设置了一次client,其他时间我只调用了那个实例

using Remoting;

namespace FileEventReaderService.Services.Remotable
{
    class SingletonClient
    {
        private static SingletonClient _instance = new SingletonClient();
        private Client _client = null;

        public static SingletonClient GetSingletonClient()
        {
            return _instance;
        }

        public Client GetClient()
        {
            return _client;
        }

        public void SetClientConfiguration(string url)
        {
            _client=new Client(url);
        }
    }
}
顺便说一句,如果有人需要找到流程。。。他可以使用这篇文章: 下载演示源代码并使用它

我还以自己的方式编辑它,您可以使用它:(我没有更改2个主类“TcpTable”和“IpHelper”)


在我的例子中,问题是在同一个实例中有多个注册tcp通道,所以我创建了一个singleton类,只设置了一次client,其他时间我只调用了那个实例

using Remoting;

namespace FileEventReaderService.Services.Remotable
{
    class SingletonClient
    {
        private static SingletonClient _instance = new SingletonClient();
        private Client _client = null;

        public static SingletonClient GetSingletonClient()
        {
            return _instance;
        }

        public Client GetClient()
        {
            return _client;
        }

        public void SetClientConfiguration(string url)
        {
            _client=new Client(url);
        }
    }
}
顺便说一句,如果有人需要找到流程。。。他可以使用这篇文章: 下载演示源代码并使用它

我还以自己的方式编辑它,您可以使用它:(我没有更改2个主类“TcpTable”和“IpHelper”)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace FileEventReaderUI.Services.Network
{
    class ListenerProcessInformation
    {
        private string _fileName;
        private string[] _processModules;
        private IPEndPoint _localEndPoint;
        private IPEndPoint _remoteEndPoint;
        private int _processId;
        private TcpState _state;

        public ListenerProcessInformation(string fileName, string[] processModules, IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, int processId, TcpState state)
        {
            _fileName = fileName;
            _processModules = processModules;
            _localEndPoint = localEndPoint;
            _remoteEndPoint = remoteEndPoint;
            _processId = processId;
            _state = state;
        }

        public string FileName
        {
            get { return _fileName; }
        }

        public string[] ProcessModules
        {
            get { return _processModules; }
        }

        public IPEndPoint LocalEndPoint
        {
            get { return _localEndPoint; }
        }

        public IPEndPoint RemoteEndPoint
        {
            get { return _remoteEndPoint; }
        }

        public int ProcessId
        {
            get { return _processId; }
        }

        public TcpState State
        {
            get { return _state; }
        }
    }
}