0% found this document useful (0 votes)
8 views

ASP.net Core 6 Fundamentals

.NET Core is a cross-platform framework designed for building modern, cloud-enabled applications, supporting multiple programming languages. It includes ASP.NET Core, which is the cross-platform version of ASP.NET, and utilizes the Model-View-Controller (MVC) architectural pattern for web applications. Key features of ASP.NET Core include routing, dependency injection, and serialization, which enhance testability, maintainability, and reusability of code.

Uploaded by

ylnkancheti
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

ASP.net Core 6 Fundamentals

.NET Core is a cross-platform framework designed for building modern, cloud-enabled applications, supporting multiple programming languages. It includes ASP.NET Core, which is the cross-platform version of ASP.NET, and utilizes the Model-View-Controller (MVC) architectural pattern for web applications. Key features of ASP.NET Core include routing, dependency injection, and serialization, which enhance testability, maintainability, and reusability of code.

Uploaded by

ylnkancheti
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 64

.

NET Core 6 Fundamentals FactSet Engineering New Hire –


Spring 2023
James Geng / Jeremy Ling

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Chapter 1 - .NET Universe

• Should I use ASP.NET Framework or ASP.NET Core?

• How does the .NET Standard versioning system work?

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
What is .NET Core?

• Framework for building modern, cloud-enabled, Internet-connected apps


• Cross platform, Windows, Linux, macOS, and Docker.
• High performance
• Flexible deployment
• Modular
• .NET and C#, F#, VB

• 3 different technologies:
– .NET Framework (Windows only)
– .NET Core (Cross platform)
– .NET Standard (For both .NET Framework + .NET Core)

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
ASP.NET vs ASP.NET Core

• ASP.NET stands for: Active Server Pages .NET


• ASP.NET Core is the cross-platform version of ASP.NET.
• Equivalents in other languages:
– Django, Flask (Python)
– Express (JavaScript)
– Spring (Java)
– Rails (Ruby)
• Can be used to build:
– Websites (frontend + backend)
– Web Services (just backend – typically what’s used at FactSet)

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
.NET 6 Application Patterns

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Chapter 2 – Introduction to WebApp / Web API
• Web Application Framework

• Web API Applications

• MVC (Model-View-Controller)

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
What .NET Core can do

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Web Application Framework

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Client Rendered UI

– A client rendered app dynamically renders web UI on the client


– Browser content is rendered initially only using the JavaScript file, which then renders the rest of the
site using the browser.
– Twitter, Facebook, etc.
Advantages
– Allows for rich interactivity that is nearly instant, without requiring a round trip to the server.
– UI event handling and logic run locally on the user's device, reducing server load and cost.
– Supports incremental updates, saving partially completed forms or documents without the user having
to select a button to submit a form.
Disadvantages
– Code for the logic has to be downloaded and executed on the client with an external library.
– Client requirements may only work for user’s high-end devices.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Server Rendered UI

– A web UI app that renders on the server dynamically generates the page's HTML and CSS on the
server in response to a browser request.
– The page arrives at the client ready to display.
– Netflix, Pinterest, Paypal, etc.

Advantages
– The client requirements are minimal because the server does the work of logic and page generation.
– Flexibility of access to protected server resources
– Static site analysis advantages, such as search engine optimization.

Disadvantages
– As the full page has to reload every time, the overall page rendering becomes slow
– User interactions require a round trip to the server to generate UI updates

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
ASP.NET Core MVC

• ASP.NET MVC renders UI on the server and uses a Model-View-Controller (MVC)


architectural pattern.
• User requests are routed to a controller. The controller is responsible for working with the
model to perform user actions or retrieve results of queries. The controller chooses the view
to display to the user, and provides it with any model data it requires.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
MVC (Model-View-Controller)

The Model-View-Controller (MVC) framework is an architectural pattern that separates an


application into three main logical components Model, View, and Controller.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
MVC (Model-View-Controller)

• Models - Components to store data and related logic.

• Views - Part of the application that presents the data content through the user interface.

• Controllers - Part of the application that handles the user interactions.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
MVC Features

• Easy and frictionless testability. Highly testable, extensible and pluggable framework
• To design a web application architecture using the MVC pattern, it offers full control over
your HTML as well as your URLs
• Clear separation of Business, Ul and Input logic using Model, View, and Controller
respectively.
• URL Routing for SEO (Search Engine Optimization) Friendly URLs. Powerful URL- mapping
for comprehensible and searchable URLs
• Support for Test Driven Development (TDD)

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Web API

API stands for Application Programming Interface.


• Web API is a framework that builds HTTP services that reach a broad range of clients, including
browsers and mobile devices.
• .NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
API Types

• REST
– The Representational State Transfer (REST or RESTful) is the best-known API standard. REST defines
routes with a URL
• SOAP
– The Simple Object Access Protocol (SOAP) is another major API protocol. A SOAP API can
communicate over other major internet communication protocols, such as TCP and SMTP, in addition
to HTTP.
• RPC
– The Remote Procedure Call (RPC) protocol can return XML or JSON responses. It differs from SOAP
and REST APIs in a few key ways. As the name suggests, this protocol calls a method rather than a
data resource.
• GraphQL
– GraphQL is a distinct query language, with best practices for its use. GraphQL uses HTTP, similar to a
REST API, transmitting text data in the payload of each request.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
REST API

• Client-server architecture:
– The API interface remains on the client and is separated from the data kept on the server.
• Statelessness:
– Each request made to the API is independent of all others.
• Cacheable:
– A REST API response may be able to retrieve cached data, but you need to specify whether or not your
responses can be cached.
• Layered:
– The API works the same whether it interacts directly with the server or if there are other layers, like a
load balancer or proxy service, between the client and the server.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
API Methods and HTTP Verbs

• GET - Request a resource


• POST - Add a resource
• PUT - Update a resource state
• DELETE - Delete a resource

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
HTTP Status Code

• 100 - 199: Informational

• 200 - 299: Successful

• 300 - 399: Redirection

• 400 - 499: Client Error

• 500 - 599: Server Error

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
API Tool

Postman
– Postman is an API platform for building and using APIs.
– It simplifies each step of the API lifecycle and streamlines collaboration.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
API Tool

Swagger
– Swagger is a set of tools for implementing the OpenAPI specification
– OpenAPI is a specification for describing RESTful APIs
– Specifies an API's endpoints, expected payloads and sample responses
– Aimed to help developers communicate with an API

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Swagger overview of Endpoint Group

Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Swagger Endpoint Details

Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Chapter 3 – ASP.NET Core features
• Routing
• Dependency injection
• Serialization
• Environment & variables (secrets.json, appsettings.json, launch.json)
• Middleware
• Golden Path

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Routing

Routing in ASP.NET Core application is a mechanism to match incoming HTTP requests and dispatch
those requests to the app's executable endpoints, such as controllers, methods, etc.
– When a user request URLs from the server then URLs are handled by the routing system.
– The Routing system try to find out the matching route pattern of requested URL with already registered
routes which are map to controller, actions, files, or other items.
– If there is a matching route entry, then it processes the request, otherwise it returns 404 error.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Routing types

• Convention-based Routing
– Route is determined based on the conventions defined in the route templates which will map the
incoming Requests (i.e. URLs) to controllers and their action methods.
– In ASP.NET Core MVC application, the Convention based Routes are defined within the Configure
method of the Startup.cs class file.
– MapControllerRoute( name: “home", pattern: "{controller}/{action}/{id}");
• Attribute-based Routing
– Route is determined based on the attributes which are configured either at the controller level or at the
action method level.
– Route can be controlled and passed to controller and action by using the attributes that decorate
controllers and methods.
– Flexible, more explicit, reduce routing errors, simple to add more controllers and actions.
• Mixed Routing
– Mix the use of conventional routing and attribute routing. It's typical to use conventional routes for
controllers serving HTML pages for browsers, and attribute routing for controllers serving REST APIs.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Attribute-based Routing Example

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Controller (Method)

[HttpGet]
public IEnumerable<WeatherForecast> GetForecast()
{
return Enumerable.Range(1, 5)
.Select(index => _weatherRepository.GetForecast(DateTime.Now.AddDays(index)))
.ToArray();
}

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Model

public class WeatherForecast


{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }


}

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Endpoints

Endpoints is just something that can be Selected or Executed by the request.


– Executable units that can handle a request
– Matching process gives access to values from URL of request
– Endpoints that can be matched and executed by the app are configured in method UseEndpoints

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Inversion of control (loC)

• Inversion of control (IoC) is a software design principle that states a class should be configured by another class from the
outside, as opposed to configuring dependencies statically.
• It can benefit in pluggability, testability, usability and loose coupling if the management of an application's flow is
transferred to a different part of the application.
• Dependency injection describes a specific use case of IoC -- one where a container provides a configurable application
context that creates, starts, catches and manages pluggable objects.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Dependency Injection

• A design pattern for achieving Inversion of Control (IoC) between classes and their dependencies.
• Used to make a class independent of its dependencies or to create a loosely coupled program.
• Key part of building loosely coupled applications, improving the reusability of code.
• By decoupling the usage of an object, more dependencies can be replaced without needing to change
class.

• Types of dependency injection


– Interface injection.
– Constructor injection.
– Setter injection.
– Method injection.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Dependency Injection

• Advantages
– Dependency injection relieves various code modules from the task of instantiating references to
resources and enables dependencies.
– Programs are more testable, maintainable and reusable.
– Developers can create classes independently that still use each other. They only need to know the
interface of the classes.
– Dependency injection helps in unit testing directly as well.

• Disadvantages
– Can be hard to troubleshoot and debug.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Object Lifetime

• Singleton:
– IoC container will create and share a single instance of a service throughout the application's lifetime.
• Scoped:
– IoC container will create an instance of the specified service type once per request and will be shared
in a single request.
• Transient:
– IoC container will create a new instance of the specified service type every time you ask for it.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Service registration methods

• 5 different ways to register services

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Dependency Injection Sample Code
builder.Services.AddScoped<IWeatherRepository, WeatherRepository>();

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Lab 1 – Controllers, Routing and Dependency Injection

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Serialization

Serialization is the process of converting the state of an object into a form that can be
persisted or transported. The complement of serialization is deserialization, which converts a
stream into an object. Together, these processes allow data to be stored and transferred.

– JSON serialization serializes only public properties and does not preserve type fidelity. JSON is an
open standard that is an attractive choice for sharing data across the web.
– Binary serialization preserves type fidelity, which means that the complete state of the object is
recorded and when you deserialize, an exact copy is created.
– XML and SOAP serialization serializes only public properties and fields and does not preserve type
fidelity. This is useful when you want to provide or consume data without restricting the application that
uses the data.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
JSON Serialization

• Common JSON:
{
“field": {
"id": "203194F2-BF3D-4BE9-92CD-9869C883ED07",
“criteria": {
"range": {
"min": 1,
"max": 100
}
}
}
}
• Serialized JSON
{"field":{"id":"203194F2-BF3D-4BE9-92CD-9869C883ED07","criteria":{"range":{"min":1,"max":100}}}}

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Serialization and deserialization example

var weatherForecast = new WeatherForecast


{
Date = DateTime.Parse("2023-04-01"),
TemperatureCelsius = 75,
Summary = "Fools' Day"
};
var jsonString = await JsonSerializer.SerializeAsync(weatherForecast);

string jsonString =
@"{
""Date"": ""2023-04-01T00:00:00-07:00"",
""TemperatureCelsius"": 75,
""Summary"": ""Fools' Day""
} ";
var weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString);

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Serialization in ASP.NET

[Produces("application/json")]
[Consumes("application/json")]
public class SampleController : ControllerBase

Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Model Validation

• Validation is done on a deserialized object to ensure they meet a set of criteria


• Check if model is valid by calling ModelState.IsValid
• Called automatically in an API controller
• Some example built-in validation attributes:
– [Required]
– [RegularExpression]
– [Range]
– [MinLength] and [MaxLength]
– [ValidateNever]

Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Model Validation Sample

[Required]
[StringLength(8, ErrorMessage = "Description can't be longer than 8")]
public string Description { get; set; }

Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Environments

• Development:
– A development environment in software and web development is a workspace for developers to make
changes without breaking anything in a live environment

• Staging:
– The last step before something goes into production and is visible on the live site

• Production:
– The latest versions of software, products, or updates are pushed live to the intended users

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Configuration in ASP.NET Core

• Settings that an app requires to function:


– Database connection strings
– URLs of external service dependencies
– Wait time between retrying external calls

• Prevents having to hard-code information


• Avoids having sensitive information like passwords in code
• Ability to change settings without recompiling or even redeploying

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Configuration

• Configures the stage of ASP.NET Core


– Local (Development)
– Staging
– Production

Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Configuration Sample appsettings.json

Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Configuration Priorities

• Various ways to set configuration variables (from lowest priority to highest)


• Choice will depend on use case

Technology appsettings.json appsettings.{stage}.json Secret Manager / Environment launchSettings.json


secrets.json variables (FactSet)

Use Case For common For environment- Local use only, stores Used in deployed Local use only, can
settings across all specific settings settings in an environments define multiple profiles
environments obfuscated location
but does not encrypt

Should contain No No Yes Yes No


secrets

Checked into Yes Yes No N/A Yes


source control

Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Lab 2 – Serialization and Model Validation

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Middleware

Middleware is software that's assembled into an app pipeline to handle requests and responses. Each
component:
– Chooses whether to pass the request to the next component in the pipeline.
– Can perform work before and after the next component in the pipeline.
– The ASP.NET Core request pipeline consists of a sequence of request delegates, called one after the
other.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Middleware Order

The following diagram shows the complete request processing pipeline for ASP.NET Core MVC and Razor
Pages apps.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Middleware Registration Sample Code

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Golden Path

• The Golden Path, or the Key User Journey as it is also called, is the key set of steps that a user takes to find a product’s
real value.
• This path should be the ideal default and not focus on exceptions or errors.
• Knowing your product’s ideal path will help you focus your work.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Chapter 4 - Testing and documentation
• Swagger

• Developer exception page

• Debugging

• Log

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Swagger Integration

• Swagger can be easily incorporated into an ASP.NET web API via the Swashbuckle library

• Sample Swagger attribute

• [ApiExplorerSettings(GroupName = "notes")]

Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Developer exception page

• A type of middleware that displays detailed, developer-friendly information about unhandled


request exceptions.
• This information helps you to trace the errors that occur when the application is running in
the Development environment.
• Detailed exception information shouldn't be displayed publicly when the app runs in the
Production environment.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Developer exception page

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Developer Exception Page Sample Code

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Debugging

• Add breakpoints to controller action under test, and make a request to execute the action
• Sample debugging session:

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Log

• ILoggerFactory and ILogger instances are by default injected into the service collection.
• Loggers log to several locations by default, including the console.
• Other logging sinks can be configured by registering more logging providers.

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Lab 3 – Middleware

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Questions (?)

Contacts:
James Geng - [email protected];
Jeremy Ling - [email protected];
George Wilkins - [email protected]
Tamanna Rahman - [email protected]
Kevin Reitano - [email protected]

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
References

• Microsoft .NET 8 resources:


https://learn.microsoft.com/en-us/aspnet/core/release-notes/aspnetcore-8.0?view=aspnetcore-8.0
• .NET and .NET Core Support Policy: https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core
• Our Vision for .NET 9: https://devblogs.microsoft.com/dotnet/our-vision-for-dotnet-9/
• Golden Path: https://designsprintkit.withgoogle.com/methodology/phase2-define/golden-path
• Postman: https://www.postman.com/
• Swagger: https://swagger.io/
• Swagger Sample UI: https://petstore.swagger.io/
• Log: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-6.0

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
The End

Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.

You might also like