c#remoting的配置文件方式

ConsoleServer:

配置文件:

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknow mode="SingleCall" type="ConsoleServer.HelloServer,ConsoleServer" objectUri="HelloServer" />
      </service>
      <channels>
        <channel ref="tcp" port="8888"/>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

代碼:

ConsoleServer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace ConsoleServer
{
    class Program
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("ConsoleServer.exe.config",false);
            ////創建TCP通道
            //TcpChannel chan = new TcpChannel(8888); //必須指定端口號
            ////註冊通道
            //ChannelServices.RegisterChannel(chan, false);  //借用IIS的安全機制
            ////加載遠程對象
            //RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer), "HelloServer", WellKnownObjectMode.Singleton);

            Console.Read();
        }
    }
}

 

HelloServer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleServer
{
    /// <summary>
    /// 業務代碼
    /// </summary>
    public class HelloServer:MarshalByRefObject
    {
        public string HelloWorld(string str)
        {
            return "張三說: Hello" + str;
        }
    }
}

 

ConsoleClient:

配置文件:

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.runtime.remoting>
    <application>
      <client>
        <wellknow type="ConsoleServer.HelloServer,ConsoleServer" url="tcp://localhost:8888/HelloServer" />
      </client>
    </application>
  </system.runtime.remoting>
</configuration>

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using ConsoleServer;

namespace ConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("ConsoleClient.exe.config",false);
            HelloServer h = new HelloServer();
            ////建立通信通道
            //TcpChannel chan = new TcpChannel();
            ////註冊通道
            //ChannelServices.RegisterChannel(chan, false);
            //HelloServer h = (HelloServer)Activator.GetObject(typeof(ConsoleServer.HelloServer),"tcp://localhost:8888/HelloServer");
            string str = h.HelloWorld("aaa");
            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章