developpement:dotnet:msioc
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédente | |||
developpement:dotnet:msioc [2023/05/16 04:52] – [Exemple] sgariepy | developpement:dotnet:msioc [2023/06/19 18:43] (Version actuelle) – [Exemple] sgariepy | ||
---|---|---|---|
Ligne 153: | Ligne 153: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ====== Exemple exécutable dans LinqPad 7 ====== | ||
+ | |||
+ | Language: C# Program, .NET 7.0 | ||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | string[] args = new string[0]; | ||
+ | |||
+ | using IHost host = Host.CreateDefaultBuilder(args) | ||
+ | .ConfigureServices((_, | ||
+ | services | ||
+ | .AddSingleton< | ||
+ | .AddSingleton< | ||
+ | ) | ||
+ | .Build(); | ||
+ | |||
+ | using IServiceScope serviceScope = host.Services.CreateScope(); | ||
+ | IServiceProvider provider = serviceScope.ServiceProvider; | ||
+ | |||
+ | BasicService basicService = provider.GetService< | ||
+ | |||
+ | // host.Run(); | ||
+ | |||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | |||
+ | #region Services | ||
+ | |||
+ | public class BasicService | ||
+ | { | ||
+ | private readonly ISimpleService _simpleService; | ||
+ | |||
+ | public BasicService( | ||
+ | ISimpleService simpleService) | ||
+ | { | ||
+ | _simpleService = simpleService; | ||
+ | |||
+ | SetupService(); | ||
+ | } | ||
+ | |||
+ | |||
+ | private void SetupService() | ||
+ | { | ||
+ | _simpleService.SayHello(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public interface ISimpleService { | ||
+ | public void SayHello(); | ||
+ | } | ||
+ | |||
+ | public class SimpleService: | ||
+ | { | ||
+ | private readonly ISimpleService _simpleService; | ||
+ | |||
+ | public SimpleService() {} | ||
+ | |||
+ | public void SayHello() | ||
+ | { | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #endregion | ||
+ | </ | ||
+ | |||
developpement/dotnet/msioc.1684205559.txt.gz · Dernière modification : 2023/05/16 04:52 de sgariepy