Ejemplo n.º 1
0
        /// <summary>
        /// Create a session with the given UA server
        /// </summary>
        /// <param name="discoveryUrl"></param>
        /// <param name="configSection"></param>
        /// <returns></returns>
        public static Session CreateSession(string discoveryUrl, string configSection)
        {
            // Step 1 -- Load configuration
            ApplicationConfiguration configuration = ApplicationConfiguration.Load(configSection, ApplicationType.Client);

            ClientUtils.CheckApplicationInstanceCertificate(configuration);

            // Step 2 -- Select an endpoint
            // create the endpoint description
            EndpointDescription endpointDescription = ClientUtils.SelectEndpoint(discoveryUrl, false);

            endpointDescription.SecurityMode      = MessageSecurityMode.None;
            endpointDescription.SecurityPolicyUri = @"http://opcfoundation.org/UA/SecurityPolicy#None";

            // create the endpoint configuration
            EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(configuration);

            // the default timeout for a requests sent using the channel.
            endpointConfiguration.OperationTimeout = 600000;
            // create the endpoint.
            ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);

            // choose the encoding strategy: true: BinaryEncoding; false: XmlEncoding
            endpoint.Configuration.UseBinaryEncoding = true;

            // create the binding factory.
            BindingFactory bindingFactory = BindingFactory.Create(configuration, configuration.CreateMessageContext());

            // update endpoint description using the discovery endpoint.
            if (endpoint.UpdateBeforeConnect)
            {
                endpoint.UpdateFromServer(bindingFactory);

                //Console.Error.WriteLine("Updated endpoint description for url: {0}\n", endpointDescription.EndpointUrl);

                endpointDescription   = endpoint.Description;
                endpointConfiguration = endpoint.Configuration;
            }

            X509Certificate2 clientCertificate = configuration.SecurityConfiguration.ApplicationCertificate.Find();

            // set up a callback to handle certificate validation errors.
            configuration.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);

            // Step 3 -- Initialize the channel which will be created with the server.
            SessionChannel channel = SessionChannel.Create(
                configuration,
                endpointDescription,
                endpointConfiguration,
                bindingFactory,
                clientCertificate,
                null);

            // Step 4 -- Create a session
            Session session = new Session(channel, configuration, endpoint);

            session.ReturnDiagnostics = DiagnosticsMasks.All;
            string       sessionName = "SenderSession";
            UserIdentity identity    = new UserIdentity(); // anonymous

            session.KeepAlive += new KeepAliveEventHandler(Session_KeepAlive);
            session.Open(sessionName, identity);
            return(session);
        }