Summary of ASP. NET installation and deployment problems

Source: Internet
Author: User

It is convenient to deploy Web solutions on the. NET platform. We can use Visual Studio. NET 2003 adds a WEB installation project, and adds the project's main output and content files to the deployed "File System Editor" to easily complete the installation program.

However, the installation program created in this way only installs the DLL files compiled by the web page and asp.net program to the IIS Directory of the target machine. For general applications, such as Access database, can be packaged together in the installer); if the database is SQL SERVER, you need to install the database at deployment, the installation program will be more complex, we need to customize the installer class. Execute the SQL script in the installer class and write the connection string to Web. config.

ASP. NET installation and deployment · Installation Database

Microsoft MSDN introduced how to create a database when deploying an application. For example, this method creates an installer class and calls ADO. NET in the installer class to execute SQL statements and place them in a text file) to create a database.

However, this method has a problem. If SQL Server2000 is used to generate a script file for all table creation, views, and stored procedures, ADO is used. NET to execute this script file, it will be because the script contains many "GO" statements and errors. Of course, we can replace "GO" with a line break and use ADO. NET to execute SQL statements one by one. Obviously, the efficiency is relatively low.

The best way is to call osql to execute the script. Or create a cmd file for the database project, and the cmd file also calls osql when creating the database ).

First, create an asp.net Web application, http: // localhost/VbNetTest, and open the VbNetTest project.

ASP. NET installation and deployment · Create a deployment project

1. Point to "add project" on the "file" menu and select "new project ".

2. In the "Add new project" dialog box, select "Install and deploy Project" in the "project type" pane, and select "Web Installation Project" in the "template" pane ". In the Name box, type Test Installer.

3. Click OK to close the dialog box.

4. The project is added to Solution Explorer and opened in the file system editor.

5. In the Properties window, select ProductName properties and type GCRM.

Add the output of the VbNetTest project to the deployment project.

1. In the file system Editor, select the Web application folder. On the "operations" menu, point to "add" and select "project output ".

2. In the "add project output group" dialog box, select "VbNetTest" from the "project" drop-down list ".

3. Click OK to close the dialog box.

4. Select the "main output" and "Content File" groups from the list, and click "OK ".

ASP. NET installation and deployment · Create installer class

1. Point to "new" on the "file" menu and select "project ".

2. In the "new project" dialog box, select "Visual Basic Project" in the "project type" pane and select "class library" in the "template" pane ". In the "name" box, type DBCustomAction.

3. Click Open to close the dialog box.

4. Select "Add new project" from the "project" menu ".

5. In the "Add new project" dialog box, select "Installer class ". In the "name" box, type DBCustomAction.

6. Click OK to close the dialog box.

ASP. NET installation and deployment · create custom installation dialog box

1. Select the "Test Installer" project in Solution Explorer. On the "View" menu, point to "Editor" and select "User Interface ".

2. In the User Interface Editor, select the "Start" node under "installation. On the "operations" menu, select "add dialog box ".

3. In the "add dialog box", select the "License Agreement" dialog box and click "OK" to close the dialog box.

4. In the Add dialog box, select the text box (A) and click OK to close the dialog box.

5. On the "operations" menu, select "Move Up ". Repeat this step until the "text box (A)" dialog box is located on the "Install Folder" node.

6. In the "properties" window, select the BannerText attribute and type: Install database ..

7. Select the BodyText attribute and type: the installer will install the database on the target machine.

8. Select the Edit1Label attribute and type: Database Name :.

9. Select the Edit1Property property and type CUSTOMTEXTA1.

10. Select the Edit1Value attribute and type: GsCrm.

11. Select the Edit2Label attribute and type: Server Name :.

12. Select the Edit2Property property and type CUSTOMTEXTA2.

13. Select the Edit2Value attribute and type: (local ).

14. Select the Edit3Label attribute and type: User name :.

15. Select the Edit3Value attribute and type sa.

16. Select the Edit3Property property and type CUSTOMTEXTA3.

17. Select the Edit4Label attribute and type: Password :.

18. Select the Edit4Property property and type CUSTOMTEXTA4.

19. Select the Edit2Visible, Edit3Visible, and Edit4Visible attributes, and set them to False.

ASP. NET installation and deployment · create custom operations

1. Select the "Test Installer" project in Solution Explorer. On the "View" menu, point to "Editor" and select "Custom operations ".

2. Select the "Install" node in the Custom operation editor. On the "operations" menu, select "add custom operation ".

3. In the "select project items" dialog box, double-click "application folder ".

4. Select "main output from DBCustomAction activity)" and click "OK" to close the dialog box.

5. in the "properties" window, select the CustomActionData attribute and type/dbname = [CUSTOMTEXTA1]/server = [CUSTOMTEXTA2]/user = [CUSTOMTEXTA3]/pwd = [CUSTOMTEXTA4]/targetdir = "[TARGETDIR]".

Attachment/targetdir = "[TARGETDIR]" is the target path after installation. To obtain the path after installation in the DBCustomAction class, we set this parameter.

In addition, the installation path can also be obtained through Reflection:

Dim Asm As System. Reflection. Assembly = _

System. Reflection. Assembly. GetExecutingAssembly

MsgBox ("Asm. Location ")

Add File

1. Add the script file DB. SQL generated by SQL Server to the "Test Installer" project.

2. Add the Installation File LisenceFile. rtf to the "Test Installer" project.

3. In the User Interface Editor, select the license agreement and set the LisenceFile attribute to the LisenceFile. rtf file.

Files in the project:

Add the code to the installer class, DBCustomAction. vb class

 
 
  1. Imports System. ComponentModel
  2.  
  3. Imports System. Configuration. Install
  4.  
  5. Imports System. IO
  6.  
  7. Imports System. Reflection
  8.  
  9.  
  10.  
  11. <RunInstaller (True)> Public Class DBCustomAction
  12.  
  13. Inherits System. Configuration. Install. Installer 
  14.  
  15.  
  16.  
  17. # Region "code generated by the component designer" 
  18.  
  19. Public Sub New ()
  20.  
  21. MyBase. New ()
  22.  
  23. 'The call is required by the component designer
  24.  
  25. InitializeComponent ()
  26.  
  27. 'Add any initialization after InitializeComponent () is called.
  28.  
  29. End Sub
  30.  
  31. 'Installer override dispose to clear the component list.
  32.  
  33. Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
  34.  
  35. If disposing Then
  36.  
  37. If Not (components Is Nothing) Then
  38.  
  39. Components. Dispose ()
  40.  
  41. End If
  42.  
  43. End If
  44.  
  45. MyBase. Dispose (disposing)
  46.  
  47. End Sub
  48.  
  49. Private components As System. ComponentModel. IContainer
  50.  
  51. <System. Diagnostics. DebuggerStepThrough ()> Private Sub InitializeComponent ()
  52.  
  53. End Sub 
  54.  
  55. # End Region 
  56.  
  57. 'Execute SQL statements
  58.  
  59. Private Sub ExecuteSql (ByVal conn As String, ByVal DatabaseName As String, ByVal SQL As String)
  60.  
  61. Dim mySqlConnection As New SqlClient. SqlConnection (conn)
  62.  
  63. Dim Command As New SqlClient. SqlCommand (SQL, mySqlConnection)
  64.  
  65. Command. Connection. Open ()
  66.  
  67. Command. Connection. ChangeDatabase (DatabaseName)
  68.  
  69. Try
  70.  
  71. Command. ExecuteNonQuery ()
  72.  
  73. Finally
  74.  
  75. 'Close Connection
  76.  
  77. Command. Connection. Close ()
  78.  
  79. End Try
  80.  
  81. End Sub
  82.  
  83. Public Overrides Sub Install (ByVal stateSaver As System. Collections. IDictionary)
  84. MyBase. Install (stateSaver)
  85.  
  86. '------------------------ Create a database -------------------------------------------------
  87.  
  88. Try
  89.  
  90. Dim connStr As String = String. Format ("Data source = {0}; user id = {1}; password = {2}; persist security info = false; packet size = 4096", Me. Context. Parameters. Item ("Server"), Me. Context. Parameters. Item ("User"), Me. Context. Parameters. Item ("Pwd"))
  91.  
  92. 'Create a database based on the input database name
  93.  
  94. ExecuteSql (connStr,"Master","Create database"+ Me. Context. Parameters. Item ("Dbname"))
  95.  
  96. 'Call osql to execute the script
  97.  
  98. Dim sqlProcess As New System. Diagnostics. Process
  99.  
  100. SqlProcess. StartInfo. FileName ="Osql.exe" 
  101.  
  102. SqlProcess. StartInfo. Arguments = String. Format ("-U {0}-P {1}-d {2}-I {3} db. SQL", Me. Context. Parameters. Item ("User"), Me. Context. Parameters. Item ("Pwd"), Me. Context. Parameters. Item ("Dbname"), Me. Context. Parameters. Item ("Targetdir"))
  103.  
  104. SqlProcess. StartInfo. WindowStyle = ProcessWindowStyle. Hidden
  105.  
  106. SqlProcess. Start ()
  107.  
  108. SqlProcess. WaitForExit () 'waiting for execution
  109.  
  110. SqlProcess. Close ()
  111.  
  112. 'Delete the script file
  113.  
  114. Dim sqlFileInfo As New System. IO. FileInfo (String. Format ("{0} db. SQL", Me. Context. Parameters. Item ("Targetdir")))
  115.  
  116. If sqlFileInfo. Exists Then
  117.  
  118. SqlFileInfo. Delete ()
  119.  
  120. End If
  121.  
  122. Catch ex As Exception
  123.  
  124. Throw ex
  125.  
  126. End Try
  127.  
  128.  
  129.  
  130. '--------------------- Write the connection string to Web. config -----------------------------------
  131.  
  132. Try
  133.  
  134. Dim FileInfo As System. IO. FileInfo = New System. IO. FileInfo (Me. Context. Parameters. Item ("Targetdir")&"Web. config")
  135.  
  136. If Not FileInfo. Exists Then
  137.  
  138. Throw New InstallException ("No configuration file found")
  139.  
  140. End If
  141.  
  142. 'Instantiate XML document
  143.  
  144. Dim XmlDocument As New System. Xml. XmlDocument
  145.  
  146. XmlDocument. Load (FileInfo. FullName)
  147.  
  148.  
  149.  
  150. 'Find the node in the deleetask'
  151.  
  152. Dim Node As System. Xml. XmlNode
  153.  
  154. Dim FoundIt As Boolean = False
  155.  
  156. For Each Node In XmlDocument. Item ("Configuration"). Item ("AppSettings")
  157.  
  158. If Node. Name ="Add"Then
  159.  
  160. If Node. Attributes. GetNamedItem ("Key"). Value ="ConnString"Then
  161.  
  162. 'Write the connection string
  163.  
  164. Node. Attributes. GetNamedItem ("Value"). Value = String. Format ("Persist Security Info = False; Data Source = {0}; Initial Catalog = {1}; User ID = {2}; Password = {3}; Packet Size = 4096; pooling = true; Max Pool Size = 100; Min Pool Size = 1",_
  165.  
  166. Me. Context. Parameters. Item ("Server"), Me. Context. Parameters. Item ("Dbname"), Me. Context. Parameters. Item ("User"), Me. Context. Parameters. Item ("Pwd"))
  167.  
  168. FoundIt = True
  169.  
  170. End If
  171.  
  172. End If
  173.  
  174. Next Node
  175.  
  176. If Not FoundIt Then
  177.  
  178. Throw New InstallException ("The web. Config file does not contain the connString connection string Settings")
  179.  
  180. End If
  181.  
  182. XmlDocument. Save (FileInfo. FullName)
  183.  
  184. Catch ex As Exception
  185.  
  186. Throw ex
  187.  
  188. End Try
  189.  
  190. End Sub
  191.  
  192. End Class
  193.  

Finally compiled and generated!

  1. ASP. net mvc jQuery Delete Link
  2. Introduce jquery jqrte control in ASP. net mvc Framework
  3. Sample ASP. net mvc project: Suteki. Shop
  4. Example of ASP. net mvc three-tier architecture
  5. The concept of dependency injection in ASP. net mvc Architecture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.