public static void Main(string[] args) { //<snippet21> // Create the server channel. TcpServerChannel channel = new TcpServerChannel( "Server Channel", 9090, null); //</snippet21> // Register the server channel. ChannelServices.RegisterChannel(channel); // Expose an object for remote calls. RemotingConfiguration.RegisterWellKnownServiceType( typeof(RemoteObject), "RemoteObject.rem", WellKnownObjectMode.Singleton); //<snippet22> // Display the channel's URI. Console.WriteLine("The channel URI is {0}.", channel.GetChannelUri()); //</snippet22> //<snippet23> // Parse the channel's URI. string[] urls = channel.GetUrlsForUri("RemoteObject.rem"); if (urls.Length > 0) { string objectUrl = urls[0]; string objectUri; string channelUri = channel.Parse(objectUrl, out objectUri); Console.WriteLine("The object URI is {0}.", objectUri); Console.WriteLine("The channel URI is {0}.", channelUri); Console.WriteLine("The object URL is {0}.", objectUrl); } //</snippet23> // Wait for the user prompt. Console.WriteLine("Press ENTER to exit the server."); Console.ReadLine(); }
public static void Main() { // Create a remotable object. TcpServerChannel tcpChannel = new TcpServerChannel(8085); WellKnownServiceTypeEntry WKSTE = new WellKnownServiceTypeEntry(typeof(HelloService), "Service", WellKnownObjectMode.Singleton); RemotingConfiguration.RegisterWellKnownServiceType(WKSTE); RemotingConfiguration.ApplicationName = "HelloServer"; // Print out the urls for the HelloServer. string[] urls = tcpChannel.GetUrlsForUri("HelloServer"); foreach (string url in urls) { System.Console.WriteLine("{0}", url); } }
/// <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); }