Connectivity Software User's Guide and Reference
GetService Method (EasySparkplugEdgeNodeCore)
Example 



OpcLabs.EasySparkplug Assembly > OpcLabs.EasySparkplug Namespace > EasySparkplugEdgeNodeCore Class : GetService Method

The value of this parameter cannot be null (Nothing in Visual Basic).

Syntax
'Declaration
 
Public Overrides Function GetService( _
   ByVal serviceType As Type _
) As Object
'Usage
 
Dim instance As EasySparkplugEdgeNodeCore
Dim serviceType As Type
Dim value As Object
 
value = instance.GetService(serviceType)
public override object GetService( 
   Type serviceType
)
public:
Object^ GetService( 
   Type^ serviceType
) override 

Parameters

serviceType

The value of this parameter cannot be null (Nothing in Visual Basic).

Return Value

This method can return null (Nothing in Visual Basic).

Exceptions
ExceptionDescription

A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

Remarks

 

Sparkplug is a trademark of Eclipse Foundation, Inc. "MQTT" is a trademark of the OASIS Open standards consortium. Other related terms are trademarks of their respective owners. Any use of these terms on this site is for descriptive purposes only and does not imply any sponsorship, endorsement or affiliation.

Example
// This example shows how to monitor birth, death, and rebirth of a Sparkplug edge node and its devices.
//
// You can use any Sparkplug application, including our SparkplugCmd utility and the SparkplugApplicationConsoleDemo
// program, to subscribe to the edge node data. 
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using Microsoft.Extensions.DependencyInjection;
using OpcLabs.EasySparkplug;
using System;
using OpcLabs.EasySparkplug.Services;

namespace SparkplugDocExamples.EdgeNode._SparkplugProducerMonitoring
{
    class EdgeNodeAndDevices
    {
        static public void Main1()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the edge node object and hook events.
            var edgeNode = new EasySparkplugEdgeNode(hostDescriptor, "easyGroup", "easySparkplugDemo");
            edgeNode.SystemConnectionStateChanged += (sender, eventArgs) =>
            {
                // Display the new connection state (such as when the connection to the broker succeeds or fails).
                Console.WriteLine($"{nameof(EasySparkplugEdgeNode.SystemConnectionStateChanged)}: {eventArgs}");
            };

            // Define a metric providing random integers.
            var random = new Random();
            SparkplugMetric.CreateIn(edgeNode, "MyMetric").ReadValueFunction(() => random.Next());

            // Define two devices, each with a single metric providing random integers.
            SparkplugDevice myDevice1 = SparkplugDevice.CreateIn(edgeNode, "MyDevice1");
            SparkplugMetric.CreateIn(myDevice1, "MyMetric1").ReadValueFunction(() => random.Next());
            SparkplugDevice myDevice2 = SparkplugDevice.CreateIn(edgeNode, "MyDevice2");
            SparkplugMetric.CreateIn(myDevice2, "MyMetric2").ReadValueFunction(() => random.Next());

            // Obtain monitoring services and hook events to them.
            ISparkplugProducerMonitoring edgeNodeMonitoring = edgeNode.GetService<ISparkplugProducerMonitoring>();
            if (!(edgeNodeMonitoring is null))
            {
                // Monitor the edge node itself.
                edgeNodeMonitoring.Birth += (sender, eventArgs) =>
                    Console.WriteLine($"{sender}.{nameof(edgeNodeMonitoring.Birth)}");
                edgeNodeMonitoring.Death += (sender, eventArgs) =>
                    Console.WriteLine($"{sender}.{nameof(edgeNodeMonitoring.Death)}");
                edgeNodeMonitoring.Rebirth += (sender, eventArgs) =>
                    Console.WriteLine($"{sender}.{nameof(edgeNodeMonitoring.Rebirth)}");

                // Monitor all devices in the edge node.
                foreach (SparkplugDevice device in edgeNode.Devices)
                {
                    ISparkplugProducerMonitoring deviceMonitoring = device.GetService<ISparkplugProducerMonitoring>();
                    if (!(deviceMonitoring is null))
                    {
                        deviceMonitoring.Birth += (sender, eventArgs) =>
                            Console.WriteLine($"{sender}.{nameof(deviceMonitoring.Birth)}");
                        deviceMonitoring.Death += (sender, eventArgs) =>
                            Console.WriteLine($"{sender}.{nameof(deviceMonitoring.Death)}");
                        deviceMonitoring.Rebirth += (sender, eventArgs) =>
                            Console.WriteLine($"{sender}.{nameof(deviceMonitoring.Rebirth)}");
                    }
                }
            }

            // Start the edge node.
            Console.WriteLine("The edge node is starting...");
            edgeNode.Start();

            Console.WriteLine("The edge node is started.");
            Console.WriteLine();

            // Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the edge node...");
            Console.ReadLine();

            // Stop the edge node.
            Console.WriteLine("The edge node is stopping...");
            edgeNode.Stop();

            Console.WriteLine("The edge node is stopped.");
        }
    }
}
' This example shows how to monitor birth, death, and rebirth of a Sparkplug edge node and its devices.
'
' You can use any Sparkplug application, including our SparkplugCmd utility and the SparkplugApplicationConsoleDemo
' program, to subscribe to the edge node data.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports Microsoft.Extensions.DependencyInjection
Imports OpcLabs.EasySparkplug
Imports OpcLabs.EasySparkplug.Services

Namespace Global.SparkplugDocExamples.EdgeNode._SparkplugProducerMonitoring
    Class EdgeNodeAndDevices
        Public Shared Sub Main1()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the edge node object and hook events.
            Dim edgeNode = New EasySparkplugEdgeNode(hostDescriptor, "easyGroup", "easySparkplugDemo")
            AddHandler edgeNode.SystemConnectionStateChanged,
                Sub(sender, eventArgs)
                    ' Display the new connection state (such as when the connection to the broker succeeds or fails).
                    Console.WriteLine($"{NameOf(EasySparkplugEdgeNode.SystemConnectionStateChanged)}: {eventArgs}")
                End Sub

            ' Define a metric providing random integers.
            Dim random = New Random()
            SparkplugMetric.CreateIn(edgeNode, "MyMetric").ReadValueFunction(Function() random.Next())

            ' Define two devices, each with a single metric providing random integers.
            Dim myDevice1 As SparkplugDevice = SparkplugDevice.CreateIn(edgeNode, "MyDevice1")
            SparkplugMetric.CreateIn(myDevice1, "MyMetric1").ReadValueFunction(Function() random.Next())
            Dim myDevice2 As SparkplugDevice = SparkplugDevice.CreateIn(edgeNode, "MyDevice2")
            SparkplugMetric.CreateIn(myDevice2, "MyMetric2").ReadValueFunction(Function() random.Next())

            ' Obtain monitoring services and hook events to them.
            Dim edgeNodeMonitoring As ISparkplugProducerMonitoring = edgeNode.GetService(Of ISparkplugProducerMonitoring)()
            If Not edgeNodeMonitoring Is Nothing Then
                ' Monitor the edge node itself.
                AddHandler edgeNodeMonitoring.Birth, Sub(sender, EventArgs) _
                    Console.WriteLine($"{sender}.{NameOf(edgeNodeMonitoring.Birth)}")
                AddHandler edgeNodeMonitoring.Death, Sub(sender, EventArgs) _
                    Console.WriteLine($"{sender}.{NameOf(edgeNodeMonitoring.Death)}")
                AddHandler edgeNodeMonitoring.Rebirth, Sub(sender, EventArgs) _
                    Console.WriteLine($"{sender}.{NameOf(edgeNodeMonitoring.Rebirth)}")

                ' Monitor all devices in the edge node.
                For Each device As SparkplugDevice In edgeNode.Devices
                    Dim deviceMonitoring As ISparkplugProducerMonitoring = device.GetService(Of ISparkplugProducerMonitoring)()
                    If Not deviceMonitoring Is Nothing Then
                        AddHandler deviceMonitoring.Birth, Sub(sender, eventArgs) _
                            Console.WriteLine($"{sender}.{NameOf(deviceMonitoring.Birth)}")
                        AddHandler deviceMonitoring.Death, Sub(sender, eventArgs) _
                            Console.WriteLine($"{sender}.{NameOf(deviceMonitoring.Death)}")
                        AddHandler deviceMonitoring.Rebirth, Sub(sender, eventArgs) _
                            Console.WriteLine($"{sender}.{NameOf(deviceMonitoring.Rebirth)}")
                    End If
                Next
            End If

            ' Start the edge node.
            Console.WriteLine("The edge node is starting...")
            edgeNode.Start()

            Console.WriteLine("The edge node is started.")
            Console.WriteLine()

            ' Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the edge node...")
            Console.ReadLine()

            ' Stop the edge node.
            Console.WriteLine("The edge node is stopping...")
            edgeNode.Stop()

            Console.WriteLine("The edge node is stopped.")
        End Sub
    End Class
End Namespace
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also