用ASP.NET包装第三方组件以提供服务的尝试

来源:互联网 发布:淘宝店铺免费推广软件 编辑:程序博客网 时间:2024/06/11 15:10
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
  不少应用程序的作者为编程人员提供了可以在其它程序中复用的组件。我们通常会在自己的程序中引入这些第三方组件,调用其中现成的函数来实现相对复杂的功能。事实上,我们也可以通过网络发布第三方组件中的函数,以便为更多用户更为方便地提供服务

  对于.NET开发,可以通过ASP.NET(对人)或Web Service(对机器)技术包装第三方组件。下面以用ASP.NET发布Matlab组件中的Execute函数为例说明。前提是服务器上已安装Matlab。

  在VS2005中新建一个ASP.NET站点,在Solution Explorer窗格的站点目录上点击右键选择Add Reference,加入Matlab的COM组件:Matlab Application(version X.X) Type Library。

  在网页上分别添加一个TextBox、一个Button和一个Literal控件。TextBox用以输入表达式,Button确定执行,Literal输出结果。添加以下事件代码,以便调用Matlab组件中的Execute函数执行Matlab语句:

Protected Sub Button1_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles Button1.Click

Dim matlab As New MLApp.MLApp

Dim strMatLab As String

strMatLab = TextBox1.Text

Dim strResult As String = matlab.Execute(strMatLab)

strResult = strResult.Replace(Chr(10), "<br>")

strResult = strResult.Replace(" ", "&nbsp;")

Me.Literal1.Text = strResult

matlab.Quit()

End Sub

  在VS2005中运行这个站点,在出现的网页中输入一个表达式,如“dsolve('Dx=x^2 5')”,点击按钮后随即会显示结果:“ans = 5^(1/2)*tan(5^(1/2)*t 5^(1/2)*C1)”。但如果用IIS发布这个站点,运行时则会出错,提示的原因是ASP.NET没有创建组件的权限。根据出错提示,我们需要在网站的web.config文件中加入一句:

<identity impersonate="true" userName="xx" password="xx"/>

  这里指明的用户应当是服务器端有相关权限的用户。如果不指定则在客户端页面进行身份验证。当然,在web.config中明文保存用户名和密码是不安全的,在真正的应用中要加密。

  就此,我们完成了Matlab表达式求值函数的网络发布。可以通知没有安装Matlab的朋友登录你的网站使用的Matlab强大的计算功能了。(注意:此例仅供演示,Matlab强大的语句和工具箱足以让远程用户通过这个网页访问你的文件系统。)

  由于HTTP协议的性质以及服务器端执行的特性,这种对第三方组件的包装一般适于发布数据处理函数,而不适于发布控制函数。同时这一方法对实时性强或需要保存状态的服务也不很适宜。安全起见,我们也可以对第三方组件进行二次封装,加入数据过滤与异常处理。

  原理非常简单。希望大家能与我讨论这一方案在实际应用中的价值。

参考:

1、李洪根,在.NET中应用MATLAB算法

(http://www.microsoft.com/china/community/Column/25.mspx)

2、Matlab 7.1,Matlab Web Server 相关帮助文档

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击