重温.NET Remoting(二)

这节来谈谈关于Remoting的简单配置

服务端配置

<?xml version="1.0"?>
<configuration>
  
<system.runtime.remoting>
    
<application>
      
<channels>
        
<channel ref="tcp" port="9999"/>
      
</channels>
      
<service>
        
<wellknown mode="Singleton" type="Server.MyServiceImpl,Server" objectUri="myservice.rem"/>
      
</service>
    </application>
  
</system.runtime.remoting>
</configuration>

服务端调用

RemotingConfiguration.Configure("Server.exe.config",false);
            Console.WriteLine(
"Server is Running...");

当服务开启后,可以通过控制台查看端口是否开始监听,运行-》cmd-》netstat -a ,通过查看命令显示的信息,判断你所设置的服务是否开启

客户端配置

<?xml version="1.0"?>
<configuration>
  
<system.runtime.remoting>
    
<application>
      
<channels>
        
<channel ref="tcp"/>
      
</channels>
      
<client>
        
<wellknown type="ShareDLL.IMyService,ShareDLL" url="tcp://localhost:9999/myservice.rem"/>
      
</client>
    
</application>
  
</system.runtime.remoting>
</configuration>

客户端调用

 RemotingConfiguration.Configure("Client.exe.config"false);
            WellKnownClientTypeEntry[] types 
= RemotingConfiguration.GetRegisteredWellKnownClientTypes();
            
if (types.Length > 0)
            {
                ShareDLL.IMyService service 
= (ShareDLL.IMyService)Activator.GetObject(types[0].ObjectType,
                    types[
0].ObjectUrl);
                
try
                {
                    Console.WriteLine(service.SayHello(
"mike"));
                }
                
catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            
else
                Console.WriteLine(
"no service reigstered");

更多关于Remoting的配置,可以参考其他

作者:Jackhuclan
出处:http://jackhuclan.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/jackhuclan/p/2137756.html