Opc.Ua.Client.Session.Create C# (CSharp) Method

Create() public static method

Creates a new communication session with a server by invoking the CreateSession service
public static Create ( ApplicationConfiguration configuration, ConfiguredEndpoint endpoint, bool updateBeforeConnect, bool checkDomain, string sessionName, uint sessionTimeout, IUserIdentity identity, IList preferredLocales ) : Session
configuration ApplicationConfiguration The configuration for the client application.
endpoint ConfiguredEndpoint The endpoint for the server.
updateBeforeConnect bool If set to true the discovery endpoint is used to update the endpoint description before connecting.
checkDomain bool If set to true then the domain in the certificate must match the endpoint used.
sessionName string The name to assign to the session.
sessionTimeout uint The timeout period for the session.
identity IUserIdentity The user identity to associate with the session.
preferredLocales IList The preferred locales.
return Session
        public static Session Create( 
            ApplicationConfiguration configuration,
            ConfiguredEndpoint       endpoint,
            bool                     updateBeforeConnect,
            bool                     checkDomain,
            string                   sessionName,
            uint                     sessionTimeout,
            IUserIdentity            identity,
            IList<string>            preferredLocales)
        {
            endpoint.UpdateBeforeConnect = updateBeforeConnect;

            EndpointDescription endpointDescription = endpoint.Description;

            // create the endpoint configuration (use the application configuration to provide default values).
            EndpointConfiguration endpointConfiguration = endpoint.Configuration;
            
            if (endpointConfiguration == null)
            {
                endpoint.Configuration = endpointConfiguration = EndpointConfiguration.Create(configuration);
            }

            // create message context.
            ServiceMessageContext messageContext = configuration.CreateMessageContext();
            
            // update endpoint description using the discovery endpoint.
            if (endpoint.UpdateBeforeConnect)
            {
                BindingFactory bindingFactory = BindingFactory.Create(configuration, messageContext);
                endpoint.UpdateFromServer(bindingFactory);

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

            // checks the domains in the certificate.
            if (checkDomain && endpoint.Description.ServerCertificate != null && endpoint.Description.ServerCertificate.Length > 0)
            {
                CheckCertificateDomain(endpoint);
            }

            X509Certificate2 clientCertificate = null;
			//X509Certificate2Collection clientCertificateChain = null;

            if (endpointDescription.SecurityPolicyUri != SecurityPolicies.None)
            {
                if (configuration.SecurityConfiguration.ApplicationCertificate == null)
                {
                    throw ServiceResultException.Create( StatusCodes.BadConfigurationError, "ApplicationCertificate must be specified." );
                }

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

				if( clientCertificate == null )
				{
                    throw ServiceResultException.Create( StatusCodes.BadConfigurationError, "ApplicationCertificate cannot be found." );
                }

                //load certificate chain
                //clientCertificateChain = new X509Certificate2Collection(clientCertificate);
                //List<CertificateIdentifier> issuers = new List<CertificateIdentifier>();
                //configuration.CertificateValidator.GetIssuers(clientCertificate, issuers);
                //for (int i = 0; i < issuers.Count; i++)
                //{
                //    clientCertificateChain.Add(issuers[i].Certificate);
                //}
            }

            // initialize the channel which will be created with the server.
            ITransportChannel channel = SessionChannel.Create(
                 configuration,
                 endpointDescription,
                 endpointConfiguration,
                 //clientCertificateChain,
                 clientCertificate,
                 messageContext);

            // create the session object.
            Session session = new Session(channel, configuration, endpoint, null);

            // create the session.
			try
			{
				session.Open( sessionName, sessionTimeout, identity, preferredLocales, checkDomain );
			}
			catch
			{
				session.Dispose();
				throw;
			}

            return session;
        }

Same methods

Session::Create ( ApplicationConfiguration configuration, ConfiguredEndpoint endpoint, bool updateBeforeConnect, string sessionName, uint sessionTimeout, IUserIdentity identity, IList preferredLocales ) : Session

Usage Example

        private static async Task RunClient(string address)
        {
            var clientApp = new ApplicationInstance
            {
                ApplicationName   = "OPC UA StackOverflow Example Client",
                ApplicationType   = ApplicationType.Client,
                ConfigSectionName = "Opc.Client"
            };

            // load the application configuration.
            var clientConfig = await clientApp.LoadApplicationConfiguration(true);

            var haveClientAppCertificate = await clientApp.CheckApplicationInstanceCertificate(true, 0);

            if (!haveClientAppCertificate)
            {
                throw new Exception("Application instance certificate invalid!");
            }
            var endpointConfiguration = EndpointConfiguration.Create(clientConfig);
            var selectedEndpoint      = CoreClientUtils.SelectEndpoint(address, true, 15000);
            var endpoint = new ConfiguredEndpoint(null, selectedEndpoint, endpointConfiguration);

            using (var session = await Session.Create(clientConfig, endpoint, false, clientConfig.ApplicationName, 60000, new UserIdentity(new AnonymousIdentityToken()), null))
            {
                Console.WriteLine("Client connected");
                var rootFolders = await session.BrowseAsync(ObjectIds.ObjectsFolder);

                Console.WriteLine("Received root folders");
                var childFolders = await Task.WhenAll(
                    from root in rootFolders
                    select session.BrowseAsync(ExpandedNodeId.ToNodeId(root.NodeId, session.NamespaceUris)));

                foreach (var childFolder in childFolders.SelectMany(x => x).OrderBy(c => c.BrowseName.Name))
                {
                    Console.WriteLine(childFolder.BrowseName.Name);
                }
                Console.WriteLine("Client disconnecting");
                session.Close();
                Console.WriteLine("Client disconnected");
            }
            Console.WriteLine("Client disposed");
        }
All Usage Examples Of Opc.Ua.Client.Session::Create