关于Remoting server
使用反射调用指定的服务器端程序集
Server端:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Runtime.Remoting.Channels.Tcp;
5
using System.Runtime.Remoting.Channels;
6
using System.Runtime.Remoting;
7 
8
namespace Server
9
{
10
class Program
11
{
12
private static TcpServerChannel channel = null;
13
private static int Port = 8080;
14
15
static void Main( string[] args)
16
{
17
channel = new TcpServerChannel( "NcZerg", Port);
18
ChannelServices.RegisterChannel(channel, false);
19
System.Type ServerObj = typeof(ServerObjects.ServerObjFactory);
20
/// System.Type ServerObj = typeof(ServerObjects.ServerObject);
21
System.Runtime.Remoting.WellKnownServiceTypeEntry Services = new WellKnownServiceTypeEntry(ServerObj, "Services", WellKnownObjectMode.SingleCall);
22
RemotingConfiguration.RegisterWellKnownServiceType(Services);
23
Console.WriteLine( "服务已经启动");
24
Console.ReadLine();
25
}
26
}
27
}
7
namespace ServerObjects
8
{
9
[Serializable]
10
public class ServerObject : MarshalByRefObject, IServerObject
11
{
12
13
public int GetSum( int a, int b)
14
{
15
return a + b;
16
}
17
18
public void SayHello(String name)
19
{
20
;
21
}
22
}
23
24
/// <summary>
25
/// 工厂方法,构件对象
26
/// </summary>
27
[Serializable]
28
public class ServerObjFactory : MarshalByRefObject, IServerObjFactory
29
{
30
public IServerObject CreateInstance()
31
{
32
return new ServerObject();
33
}
34
35
/// <summary>
36
/// 利用反射和工厂方法类构造自己所需的实例类
37
/// </summary>
38
/// <param name="ClassName"></param>
39
/// <returns></returns>
40
public object GetInstance(String ClassName)
41
{
42
Assembly assembly = Assembly.GetExecutingAssembly();
43
Type typForm = assembly.GetType(ClassName);
44
Object obj = typForm.InvokeMember(
45
null,
46
BindingFlags.DeclaredOnly |
47
BindingFlags.Public | BindingFlags.NonPublic |
48
BindingFlags.Instance | BindingFlags.CreateInstance,
49
null,
50
null,
51
null);
52
53
return obj;
54
}
55
}
56
}
57
Server端:









10


12

13

14

15


17


19

20

21



24


26

27

应用程序类:
IServerObj工程 1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4 
5
namespace IServerObj
6
{
7
public interface IServerObject
8
{
9
int GetSum( int a, int b);
10
void SayHello(String name);
11
}
12
13
public interface IServerObjFactory
14
{
15
IServerObject CreateInstance();
16
object GetInstance(String ClassName);
17
}
18
}
19
IServerObj工程











12

13


15



18

19

Client端:
using System;

using System.Collections.Generic;

using System.Text;

using System.Configuration;

using IServerObj;


namespace Client

{
class RemotingServices
{
private static IServerObjFactory _ServerFactory = null;

private static IServerObjFactory ServerFactory
{
get
{
if (_ServerFactory == null)
{
_ServerFactory = (IServerObjFactory)Activator.GetObject(
typeof(IServerObjFactory),
"tcp://127.0.0.1:8080/Services");
}

return _ServerFactory;
}
}

private static IServerObject _ServerObject = null;
public static IServerObject ServerObject
{
get
{
if (_ServerObject == null)
{
/// 利用反射和工厂方法类构造自己所需的实例类
_ServerObject = (IServerObject)ServerFactory.GetInstance( "ServerObjects.ServerObject");
}
return _ServerObject;
}
}
}
}
















































12

13


15


17

18


20


22

23

24

25


27



30


32


34

35

36


38

39

40


42




46

47

48


50

51

52

53


55

56

57
