Opc.Ua.Com.Client.ComClient.CreateInstance C# (CSharp) Method

CreateInstance() public method

Creates an instance of the COM server.
public CreateInstance ( ) : void
return void
        public void CreateInstance()
        {        
            // multiple calls are not allowed - may block for a while due to network operation.
            lock (m_lock)
            {
                ServerFactory factory = new ServerFactory();
                
                try
                {
                    // create the server.
                    Unknown = factory.CreateServer(new Uri(m_url), null);

                    // set the locale.
                    SetLocale(LocaleId);

                    if (UserIdentity != null)
                    {
                        SetUserIdentity(UserIdentity);
                    }

                    // do any post-connect processing.
                    OnConnected();
                }
                catch (Exception e)
                {
                    ComUtils.TraceComError(e, "Could not connect to server ({0}).", m_url);

                    // cleanup on error.
                    Close();
                }
                finally
                {
                    factory.Dispose();
                }
            }
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// Returns a localized client for the specified locale id.
        /// </summary>
        /// <param name="identity">The identity.</param>
        /// <param name="localeId">The locales id.</param>
        /// <returns>A localized client.</returns>
        public ComClient GetLocalizedClient(IUserIdentity identity, int localeId)
        {
            // check if a logon is required.
            string userName = null;

            if (identity != null && identity.TokenType == UserTokenType.UserName)
            {
                userName = (identity.GetIdentityToken() as UserNameIdentityToken).UserName;
            }

            if (String.IsNullOrEmpty(userName) && localeId == ComUtils.LOCALE_SYSTEM_DEFAULT)
            {
                Utils.Trace("COM Client Selected: DEFAULT (no match for locale)");
                return(DefaultClient);
            }

            // create the key.
            StringBuilder buffer = new StringBuilder();

            buffer.Append(localeId);

            if (!String.IsNullOrEmpty(userName))
            {
                buffer.Append(':');
                buffer.Append(userName);
            }

            string key = buffer.ToString();

            if (m_localizedClients == null)
            {
                m_localizedClients = new Dictionary <string, ComClient>();
            }

            ComClient client = null;

            if (!m_localizedClients.TryGetValue(key, out client))
            {
                client              = CreateClient();
                client.Key          = key;
                client.LocaleId     = localeId;
                client.UserIdentity = identity;
                client.CreateInstance();
                m_localizedClients[key] = client;
            }

            // Utils.Trace("COM Client Seleted: {0}", key);
            return(client);
        }