用ASP.NET调用Tuxedo

  由于项目原因,需要在ASP.NET网页上显示电力用户的电费情况,电力公司那边取用户电费的接口是Tuxedo的,这在应用程序下是很容易调用的,可是把它搬到ASP.NET中,情况就不同了,在IIS环境下,调用Tuxedo始终不成功,我想,可能是安全性上的问题,也有可能是Tuxedo本身不能在IIS宿主中执行的缘故,所以,只好另外开辟一条路。
  因为可以在应用程序中执行Tuxedo,所以,我想用Remoting在IIS与Tuxedo服务之间架一个代理,让ASP.NET去调用Remoting,再由Remoting去调用Tuxedo,这样一定是可行的。
  首先,写一个公共类库,里面有代理执行的接口,以及一个代理执行类厂类:

 1 None.gif namespace  TuxedoObject
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif    public interface ITuxedoObject
 4ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 5InBlock.gif        bool ExecuteTuxedo(string[] EnvList, string Service, string Param, out string Output);
 6ExpandedSubBlockEnd.gif    }

 7InBlock.gif
 8InBlock.gif    [Serializable]
 9InBlock.gif    public class TuxedoFac : MarshalByRefObject
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        public static Type TuxedoType;
12InBlock.gif
13InBlock.gif        public ITuxedoObject GetTuxedoObject()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            return (ITuxedoObject)TuxedoType.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, nullnullnull);
16ExpandedSubBlockEnd.gif        }

17ExpandedSubBlockEnd.gif    }

18ExpandedBlockEnd.gif}

19 None.gif

PS:之所以把代理执行写成接口,是因为我不喜欢把具体实现让Remoting客户端看见

  接着写一个Remoting服务器,我选择用Windows服务的形式提供,服务器里包含代理执行的接口实现以及服务类:
 1 None.gif namespace  TuxedoRemoting
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif    [Serializable]
 4InBlock.gif    public class Tuxedo : MarshalByRefObject, TuxedoObject.ITuxedoObject
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 6InBlock.gif        public Tuxedo()
 7ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 8ExpandedSubBlockEnd.gif        }

 9InBlock.gif
10InBlock.gif        public bool ExecuteTuxedo(string[] EnvList, string Server, string Param, out string Output)
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            bool ret = false;
13InBlock.gif
14InBlock.gif            try
15ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
16InBlock.gif                foreach(string env in EnvList)
17InBlock.gif                    Bea.Tuxedo.ATMI.Utils.tuxputenv(env);
18InBlock.gif                AppContext ac = AppContext.tpinit(null);
19InBlock.gif
20InBlock.gif                TypedBuffer its = new TypedString(Param);
21InBlock.gif                TypedBuffer rts = new TypedString("");
22InBlock.gif
23InBlock.gif                ac.tpcall(Server, its, ref rts, 0);
24InBlock.gif
25InBlock.gif                Output = (rts as TypedString).GetString();
26InBlock.gif
27InBlock.gif                ret = true;
28InBlock.gif                try
29ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
30InBlock.gif                    ac.tpterm();
31ExpandedSubBlockEnd.gif                }

32InBlock.gif                catch
33ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
34ExpandedSubBlockEnd.gif                }

35ExpandedSubBlockEnd.gif            }

36InBlock.gif            catch (Exception Err)
37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
38InBlock.gif                Output = "Tuxedo执行异常," + Err.Message;
39ExpandedSubBlockEnd.gif            }

40InBlock.gif            return ret;
41ExpandedSubBlockEnd.gif        }

42ExpandedSubBlockEnd.gif    }

43InBlock.gif
44InBlock.gif    public partial class TuxedoService : ServiceBase
45ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
46InBlock.gif        public TuxedoService()
47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
48InBlock.gif            InitializeComponent();
49ExpandedSubBlockEnd.gif        }

50InBlock.gif
51InBlock.gif        protected override void OnStart(string[] args)
52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
53InBlock.gif            TuxedoObject.TuxedoFac.TuxedoType = typeof(Tuxedo);
54InBlock.gif            TcpChannel channel = new TcpChannel(18000);
55InBlock.gif            ChannelServices.RegisterChannel(channel, false);
56InBlock.gif
57InBlock.gif            RemotingConfiguration.ApplicationName = "TuxedoRemoting";
58InBlock.gif            RemotingConfiguration.RegisterActivatedServiceType(typeof(TuxedoObject.TuxedoFac));
59ExpandedSubBlockEnd.gif        }

60InBlock.gif
61InBlock.gif        protected override void OnStop()
62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
63InBlock.gif            IChannel[] channels = ChannelServices.RegisteredChannels;
64InBlock.gif            foreach (IChannel channel in channels)
65ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
66InBlock.gif                if (channel is TcpChannel)
67ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
68InBlock.gif                    TcpChannel ch = channel as TcpChannel;
69InBlock.gif                    ch.StopListening(null);
70ExpandedSubBlockEnd.gif                }

71InBlock.gif
72InBlock.gif                ChannelServices.UnregisterChannel(channel);
73ExpandedSubBlockEnd.gif            }

74ExpandedSubBlockEnd.gif        }

75ExpandedSubBlockEnd.gif    }

76InBlock.gif
77ExpandedBlockEnd.gif}

78 None.gif

  好了,下面该写ASP.NET了,我只给出部分调用方法,很容易:

1 None.gif TcpChannel channel  =   new  TcpChannel();
2 None.gifChannelServices.RegisterChannel(channel,  false );
3 ExpandedBlockStart.gifContractedBlock.gif object [] attrs  =   dot.gif new UrlAttribute("tcp://xxx.xxx.xxx.xxx:18000/TuxedoRemoting") } ;
4 None.gifTuxedoFac tf  =  (TuxedoFac)Activator.CreateInstance( typeof (TuxedoFac),  null , attrs);
5 None.gifITuxedoObject Tuxedo  =  tf.GetTuxedoObject();
6 ExpandedBlockStart.gifContractedBlock.gifTuxedo.ExecuteTuxedo( new   string []  dot.gif { TuxedoEnv } , Service, Param,  out  Output);

转载于:https://www.cnblogs.com/AndyHai/archive/2007/07/12/815536.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值