Many application authors provide programmers with components that can be reused in other programs. We usually introduce these third-party components into our own programs and call ready-made functions to implement relatively complex functions. In fact, we can also publish functions in third-party components through the network to provide services to more users more conveniently.
For .NET development, third-party components can be wrapped through ASP.NET (for humans) or Web Service (for machines) technologies. The following uses ASP.NET to publish the Execute function in the Matlab component as an example. The premise is that Matlab has been installed on the server.
Create a new ASP.NET site in VS2005, right-click on the site directory in the Solution Explorer pane, select Add Reference, and add Matlab's COM component: Matlab Application (version XX) Type Library.
Add a TextBox, a Button and a Literal control to the web page. TextBox is used to enter expressions, Button confirms execution, and Literal outputs results. Add the following event code to call the Execute function in the Matlab component to execute the Matlab statement:
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(" ", " ")
Me.Literal1. Text = strResult
matlab.Quit()
End Sub
Run this site in VS2005, enter an expression in the web page that appears, such as "dsolve('Dx=x^2+5')", and the results will be displayed after clicking the button : "ans = 5^(1/2)*tan(5^(1/2)*t+5^(1/2)*C1)". But if you use IIS to publish this site, an error will occur during runtime. The reason is that ASP.NET does not have the permission to create components. According to the error message, we need to add a sentence to the website's web.config file:
<identity impersonate="true" userName="xx" password="xx"/>
The user specified here should be a user with relevant permissions on the server side. If not specified, authentication will be performed on the client page. Of course, it is not safe to save usernames and passwords in plain text in web.config, and they must be encrypted in real applications.
In this regard, we have completed the online release of Matlab expression evaluation functions. You can notify friends who have not installed Matlab to log in to your website to use the powerful computing functions of Matlab. (Note: This example is for demonstration only. Matlab's powerful statements and toolbox are enough to allow remote users to access your file system through this web page.)
Due to the nature of the HTTP protocol and the characteristics of server-side execution, this kind of packaging of third-party components Generally suitable for publishing data processing functions, but not suitable for publishing control functions. At the same time, this method is not suitable for services that are highly real-time or require state preservation. For security reasons, we can also re-encapsulate third-party components and add data filtering and exception handling.
The principle is very simple. I hope you can discuss with me the value of this solution in practical applications.
Reference:
1. Li Honggen, applying MATLAB algorithm in .NET
( http://www.microsoft.com/china/community/Column/25.mspx )
2. Matlab 7.1, Matlab Web Server related help documents