StaticRoutesXmlConfigurationProvider Class

Class StaticRoutesXmlConfigurationProvider. Implements the ConfigurationProvider

Inheritance Hierarchy

System.Object
  Microsoft.Extensions.Configuration.ConfigurationProvider
    TwinCAT.Ads.Configuration.StaticRoutesXmlConfigurationProvider

Namespace: TwinCAT.Ads.Configuration
Assembly: TwinCAT.Ads.ConfigurationProviders (in TwinCAT.Ads.ConfigurationProviders.dll) Version: 7.0.0+e56d35ccc4675faac24789a4aab60071fc61d470

Syntax

C#

public class StaticRoutesXmlConfigurationProvider : ConfigurationProvider

The StaticRoutesXmlConfigurationProvider type exposes the following members.

Constructors

 

Name

Description

StaticRoutesXmlConfigurationProvider Class 1:

StaticRoutesXmlConfigurationProvider()

Initializes a new instance of the StaticRoutesXmlConfigurationProvider class.

StaticRoutesXmlConfigurationProvider Class 2:

StaticRoutesXmlConfigurationProvider(ILoggerFactory)

Initializes a new instance of the StaticRoutesXmlConfigurationProvider class.

Properties

 

Name

Description

StaticRoutesXmlConfigurationProvider Class 3:

Data

The configuration key value pairs for this provider.
(Inherited from ConfigurationProvider)

MqttBrokers

Gets the MQTT brokers.

Methods

 

Name

Description

StaticRoutesXmlConfigurationProvider Class 4:

Equals

Determines whether the specified object is equal to the current object.
(Inherited from Object)

StaticRoutesXmlConfigurationProvider Class 5:

Finalize

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)

StaticRoutesXmlConfigurationProvider Class 6:

GetChildKeys

Returns the list of keys that this provider has.
(Overrides ConfigurationProvider.GetChildKeys(IEnumerable<String>, String))

StaticRoutesXmlConfigurationProvider Class 7:

GetHashCode

Serves as the default hash function.
(Inherited from Object)

StaticRoutesXmlConfigurationProvider Class 8:

GetReloadToken

Returns a IChangeToken that can be used to listen when this provider is reloaded.
(Inherited from ConfigurationProvider)

StaticRoutesXmlConfigurationProvider Class 9:

GetType

Gets the Type of the current instance.
(Inherited from Object)

StaticRoutesXmlConfigurationProvider Class 10:

Load

Loads (or reloads) the StaticRoutes.xml from the [TwinCATTarget] folder and the Routes child folder.
(Overrides ConfigurationProvider.Load())

StaticRoutesXmlConfigurationProvider Class 11:

MemberwiseClone

Creates a shallow copy of the current Object.
(Inherited from Object)

StaticRoutesXmlConfigurationProvider Class 12:

OnReload

Triggers the reload change token and creates a new one.
(Inherited from ConfigurationProvider)

StaticRoutesXmlConfigurationProvider Class 13:

Set

Sets a value for a given key.
(Inherited from ConfigurationProvider)

StaticRoutesXmlConfigurationProvider Class 14:

ToString

Generates a string representing this provider name and relevant details.
(Inherited from ConfigurationProvider)

StaticRoutesXmlConfigurationProvider Class 15:

TryGet

Attempts to find a value with the given key, returns true if one is found, false otherwise.
(Overrides ConfigurationProvider.TryGet(String, String))

Remarks

To use this provider, add the StaticRoutesXmlConfigurationSource to your application host configuration. See AddStaticRoutesXmlConfiguration(IConfigurationBuilder, ILoggerFactory)

Example

Enhanced Logging/Configuration approach with HostBuilder

class Program
{
    static ILogger s_logger = null;

    public static void Main(string[] args)
    {
    // Create Build and run a Host Builder that runs the ServerWorker Hosted Service
    CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
    //https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line
    //https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration

    var hostBuilder = Host.CreateDefaultBuilder(args)
        .ConfigureServices((hostContext, services) =>
        {
        services.AddHostedService<ServerWorker>();
        })
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
        // Add Configuration providers here ...
        //config.Sources.Clear(); // Clear all default config sources 

        // Different options for configuration
        //config.AddEnvironmentVariables(); // Use Environment variables
        //config.AddCommandLine(args); // Use Command Line
        //config.AddJsonFile("appSettings.json"); // Use AppSettings configuration file as config
        //config.AddStaticRoutesXmlConfiguration(null); // Use configuration from StaticRoutes.Xml 
        })
    .ConfigureLogging((context, logging) =>
    {
        // Create specific Ads Logger configuration for formatted output
        AdsLoggerConfiguration loggerConfig = AdsLoggerConfiguration.CreateFromConfiguration(context.Configuration);
        // Overwrites the configured Loglevel programatically
        // loggerConfig.LogLevel = LogLevel.Debug;

        logging.ClearProviders();
        // Adding console logging here.
        logging.AddProvider(new AdsLoggerProvider(() => loggerConfig)); // Adds the AdsLoggerProvider to logging providers
        logging.SetMinimumLevel(LogLevel.Debug);
    });
    return hostBuilder;
    }

    public class ServerWorker : BackgroundService
    {
    private ILoggerFactory _loggerFactory;
    private ILogger<ServerWorker> _logger;
    private IConfiguration _configuration;

    public ServerWorker(ILoggerFactory loggerFactory, IConfiguration configuration)
    {
        // This is called by the Host (Dependency injection)
        _configuration = configuration;
        _loggerFactory = loggerFactory;

        if (_loggerFactory != null)
        {
        _logger = _loggerFactory.CreateLogger<ServerWorker>();
        }
    }

    protected override async Task ExecuteAsync(CancellationToken cancel)
    {
        AdsSampleServer server1 = new AdsSampleServer(666, "TestAdsServer1", _loggerFactory);
        AdsSampleServer server2 = new AdsSampleServer(999, "TestAdsServer2", _loggerFactory);

        Task[] serverTasks = new Task[2];

        serverTasks[0] = Task.Run(async () =>
        {
        await server1.ConnectServerAndWaitAsync(cancel);
        });

        serverTasks[1] = Task.Run(async () =>
        {
        await server2.ConnectServerAndWaitAsync(cancel);
        });

        Task shutdownTask = Task.Run(async () =>
        {
        await Task.WhenAll(serverTasks);
        _logger.LogInformation("All AdsServers closed down.!");
        });

        Console.WriteLine("Press enter to shutdown servers ...");
        Console.ReadLine();

        server1.Disconnect();
        server2.Disconnect();

        await shutdownTask; // Wait for Shutdown of both Servers
    }
    }
}

See Also

Reference

TwinCAT.Ads.Configuration Namespace ConfigurationProvider StaticRoutesXmlConfigurationSource StaticRoutesXmlConfigurationBuilderExtension

Other Resources

Configuration overview

Beckhoff Automation GmbH & Co. KG 2001-2026