Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Last active April 26, 2021 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/907c09d90ac5ee2f8a92467218277d6e to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/907c09d90ac5ee2f8a92467218277d6e to your computer and use it in GitHub Desktop.
Controller action and view are modified for demonstrating Autofac with .NET core web applications
// HomeController.cs
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly ISingletonService singleton;
private readonly IScopedService scoped;
private readonly ITransientService transient;
public HomeController(ILogger<HomeController> logger, ISingletonService singleton,
IScopedService scoped, ITransientService transient)
{
_logger = logger;
this.singleton = singleton;
this.scoped = scoped;
this.transient = transient;
}
public IActionResult Index()
{
ViewData["SingletonCreatedOn"] = this.singleton.CreatedOn;
ViewData["ScopedCreatedOn"] = this.scoped.CreatedOn;
ViewData["TransientCreatedOn"] = this.transient.CreatedOn;
return View();
}
}
// Index.cshtml
@{
ViewData["Title"] = "Home Page";
}
<div class="text-left">
<h1 class="display-4">Welcome</h1>
<p>Learn about Autofac integration with ASP .NET Core Web Apps</p>
<div class="card" style="margin-top:2rem; width: 40rem">
<div class="card-header">
Created On Times (Transient and Scoped is same in this case, can you guess why ?)
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item text-left"><strong>Singleton.CreatedOn:</strong> @ViewData["SingletonCreatedOn"]</li>
<li class="list-group-item text-left"><strong>Scoped.CreatedOn:</strong> @ViewData["ScopedCreatedOn"]</li>
<li class="list-group-item text-left"><strong>Transient.CreatedOn:</strong> @ViewData["TransientCreatedOn"]</li>
</ul>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment