Skip to content

Instantly share code, notes, and snippets.

@NatMarchand
Last active May 4, 2018 11:49
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 NatMarchand/9082944cbe0d57c1322e3a490f54c780 to your computer and use it in GitHub Desktop.
Save NatMarchand/9082944cbe0d57c1322e3a490f54c780 to your computer and use it in GitHub Desktop.
webformdependencyinjection_start
using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
namespace Microsoft.Extensions.DependencyInjection.WebForms.Sample
{
[DebuggerDisplay("Dependency #{" + nameof(Id) + "}")]
public class Dependency : IDependency
{
private static int _id;
public int Id { get; }
public Dependency()
{
Id = Interlocked.Increment(ref _id);
}
public string GetFormattedTime() => DateTimeOffset.UtcNow.ToString("f", CultureInfo.InvariantCulture);
}
public interface IDependency
{
int Id { get; }
string GetFormattedTime();
}
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" CodeBehind="Index.aspx.cs" Inherits="Autofac.Integration.Web.Sample.Index" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeaderPlaceHolder" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder" runat="server">
<h1>Hi from Index</h1>
<div><%=Dependency.GetFormattedTime() %></div>
<div>Dependency #<%=Dependency.Id %></div>
</asp:Content>
using System;
using System.Web.UI;
namespace Microsoft.Extensions.DependencyInjection.WebForms.Sample
{
public partial class Index : Page
{
protected IDependency Dependency { get; }
public Index(IDependency dependency)
{
Dependency = dependency;
}
}
}
<%@ Master Language="C#" CodeBehind="Main.master.cs" Inherits="Autofac.Integration.Web.Sample.Main" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="HeaderPlaceHolder" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder" runat="server">
</asp:ContentPlaceHolder>
</div>
<div><%=Dependency.GetFormattedTime() %></div>
<div>Dependency #<%=Dependency.Id %></div>
</form>
</body>
</html>
using System;
using System.Web.UI;
namespace Microsoft.Extensions.DependencyInjection.WebForms.Sample
{
public partial class Main : MasterPage
{
protected IDependency Dependency { get; }
public Main(IDependency dependency)
{
Dependency = dependency;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment