• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

net-daemon / netdaemon / 6659031396

26 Oct 2023 07:47PM UTC coverage: 79.577% (-1.2%) from 80.727%
6659031396

Pull #958

github

web-flow
Merge 90fc48222 into 4aede33a5
Pull Request #958: .NET 8 version 4 release

806 of 1148 branches covered (0.0%)

Branch coverage included in aggregate %.

33 of 33 new or added lines in 7 files covered. (100.0%)

2923 of 3538 relevant lines covered (82.62%)

50.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.76
/src/AppModel/NetDaemon.AppModel.SourceDeployedApps/ServiceCollectionExtension.cs
1
using System.Reflection;
2
using Microsoft.Extensions.DependencyInjection;
3
using NetDaemon.AppModel.Internal.AppAssemblyProviders;
4
using NetDaemon.AppModel.Internal.Compiler;
5

6
namespace NetDaemon.AppModel;
7

8
/// <summary>
9
///     ServiceCollection extensions
10
/// </summary>
11
public static class ServiceCollectionExtensions
12
{
13
    /// <summary>
14
    ///     Add apps from c# source code using the configuration source to find path
15
    /// </summary>
16
    /// <param name="services">Services</param>
17
    /// <param name="useDebug">Override UseDebug in CompileSettings</param>
18
    public static IServiceCollection AddAppsFromSource(this IServiceCollection services, bool useDebug = false)
19
    {
20
        // // We make sure we only add AppModel services once
21
         services
13✔
22
            .AddNetDaemonAppModel()
13✔
23
            .AddSingleton<Compiler>()
13✔
24
            .AddSingleton<ICompiler>(s => s.GetRequiredService<Compiler>())
15✔
25
            .AddSingleton<SyntaxTreeResolver>()
13✔
26
            .AddSingleton<ISyntaxTreeResolver>(s => s.GetRequiredService<SyntaxTreeResolver>())
15✔
27
            .AddOptions<CompileSettings>().Configure(settings => settings.UseDebug = useDebug);
28✔
28

29
        // We need to compile it here so we can dynamically add the service providers
30
        var assemblyResolver = ActivatorUtilities.CreateInstance<DynamicallyCompiledAppAssemblyProvider>(services.BuildServiceProvider());
13✔
31
        services.RegisterDynamicFunctions(assemblyResolver.GetAppAssembly());
13✔
32

33
        // And now register the assembly resolver that will have the assembly already compiled
34
        services.AddSingleton(assemblyResolver);
12✔
35
        services.AddSingleton<IAppAssemblyProvider>(s => s.GetRequiredService<DynamicallyCompiledAppAssemblyProvider>());
22✔
36

37
        return services;
12✔
38
    }
39

40
    private static IServiceCollection RegisterDynamicFunctions(this IServiceCollection services, Assembly assembly)
41
    {
42
        var methods = assembly.GetTypes().SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public))
36!
43
                          .Where(m => m.GetCustomAttribute<ServiceCollectionExtensionAttribute>() != null).ToArray() ??
2✔
44
                      Array.Empty<MethodInfo>();
12✔
45

46
        if (methods.Any(
12!
47
                m => m.GetParameters().Length != 1 && m.GetParameters()[0].GetType() != typeof(IServiceProvider)))
14!
48
            throw new InvalidOperationException(
×
49
                "Methods with [ServiceCollectionExtension] Attribute should have exactly one parameter of type IServiceCollection");
×
50

51
        foreach (var methodInfo in methods) methodInfo.Invoke(null, new object?[] {services});
30✔
52

53
        return services;
12✔
54
    }
55
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc