惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
GbyAI
GbyAI
爱范儿
爱范儿
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
有赞技术团队
有赞技术团队
罗磊的独立博客
Recent Announcements
Recent Announcements
博客园 - 司徒正美
M
MIT News - Artificial intelligence
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
B
Blog RSS Feed
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网

博客园 - AndyHai

用C#实现的黑客帝国中的字符雨特效 谁动了我的构造函数? 一个PCM音频转换与混音的示例 关心则乱 让ASPX和ASMX脱离IIS运行的例子(ASP.NET宿主程序) SQL 中如何对纪录进行拆分 带有空值提示的TextBox NAT类型检测方法(转载) 在.NET中探测U盘的插入/拔出 用WebService实现中国移动的Provision反向接口 一个动态加载/卸载DLL的例子 Tuxedo 搞定! 用Multi-Media Library实现的波形音频录制与播放 用Multi-Media Library制作流式音频播放器 研究如何用Multi-Media Library播放波形数据 又多一道面试题 面试 RTP协议
用ASP.NET调用Tuxedo
AndyHai · 2007-07-12 · via 博客园 - AndyHai

  由于项目原因,需要在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, nullnullnull);
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

  好了,下面该写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);