Opc.Ua.Com.ServerFactory.CreateServer C# (CSharp) Method

CreateServer() public method

Connects to the specified COM server server.
public CreateServer ( Uri uri, UserIdentity identity ) : object
uri System.Uri
identity UserIdentity
return object
		public object CreateServer(Uri uri, UserIdentity identity)
		{
			// parse path to find prog id and clsid.
			string progID = uri.LocalPath;
			string clsid  = null;

            while (progID.StartsWith("/"))
            {
                progID = progID.Substring(1);
            }

			int index = progID.IndexOf('/');

			if (index >= 0)
			{
				clsid  = progID.Substring(index+1);
				progID = progID.Substring(0, index);
			}

			// look up prog id if clsid not specified in the uri.
			Guid guid = Guid.Empty;

			if (String.IsNullOrEmpty(clsid))
			{
				// connect to enumerator.
				Connect(uri.Host, identity);

				// use OpcEnum to lookup the prog id.
				guid = CLSIDFromProgID(progID);

				// check if prog id is actually a clsid string.
				if (guid == Guid.Empty)
				{
                    clsid = progID;
				}
			}

            // convert CLSID to a GUID.
            if (guid == Guid.Empty)
            {
			    try 
			    { 
				    guid = new Guid(clsid); 
			    }
			    catch (Exception e)
			    {
                    throw ServiceResultException.Create(StatusCodes.BadCommunicationError, e, "COM server URI does not contain a valid CLSID or ProgID.");
			    }
            }
                       
            // use normal activation.
            return ComUtils.CreateInstance(guid, uri.Host, identity);

            #if COM_IMPERSONATION_SUPPORT
            // set the current thread token.
            IPrincipal existingPrincipal = Thread.CurrentPrincipal;
            WindowsPrincipal principal = ComUtils.GetPrincipalFromUserIdentity(identity);
            
            try
            {
                if (principal != null)
                {
                    Thread.CurrentPrincipal = principal;
                }

                // activate with a license key if one provided.
                if (identity != null && !String.IsNullOrEmpty(identity.LicenseKey))
                {
                    return ComUtils.CreateInstanceWithLicenseKey(guid, uri.Host, identity, identity.LicenseKey);
                }

                // use normal activation.
                return ComUtils.CreateInstance(guid, uri.Host, identity);
            }
            finally
            {
                Thread.CurrentPrincipal = existingPrincipal;
            }
            #endif
		}
		#endregion

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Creates an instance of the COM server.
        /// </summary>
        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);

                    // fetch the available locales.
                    m_availableLocaleIds = QueryAvailableLocales();

                    // 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();
                }
            }
        }
All Usage Examples Of Opc.Ua.Com.ServerFactory::CreateServer