Issues when running a client as a service

Questions regarding the use of the .NET SDK 2.0 for Server or Client development or integration into customer products ...

Moderator: uasdknet

Post Reply
bomurugend
Jr. Member
Jr. Member
Posts: 1
Joined: 07 Nov 2017, 13:00

Issues when running a client as a service

Post by bomurugend »

I am trying to run a console client application as a service. Installation seems to work with some minor issues. But, service is always stuck in starting phase.

When installing using ClientService.exe /install:
  • the service starts running even though NoAutoStartService is set to true.
  • nothing is logged from the Run method, even though this works when debugging.
When starting service from Microsoft management console:
  • Error 1053: The service did not respond to the start or control request in a timely fashion
When starting the service using ClientService.exe /service:
  • the service starts running
  • in Microsoft management console the service seems to be stuck in the starting phase
The documentation seems to be lacking when it comes to running clients as services. I have been stuck here for a couple of days now. Any help would be appreciated. Code below.

Code: Select all

class Program
    {
        static ILog log;
        const int SECONDS = 2000;

        static string serverUrl = "opc.tcp://*************.net:53530/OPCUA/SimulationServer";
        static string nodeIdentifier = "Sinusoid1";
        static ushort namespaceIndex = 5;

        static void Main(string[] args)
        {
            // Configure
            log4net.Config.XmlConfigurator.Configure();
            log = LogManager.GetLogger(typeof(Program));

            try
            {
                // Add Process License
                ApplicationLicenseManager.AddProcessLicenses(System.Reflection.Assembly.GetExecutingAssembly(), "License.lic");


                // Trying settings for running as a service
                ApplicationInstance instance = ApplicationInstance.Default;
                instance.NoAutoStartService = true;
                ApplicationInstance.Default.Start(Run, instance);


                log.Info("Application Finished!!!");
            }
            catch (Exception e)
            {

                log.Error("Application failed to run:", e);
            }
        }


        static void Run(object userState)
        {
            Session _session = null;

            log.Info("Attempting to connect!!!");


            // Connect to server
            _session = new Session(userState as ApplicationInstance);

            _session.UseDnsNameAndPortFromDiscoveryUrl = false;
            _session.Connect(serverUrl, SecuritySelection.None);

            log.Info("Connected!!!");

            // Read value from SinusNode
            List<ReadValueId> nodesToRead = new List<ReadValueId>
            {
                new ReadValueId
                {
                    NodeId = new NodeId(IdType.String, nodeIdentifier, namespaceIndex),
                    AttributeId = Attributes.Value
                }
            };

            int i = 5;
            while (i > 0)
            {
                List<DataValue> results = _session.Read(nodesToRead);
                log.Info($"Sinusoid1: {results.First().WrappedValue}");

                Thread.Sleep(1 * SECONDS);
                i--;
            }

            _session.Disconnect();
        }
    }

Code: Select all

<UaApplicationConfiguration>

    <SecuredApplication xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://opcfoundation.org/UA/2011/03/SecuredApplication.xsd">

      <!-- Identify the Application -->
      <ApplicationName>XClientService</ApplicationName>
      <ApplicationUri>urn:localhost:Statoil:XClientService</ApplicationUri>
      <ApplicationType>Client_1</ApplicationType>
      <ProductName>XClientService</ProductName>

      <Extensions>

        <!-- Specify Settings when EXE is run with the /install argument -->
        <Extension>
          <InstallationSettings xmlns="http://unifiedautomation.com/schemas/2011/12/Application.xsd">
            <GenerateCertificateIfNone>true</GenerateCertificateIfNone>
            <!-- Supported values are 1024, 2048, 3072 and 4096. Default is 2048. -->
            <CertificateKeyLength>2048</CertificateKeyLength>
            <!-- Possible values are sha1 and sha256. Default is sha256. -->
            <CertificateHashAlgorithm>sha256</CertificateHashAlgorithm>
            <DeleteCertificateOnUninstall>true</DeleteCertificateOnUninstall>
            <InstallAsService>true</InstallAsService>
            <ServiceName>XClientService</ServiceName>
            <ServiceDescription>Test Service for running OPC UA Client using Unified Automation .Net SDK</ServiceDescription>
            <!--<ServiceStartMode>Auto</ServiceStartMode>-->
            <!--<UsersGroup>LocalSystem</UsersGroup>-->
          </InstallationSettings>
        </Extension>

      </Extensions>
    </SecuredApplication>
  </UaApplicationConfiguration>

Post Reply