convertersystems / opc-ua-samples

Sample HMIs using OPC Unified Architecture (OPC UA) and Visual Studio.
MIT License
107 stars 43 forks source link

Why does this throw "LocalPrivateKey is null" #44

Closed basprins closed 5 years ago

basprins commented 5 years ago

Hi Andrew,

Why is this throwing an exception that localprivatekey is null? I am trying to connect without any security.

    public OpcUaClient()
    {
        _appDescription = new ApplicationDescription()
        {
            ApplicationName = "Test",
            ApplicationUri = $"urn:{System.Net.Dns.GetHostName()}:Bla",
            ApplicationType = ApplicationType.Client,
        };
    }

    public async Task Connect()
    {
        const string discoveryUrl = "opc.tcp://localhost:4840"; 

        var appDescription = new ApplicationDescription()
        {
            ApplicationName = "Test",
            ApplicationUri = $"urn:{System.Net.Dns.GetHostName()}:Test",
            ApplicationType = ApplicationType.Client,
        };

        _channel = new UaTcpSessionChannel(
            appDescription,
            null, 
            async args => await Task.FromResult(new AnonymousIdentity()),
            discoveryUrl,
            loggerFactory:null);

        try
        {
            await _channel.OpenAsync();
        }
        catch (Exception e)
        {
          // throws "local private key is null"

/ at Workstation.ServiceModel.Ua.Channels.UaTcpSecureChannel.d119.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) at Workstation.ServiceModel.Ua.Channels.UaTcpSessionChannel.d44.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Workstation.ServiceModel.Ua.Channels.CommunicationObject.d34.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Workstation.ServiceModel.Ua.Channels.CommunicationObject.d34.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at WpfApp1.OpcUaClient.d__3.MoveNext() in C:\Users\baprins\source\repos\WpfApp1\WpfApp1\OpcUaClient.cs:line 43 / }

    }
awcullen commented 5 years ago

Hi. When you open a SessionChannel with just the url, the channel first searches for the most secure endpoint that the server offers. Add the 'securityPolicyUri' parameter to specify the channel should choose an endpoints that matches the policy 'None'

_channel = new UaTcpSessionChannel(
            appDescription,
            null, 
            async args => await Task.FromResult(new AnonymousIdentity()),
            discoveryUrl,
            SecurityPolicyUris.None,   // <---
            loggerFactory:null);
basprins commented 5 years ago

Ah djeeze at least im consitent in leaving out vital arguments.. Thx this is solved