Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ ccd9b352d4066839cbe2dff010df6ab464ea871e

# Apply dotnet format across the solution.
2fea688003a3947a656d91e0532cd2dff2a737c6

# Convert to file-scoped namespaces and drop redundant usings.
f089e7856c50b437f6583ba069a3ed553d1f7d61
14 changes: 14 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AnalysisLevel>latest-recommended</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)build/Source.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
17 changes: 17 additions & 0 deletions build/AspNet.ruleset
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Autofac Examples ASP.NET Rules" Description="Analyzer rules for example projects hosted in ASP.NET." ToolsVersion="16.0">
<Include Path="Source.ruleset" Action="Default" />
<Rules AnalyzerId="Microsoft.CodeAnalysis.Analyzers" RuleNamespace="Microsoft.CodeAnalysis.Analyzers">
<!-- Remove underscores from member name - Web Forms generates _Default for
Default.aspx, and AutoEventWireup requires the Page_Load name. -->
<Rule Id="CA1707" Action="None" />
<!-- Change names to avoid reserved word overlaps - Global.asax expects a type named Global. -->
<Rule Id="CA1716" Action="None" />
<!-- Use LoggerMessage delegates - the right call in production, and noise wrapped around a two-line sample. -->
<Rule Id="CA1848" Action="None" />
<!-- Argument evaluation may be unnecessary if logging is disabled - same tradeoff as CA1848. -->
<Rule Id="CA1873" Action="None" />
<!-- Set ViewStateUserKey - wants a per-user value to key on, and these samples have no user identity. -->
<Rule Id="CA5368" Action="None" />
</Rules>
</RuleSet>
7 changes: 7 additions & 0 deletions build/Source.ruleset
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Autofac Examples Rules" Description="Analyzer rules for all example projects." ToolsVersion="16.0">
<!-- Rule severities come from AnalysisLevel in Directory.Build.props. This file
records deliberate deviations from that baseline, of which there are
currently none that apply to every project. Anything ASP.NET specific
belongs in AspNet.ruleset instead. -->
</RuleSet>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<CodeAnalysisRuleSet>../../build/AspNet.ruleset</CodeAnalysisRuleSet>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions src/AspNetCoreExample/AspNetCoreExample.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<CodeAnalysisRuleSet>../../build/AspNet.ruleset</CodeAnalysisRuleSet>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
4 changes: 1 addition & 3 deletions src/AspNetCoreExample/Services/IValuesService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace AspNetCoreExample.Services;
namespace AspNetCoreExample.Services;

public interface IValuesService
{
Expand Down
9 changes: 3 additions & 6 deletions src/AspNetCoreExample/Services/ValuesService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace AspNetCoreExample.Services;
namespace AspNetCoreExample.Services;

public class ValuesService : IValuesService
{
Expand All @@ -14,14 +11,14 @@ public ValuesService(ILogger<ValuesService> logger)

public IEnumerable<string> FindAll()
{
_logger.LogDebug("{method} called", nameof(FindAll));
_logger.LogDebug("{Method} called", nameof(FindAll));

return new[] { "value1", "value2" };
}

public string Find(int id)
{
_logger.LogDebug("{method} called with {id}", nameof(Find), id);
_logger.LogDebug("{Method} called with {Id}", nameof(Find), id);

return $"value{id}";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<CodeAnalysisRuleSet>../../build/AspNet.ruleset</CodeAnalysisRuleSet>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
1 change: 0 additions & 1 deletion src/AspNetCoreNoStartupExample/AutofacModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using AspNetCoreNoStartupExample.Services;
using Autofac;
using Microsoft.Extensions.Logging;

namespace AspNetCoreNoStartupExample;

Expand Down
4 changes: 1 addition & 3 deletions src/AspNetCoreNoStartupExample/Services/ValuesService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.Extensions.Logging;

namespace AspNetCoreNoStartupExample.Services;
namespace AspNetCoreNoStartupExample.Services;

public class ValuesService : IValuesService
{
Expand Down
2 changes: 1 addition & 1 deletion src/AttributeMetadataExample/AppenderMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class AppenderMetadata
{
public string AppenderName
public string? AppenderName
{
get; set;
}
Expand Down
3 changes: 1 addition & 2 deletions src/AttributeMetadataExample/AppenderNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition;

namespace AttributeMetadataExample;

Expand Down
4 changes: 1 addition & 3 deletions src/AttributeMetadataExample/AttributeMetadataAppender.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace AttributeMetadataExample;
namespace AttributeMetadataExample;

[AppenderName("attributed")]
public class AttributeMetadataAppender : ILogAppender
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace AttributeMetadataExample;
namespace AttributeMetadataExample;

public class InterfaceManualMetadataAdapter : ILogAppender
{
Expand Down
6 changes: 2 additions & 4 deletions src/AttributeMetadataExample/Log.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Autofac.Features.Metadata;
using Autofac.Features.Metadata;

namespace AttributeMetadataExample;

Expand All @@ -15,7 +13,7 @@ public Log(IEnumerable<Meta<ILogAppender>> appenders)

public void Write(string destination, string message)
{
var appender = _appenders.First(a => a.Metadata["AppenderName"].Equals(destination));
var appender = _appenders.First(a => destination.Equals(a.Metadata["AppenderName"]));
appender.Value.Write(message);
}
}
3 changes: 1 addition & 2 deletions src/AttributeMetadataExample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Diagnostics;
using Autofac;
using Autofac.Extras.AttributeMetadata;
using Autofac.Features.AttributeFilters;
Expand Down
4 changes: 1 addition & 3 deletions src/AttributeMetadataExample/StringManualMetadataAppender.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace AttributeMetadataExample;
namespace AttributeMetadataExample;

public class StringManualMetadataAppender : ILogAppender
{
Expand Down
4 changes: 1 addition & 3 deletions src/AttributeMetadataExample/TypedManualMetadataAppender.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace AttributeMetadataExample;
namespace AttributeMetadataExample;

public class TypedManualMetadataAppender : ILogAppender
{
Expand Down
7 changes: 2 additions & 5 deletions src/ConfigurationExample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Loader;
using Autofac;
Expand Down Expand Up @@ -35,7 +32,7 @@ public static void Main()
// https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/corehost.md
//
// To verify, try commenting this out and you'll see that the config system can't load the external plugin type.
var executionFolder = Path.GetDirectoryName(typeof(Program).Assembly.Location);
var executionFolder = Path.GetDirectoryName(typeof(Program).Assembly.Location) ?? AppContext.BaseDirectory;
AssemblyLoadContext.Default.Resolving += (AssemblyLoadContext context, AssemblyName assembly) => context.LoadFromAssemblyPath(Path.Combine(executionFolder, $"{assembly.Name}.dll"));

var config = new ConfigurationBuilder()
Expand Down
27 changes: 13 additions & 14 deletions src/ConfigurationExampleInterface/IPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
namespace ConfigurationExampleInterface
namespace ConfigurationExampleInterface;

/// <summary>
/// Simple plugin interface used to illustrate assembly loading/plugin handling
/// via configuration. See the "ConfigurationExample" project for usage.
/// </summary>
public interface IPlugin
{
/// <summary>
/// Simple plugin interface used to illustrate assembly loading/plugin handling
/// via configuration. See the "ConfigurationExample" project for usage.
/// Gets the name of the plugin.
/// </summary>
public interface IPlugin
/// <value>
/// A <see cref="string"/> with the plugin name. Likely just the type name for illustrative purposes.
/// </value>
string Name
{
/// <summary>
/// Gets the name of the plugin.
/// </summary>
/// <value>
/// A <see cref="string"/> with the plugin name. Likely just the type name for illustrative purposes.
/// </value>
string Name
{
get;
}
get;
}
}
29 changes: 14 additions & 15 deletions src/ConfigurationExamplePlugin/ExternalPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
using ConfigurationExampleInterface;

namespace ConfigurationExamplePlugin
namespace ConfigurationExamplePlugin;

/// <summary>
/// Implementation of the plugin interface that will be loaded from an external assembly. See the "ConfigurationExample"
/// project for more details.
/// </summary>
public class ExternalPlugin : IPlugin
{
/// <summary>
/// Implementation of the plugin interface that will be loaded from an external assembly. See the "ConfigurationExample"
/// project for more details.
/// Gets the name of the plugin.
/// </summary>
public class ExternalPlugin : IPlugin
/// <value>
/// Always returns <c>ExternalPlugin</c>.
/// </value>
public string Name
{
/// <summary>
/// Gets the name of the plugin.
/// </summary>
/// <value>
/// Always returns <c>ExternalPlugin</c>.
/// </value>
public string Name
get
{
get
{
return "ExternalPlugin";
}
return "ExternalPlugin";
}
}
}
7 changes: 2 additions & 5 deletions src/GenericHostBuilderExample/HostedService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting;

namespace GenericHostBuilderExample;

internal class HostedService : IHostedService
internal sealed class HostedService : IHostedService
{
private readonly ILogger _logger;

// ReSharper disable once UnusedMember.Global
public HostedService(ILogger logger)
{
_logger = logger;
Expand Down
4 changes: 1 addition & 3 deletions src/GenericHostBuilderExample/ILogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Threading.Tasks;

namespace GenericHostBuilderExample;
namespace GenericHostBuilderExample;

internal interface ILogger
{
Expand Down
7 changes: 2 additions & 5 deletions src/GenericHostBuilderExample/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Threading.Tasks;
namespace GenericHostBuilderExample;

namespace GenericHostBuilderExample;

internal class Logger : ILogger
internal sealed class Logger : ILogger
{
public async Task Log(string value)
{
Expand Down
3 changes: 1 addition & 2 deletions src/GenericHostBuilderExample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Threading.Tasks;
using Autofac;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down
4 changes: 1 addition & 3 deletions src/MultitenantExample.ConsoleApplication/BaseDependency.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace MultitenantExample.ConsoleApplication;
namespace MultitenantExample.ConsoleApplication;

/// <summary>
/// Base class for dependencies. Used simply to avoid redundant code; it's not
Expand Down
4 changes: 1 addition & 3 deletions src/MultitenantExample.ConsoleApplication/IDependency.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace MultitenantExample.ConsoleApplication;
namespace MultitenantExample.ConsoleApplication;

/// <summary>
/// Demonstration dependency interface that allows you to inspect the unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ManualTenantIdentificationStrategy : ITenantIdentificationStrategy
/// <see cref="MultitenantExample.ConsoleApplication.ManualTenantIdentificationStrategy.TryIdentifyTenant"/>
/// when the current tenant ID is requested.
/// </value>
public object CurrentTenantId
public object? CurrentTenantId
{
get; set;
}
Expand All @@ -38,9 +38,9 @@ public object CurrentTenantId
/// <returns>
/// This implementation always returns <see langword="true"/>.
/// </returns>
public bool TryIdentifyTenant(out object tenantId)
public bool TryIdentifyTenant(out object? tenantId)
{
if (CurrentTenantId.ToString() == "0")
if (CurrentTenantId?.ToString() == "0")
{
// 0 is the "default tenant ID"
tenantId = null;
Expand Down
Loading
Loading