Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

SDT

Simple webapp in ASP.NET Core

Basharat Hussain
PhD (perusing), Department of Computer Science
Email: basharat@live.com
Software Development Technologies 1
Sayings of Quran
WAH Campus

Software Development Technologies 2


Agenda topics
WAH Campus

 Creating .NET Core Web APP

 Create free student account subscription in Azure

 Publish in Azure

 Create a SQL server in Azure

 Connect with your WebAPP the SQL server

Software Development Technologies 3


Create an ASP.NET Core web app in Azure
WAH Campus

 In Visual Studio:
• Create a project by selecting File > New > Project.
• Select Visual C# > Web > ASP.NET Core Web
Application.
Hint: If you do not see the ASP.NET Core Web Application
project type, learn how to 
modify your Visual Studio installation to include the Web
Development workload.
• Name the application myFirstAzureWebApp
• Click OK.

Software Development Technologies 4


Run the application after creation
WAH Campus

Software Development Technologies 5


<li>Reviews</li>

<li>User Guide</li>

<li>Categories</li>

Index.cshtml content to add <li>Home</li>

<ul id="ul">

<div id="head">
WAH Campus
<h1 id="h1"> Shop<b>Online</b></h1>

<div id="main">

<h2>Use this page to demonstrate the Zure deployment by Basharat Hussain.</h2>

Double click and open the code >>

<div class="m31"></div>

<div class="m31"></div>

<div class="m31"></div>

&nbsp;Hot Deals</div>
<div id="m3"> New Stuff &nbsp;&nbsp;Top Sellers &nbsp;

<div id="mid3">

</div>

Software Development Technologies <div id="m2"></div>


6
</div>

&nbsp;do eiusmod tempcidi.


&nbsp;amet, &nbsp;consecur &nbsp;&nbsp;adipisicing &nbsp;&nbsp;elit, &nbsp;sed
Run the webapp on localhost
WAH Campus

Software Development Technologies 7


WAH Campus

Q. What is the difference between the services.AddTransient and service.AddScoped methods in ASP.NET Core?
 public void ConfigureServices(IServiceCollection services)
 {
 // Add framework services.
  
 // Add application services.
 services.AddTransient<IEmailSender, AuthMessageSender>();
 services.AddScoped<IEmailSender, AuthMessageSender>();
}

Software Development Technologies 8


WAH Campus

In .NET's dependency injection there are three major lifetimes:


 
Singleton which creates a single instance of the service throughout the application when it is first requested. It creates the instance for the first time and reuses the
same object in all calls.
 memory leaks in these services will build up over time.
 also memory efficient as they are created once reused everywhere.
 use Singletons where you need to maintain application wide state. Application configuration or parameters, Logging Service, caching of data is
some of the examples where you can use singletons.

Scoped lifetime services are created once per request within the scope. It is equivalent to a singleton in the current scope. For example, in MVC it creates one
instance for each HTTP request, but it uses the same instance in the other calls within the same web request.
 better option when you want to maintain state within a request.
 with every HTTP request, we get a new instance. However, within the same HTTP request, if the service is required in multiple places, like in the view
and in the controller, then the same instance is provided for the entire scope of that HTTP request.

Transient lifetime services are created each time they are requested. This lifetime works best for lightweight, stateless services.

 since they are created every time they will use more memory & resources and can have the negative impact on performance
 use this for the lightweight service with little or no state. 9
public interface IEmployeeRepository
{
IEnumerable<Employee> GetAllEmployees();
Employee Add(Employee employee);
} WAH Campus
public class Employee
{
 Mocking repository public int Id { get; set; }
public string Name { get; set; }
}

public class MockEmployeeRepository : IEmployeeRepository


{
private List<Employee> _employeeList;

public MockEmployeeRepository()
{
_employeeList = new List<Employee>()
{
new Employee() { Id = 1, Name = "Mary" },
new Employee() { Id = 2, Name = "John" },
new Employee() { Id = 3, Name = "Sam" },
};
}

public Employee Add(Employee employee)


{
employee.Id = _employeeList.Max(e => e.Id) + 1;
_employeeList.Add(employee);
return employee;
}

public IEnumerable<Employee> GetAllEmployees()


{
Software Development Technologies return _employeeList; 10
}
}
public class HomeController : Controller
{
private IEmployeeRepository _employeeRepository;
WAH Campus
public HomeController(IEmployeeRepository employeeRepository)
{
 HomeController _employeeRepository = employeeRepository;
}

[HttpGet]
public ViewResult Create()
{
return View();
}

[HttpPost]
public IActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
Employee newEmployee = _employeeRepository.Add(employee);
}

return View();
Software Development Technologies } 11
}
WAH Campus

@model Employee
 Create View @inject IEmployeeRepository empRepository

<form asp-controller="home" asp-action="create" method="post">


<div>
<label asp-for="Name"></label>
<div>
<input asp-for="Name">
</div>
</div>

<div>
<button type="submit">Create</button>
</div>

<div>
Total Employees Count = @empRepository.GetAllEmployees().Count().ToString()
</div>
</form>
Software Development Technologies 12
WAH Campus

 Startup.cs

public void ConfigureServices(IServiceCollection services)


{
services.AddMvc();
services.AddSingleton<IEmployeeRepository, MockEmployeeRepository>();
}

 Copy-paste this code and press on the create button


in the view and switch between AddSingleton ,
AddScoped and AddTransient you will get each time
a different result that will might help you understand
this.

Software Development Technologies 13


WAH Campus

Software Development Technologies 14


WAH Campus

Software Development Technologies 15


WAH Campus

Software Development Technologies 16


Course Titles
WAH Campus

The End

Software Development Technologies 17

You might also like