原文:http://www.cnblogs.com/AndyHai/archive/2007/07/12/815536.html
由于项目原因,需要在ASP.NET网页上显示电力用户的电费情况,电力公司那边取用户电费的接口是Tuxedo的,这在应用程序下是很容易调用的,可是把它搬到ASP.NET中,情况就不同了,在IIS环境下,调用Tuxedo始终不成功,我想,可能是安全性上的问题,也有可能是Tuxedo本身不能在IIS宿主中执行的缘故,所以,只好另外开辟一条路。
因为可以在应用程序中执行Tuxedo,所以,我想用Remoting在IIS与Tuxedo服务之间架一个代理,让ASP.NET去调用Remoting,再由Remoting去调用Tuxedo,这样一定是可行的。
首先,写一个公共类库,里面有代理执行的接口,以及一个代理执行类厂类:
1namespace TuxedoObject
2{
3 public interface ITuxedoObject
4 {
5 bool ExecuteTuxedo(string[] EnvList, string Service, string Param, out string Output);
6 }
7
8 [Serializable]
9 public class TuxedoFac : MarshalByRefObject
10 {
11 public static Type TuxedoType;
12
13 public ITuxedoObject GetTuxedoObject()
14 {
15 return (ITuxedoObject)TuxedoType.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, null, null);
16 }
17 }
18}
19
2{
3 public interface ITuxedoObject
4 {
5 bool ExecuteTuxedo(string[] EnvList, string Service, string Param, out string Output);
6 }
7
8 [Serializable]
9 public class TuxedoFac : MarshalByRefObject
10 {
11 public static Type TuxedoType;
12
13 public ITuxedoObject GetTuxedoObject()
14 {
15 return (ITuxedoObject)TuxedoType.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, null, null);
16 }
17 }
18}
19
PS:之所以把代理执行写成接口,是因为我不喜欢把具体实现让Remoting客户端看见
接着写一个Remoting服务器,我选择用Windows服务的形式提供,服务器里包含代理执行的接口实现以及服务类:
1namespace TuxedoRemoting
2{
3 [Serializable]
4 public class Tuxedo : MarshalByRefObject, TuxedoObject.ITuxedoObject
5 {
6 public Tuxedo()
7 {
8 }
9
10 public bool ExecuteTuxedo(string[] EnvList, string Server, string Param, out string Output)
11 {
12 bool ret = false;
13
14 try
15 {
16 foreach(string env in EnvList)
17 Bea.Tuxedo.ATMI.Utils.tuxputenv(env);
18 AppContext ac = AppContext.tpinit(null);
19
20 TypedBuffer its = new TypedString(Param);
21 TypedBuffer rts = new TypedString("");
22
23 ac.tpcall(Server, its, ref rts, 0);
24
25 Output = (rts as TypedString).GetString();
26
27 ret = true;
28 try
29 {
30 ac.tpterm();
31 }
32 catch
33 {
34 }
35 }
36 catch (Exception Err)
37 {
38 Output = "Tuxedo执行异常," + Err.Message;
39 }
40 return ret;
41 }
42 }
43
44 public partial class TuxedoService : ServiceBase
45 {
46 public TuxedoService()
47 {
48 InitializeComponent();
49 }
50
51 protected override void OnStart(string[] args)
52 {
53 TuxedoObject.TuxedoFac.TuxedoType = typeof(Tuxedo);
54 TcpChannel channel = new TcpChannel(18000);
55 ChannelServices.RegisterChannel(channel, false);
56
57 RemotingConfiguration.ApplicationName = "TuxedoRemoting";
58 RemotingConfiguration.RegisterActivatedServiceType(typeof(TuxedoObject.TuxedoFac));
59 }
60
61 protected override void OnStop()
62 {
63 IChannel[] channels = ChannelServices.RegisteredChannels;
64 foreach (IChannel channel in channels)
65 {
66 if (channel is TcpChannel)
67 {
68 TcpChannel ch = channel as TcpChannel;
69 ch.StopListening(null);
70 }
71
72 ChannelServices.UnregisterChannel(channel);
73 }
74 }
75 }
76
77}
78
2{
3 [Serializable]
4 public class Tuxedo : MarshalByRefObject, TuxedoObject.ITuxedoObject
5 {
6 public Tuxedo()
7 {
8 }
9
10 public bool ExecuteTuxedo(string[] EnvList, string Server, string Param, out string Output)
11 {
12 bool ret = false;
13
14 try
15 {
16 foreach(string env in EnvList)
17 Bea.Tuxedo.ATMI.Utils.tuxputenv(env);
18 AppContext ac = AppContext.tpinit(null);
19
20 TypedBuffer its = new TypedString(Param);
21 TypedBuffer rts = new TypedString("");
22
23 ac.tpcall(Server, its, ref rts, 0);
24
25 Output = (rts as TypedString).GetString();
26
27 ret = true;
28 try
29 {
30 ac.tpterm();
31 }
32 catch
33 {
34 }
35 }
36 catch (Exception Err)
37 {
38 Output = "Tuxedo执行异常," + Err.Message;
39 }
40 return ret;
41 }
42 }
43
44 public partial class TuxedoService : ServiceBase
45 {
46 public TuxedoService()
47 {
48 InitializeComponent();
49 }
50
51 protected override void OnStart(string[] args)
52 {
53 TuxedoObject.TuxedoFac.TuxedoType = typeof(Tuxedo);
54 TcpChannel channel = new TcpChannel(18000);
55 ChannelServices.RegisterChannel(channel, false);
56
57 RemotingConfiguration.ApplicationName = "TuxedoRemoting";
58 RemotingConfiguration.RegisterActivatedServiceType(typeof(TuxedoObject.TuxedoFac));
59 }
60
61 protected override void OnStop()
62 {
63 IChannel[] channels = ChannelServices.RegisteredChannels;
64 foreach (IChannel channel in channels)
65 {
66 if (channel is TcpChannel)
67 {
68 TcpChannel ch = channel as TcpChannel;
69 ch.StopListening(null);
70 }
71
72 ChannelServices.UnregisterChannel(channel);
73 }
74 }
75 }
76
77}
78
好了,下面该写ASP.NET了,我只给出部分调用方法,很容易:
1TcpChannel channel = new TcpChannel();
2ChannelServices.RegisterChannel(channel, false);
3object[] attrs = { new UrlAttribute("tcp://xxx.xxx.xxx.xxx:18000/TuxedoRemoting") };
4TuxedoFac tf = (TuxedoFac)Activator.CreateInstance(typeof(TuxedoFac), null, attrs);
5ITuxedoObject Tuxedo = tf.GetTuxedoObject();
6Tuxedo.ExecuteTuxedo(new string[] { TuxedoEnv }, Service, Param, out Output);
2ChannelServices.RegisterChannel(channel, false);
3object[] attrs = { new UrlAttribute("tcp://xxx.xxx.xxx.xxx:18000/TuxedoRemoting") };
4TuxedoFac tf = (TuxedoFac)Activator.CreateInstance(typeof(TuxedoFac), null, attrs);
5ITuxedoObject Tuxedo = tf.GetTuxedoObject();
6Tuxedo.ExecuteTuxedo(new string[] { TuxedoEnv }, Service, Param, out Output);