Files
UA-.NETStandard/SampleApplications/Samples/NetCoreConsoleServer/Program.cs
T
Martin Regen 057912c825 - Fixes found during a CTT test pass. Many fixes also due to sync with .Net codebase.
- Sync codebase of Opc.Ua libraries to UA.Net 1.03.340 source on github.
-CTT stress failures and exceptions all fixed.
-Roughly 2 dozen CTT tests are still with red bubbles. Mainly very special cases.
2016-10-07 16:20:25 +02:00

127 lines
4.2 KiB
C#

/* Copyright (c) 1996-2016, OPC Foundation. All rights reserved.
The source code in this file is covered under a dual-license scenario:
- RCL: for OPC Foundation members in good-standing
- GPL V2: everybody else
RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/
GNU General Public License as published by the Free Software Foundation;
version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2
This source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
using Opc.Ua;
using Opc.Ua.Configuration;
using Opc.Ua.Sample;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace NetCoreConsoleServer
{
public class ApplicationMessageDlg : IApplicationMessageDlg
{
private string message = string.Empty;
private bool ask = false;
public override void Message(string text, bool ask)
{
this.message = text;
this.ask = ask;
}
public override async Task<bool> ShowAsync()
{
if (ask)
{
message += " (y/n, default y): ";
Console.Write(message);
}
else
{
Console.WriteLine(message);
}
if (ask)
{
try
{
ConsoleKeyInfo result = Console.ReadKey();
Console.WriteLine();
return await Task.FromResult((result.KeyChar == 'y') || (result.KeyChar == 'Y') || (result.KeyChar == '\r'));
}
catch
{
// intentionally fall through
}
}
return await Task.FromResult(true);
}
}
public class Program
{
public static void Main(string[] args)
{
try
{
Task t = ConsoleSampleServer();
t.Wait();
Console.WriteLine("Server started. Press any key to exit...");
}
catch (Exception ex)
{
Utils.Trace("ServiceResultException:" + ex.Message);
Console.WriteLine("Exception: {0}", ex.Message);
}
try
{
Console.ReadKey(true);
}
catch
{
// wait forever if there is no console
Thread.Sleep(Timeout.Infinite);
}
}
private static async Task ConsoleSampleServer()
{
ApplicationInstance.MessageDlg = new ApplicationMessageDlg();
ApplicationInstance application = new ApplicationInstance();
application.ApplicationName = "UA Sample Server";
application.ApplicationType = ApplicationType.Server;
application.ConfigSectionName = "Opc.Ua.SampleServer";
// load the application configuration.
ApplicationConfiguration config = await application.LoadApplicationConfiguration(false);
// check the application certificate.
bool haveAppCertificate = await application.CheckApplicationInstanceCertificate(false, 0);
if (!haveAppCertificate)
{
throw new Exception("Application instance certificate invalid!");
}
if (!config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
{
config.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);
}
// start the server.
await application.Start(new SampleServer());
}
private static void CertificateValidator_CertificateValidation(CertificateValidator validator, CertificateValidationEventArgs e)
{
if (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted)
{
e.Accept = false;
Console.WriteLine("Rejected Certificate: {0}", e.Certificate.Subject);
}
}
}
}