Articles → MICROSOFT AZURE → Configure App Service With Virtual Networks In Azure
Configure App Service With Virtual Networks In Azure
Scenario
- Access the database server using the public IP address
- Configure the virtual network in the app service and access the database server using the private IP
Implementation
- Create an app service
- Create a virtual network
- Configure the virtual network on the app service
- Create a virtual machine to host the database server
- Create a database and tables
- Disassociate public IP address from the virtual machine
- Write code to access the database
- Publish app service
Create An App Service
Click to Enlarge
Create A Virtual Network
Click to Enlarge
Configure The Virtual Network On The App Service
Click to Enlarge
Click to Enlarge
Click to Enlarge
Create A Virtual Machine To Host The Database Server
Click to Enlarge
Click to Enlarge
Click to Enlarge
Create A Database And Tables
Click to Enlarge
Disassociate Public IP Address From The Virtual Machine
Click to Enlarge
Write Code To Access The Database
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p>
<p>
<a href="http://www.asp.net" class="btn btn-primary btn-lg">Learn more »</a>
</p>
</div>
<asp:GridView ID="gvData" runat="server"></asp:GridView>
</asp:Content>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
namespace WebApplication1 {
public partial class _Default: Page {
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
string connectionString = "Data Source=10.0.1.4;Initial Catalog=mydatabase;User Id=demouser;Password=AdminPassword@123";
DataTable table = new DataTable();
using(SqlConnection connection = new SqlConnection(connectionString)) {
using(SqlCommand command = new SqlCommand("select * from category", connection)) {
using(SqlDataAdapter adapter = new SqlDataAdapter(command)) {
adapter.Fill(table);
gvData.DataSource = table;
gvData.DataBind();
}
}
}
}
}
}
}
Publish App Service
Click to Enlarge