Exemplo n.º 1
0
        /// <summary>
        /// 根据指定的配置文档(ServerCfg.config)启动TCP服务,找不到该配置文件就找app.config
        /// </summary>
        /// <param name="msg"></param>
        public static RegisterServer <T> Init(T msg)
        {
            RegisterServer <T> reg = new RegisterServer <T>();

            try
            {
                if (File.Exists("ServerCfg.config"))
                {
                    RemotingConfiguration.Configure("ServerCfg.config", false);
                }
                else
                {
                    RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, false);
                }
                WellKnownServiceTypeEntry swte = new WellKnownServiceTypeEntry(typeof(T), typeof(T).Name, WellKnownObjectMode.Singleton);
                RemotingConfiguration.ApplicationName = typeof(T).Name;
                RemotingConfiguration.RegisterWellKnownServiceType(swte);
                reg.Register(msg);
                IChannel   ch  = ChannelServices.GetChannel(RemotingConfiguration.ApplicationName);
                TcpChannel tch = ch as TcpChannel;
                if (tch != null)
                {
                    if (!reg.Channels.Contains(tch))
                    {
                        reg.Channels.Add(tch);
                    }
                    string[] ss = tch.GetUrlsForUri(swte.ObjectUri);
                    foreach (string uri in ss)
                    {
                        if (!reg.Uris.Exists(a => a.AbsoluteUri == uri))
                        {
                            reg.Uris.Add(new Uri(uri, UriKind.RelativeOrAbsolute));
                        }
                    }
                }
                return(reg);
            }
            catch (Exception e)
            {
                Logs.Create("启动消息服务失败:" + e.Message);
            }
            return(reg);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据默认的配置文档(app.config)启动TCP服务
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="port"></param>
        public static RegisterServer <T> Intialize(T msg, string serviceName, int port = 8888)
        {
            RegisterServer <T> reg = new RegisterServer <T>();

            try
            {
                BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
                serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
                //BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();
                //IDictionary props = new Hashtable();
                //props["port"] = port;
                TcpServerChannel servChannel = new TcpServerChannel(serviceName, port, serverProv);
                ChannelServices.RegisterChannel(servChannel, false);
                if (!reg.Channels.Contains(servChannel))
                {
                    reg.Channels.Add(servChannel);
                }
                WellKnownServiceTypeEntry swte = new WellKnownServiceTypeEntry(typeof(T), serviceName, WellKnownObjectMode.Singleton);
                //RemotingConfiguration.ApplicationName = serviceName;
                RemotingConfiguration.RegisterWellKnownServiceType(swte);
                reg.Register(msg, serviceName);
                string[] ss = servChannel.GetUrlsForUri(serviceName);
                foreach (string uri in ss)
                {
                    if (!reg.Uris.Exists(a => a.AbsoluteUri == uri))
                    {
                        reg.Uris.Add(new Uri(uri, UriKind.RelativeOrAbsolute));
                    }
                }
                return(reg);
            }
            catch (Exception e)
            {
                Logs.Create("启动服务时出错:" + e.Message);
            }
            return(reg);
        }