Ejemplo n.º 1
0
        /// <summary>
        /// Crappy method to initialize the session. I don't know what many of these things do, sincerely.
        /// </summary>
        private Session InitializeSession(Uri url)
        {
            var certificateValidator = new CertificateValidator();

            certificateValidator.CertificateValidation += (sender, eventArgs) =>
            {
                if (ServiceResult.IsGood(eventArgs.Error))
                {
                    eventArgs.Accept = true;
                }
                else if ((eventArgs.Error.StatusCode.Code == StatusCodes.BadCertificateUntrusted) && _options.AutoAcceptUntrustedCertificates)
                {
                    eventArgs.Accept = true;
                }
                else
                {
                    throw new OpcException(string.Format("Failed to validate certificate with error code {0}: {1}", eventArgs.Error.Code, eventArgs.Error.AdditionalInfo), eventArgs.Error.StatusCode);
                }
            };
            // Build the application configuration
            var appInstance = new ApplicationInstance
            {
                ApplicationType          = ApplicationType.Client,
                ConfigSectionName        = _options.ConfigSectionName,
                ApplicationConfiguration = new ApplicationConfiguration
                {
                    ApplicationUri       = url.ToString(),
                    ApplicationName      = _options.ApplicationName,
                    ApplicationType      = ApplicationType.Client,
                    CertificateValidator = certificateValidator,
                    ServerConfiguration  = new ServerConfiguration
                    {
                        MaxSubscriptionCount     = _options.MaxSubscriptionCount,
                        MaxMessageQueueSize      = _options.MaxMessageQueueSize,
                        MaxNotificationQueueSize = _options.MaxNotificationQueueSize,
                        MaxPublishRequestCount   = _options.MaxPublishRequestCount
                    },
                    SecurityConfiguration = new SecurityConfiguration
                    {
                        AutoAcceptUntrustedCertificates = _options.AutoAcceptUntrustedCertificates
                    },
                    TransportQuotas = new TransportQuotas
                    {
                        OperationTimeout      = 600000,
                        MaxStringLength       = 1048576,
                        MaxByteStringLength   = 1048576,
                        MaxArrayLength        = 65535,
                        MaxMessageSize        = 4194304,
                        MaxBufferSize         = 65535,
                        ChannelLifetime       = 600000,
                        SecurityTokenLifetime = 3600000
                    },
                    ClientConfiguration = new ClientConfiguration
                    {
                        DefaultSessionTimeout   = 60000,
                        MinSubscriptionLifetime = 10000
                    },
                    DisableHiResClock = true
                }
            };

            // Assign a application certificate (when specified)
            if (_options.ApplicationCertificate != null)
            {
                appInstance.ApplicationConfiguration.SecurityConfiguration.ApplicationCertificate = new CertificateIdentifier(_options.ApplicationCertificate);
            }

            // Find the endpoint to be used
            var endpoints = ClientUtils.SelectEndpoint(url, _options.UseMessageSecurity);

            // Create the OPC session:
            var session = Session.Create(
                configuration: appInstance.ApplicationConfiguration,
                endpoint: new ConfiguredEndpoint(
                    collection: null,
                    description: endpoints,
                    configuration: EndpointConfiguration.Create(applicationConfiguration: appInstance.ApplicationConfiguration)),
                updateBeforeConnect: false,
                checkDomain: false,
                sessionName: _options.SessionName,
                sessionTimeout: _options.SessionTimeout,
                identity: GetIdentity(url),
                preferredLocales: new string[] { });

            return(session);
        }