Skip to content

Instantly share code, notes, and snippets.

@NatMarchand

NatMarchand/Dependency.cs Secret

Last active May 4, 2018
Embed
What would you like to do?
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