Example #1
0
        //
        //  GetLicense is the entry point called by the LicenseManager at design-time,
        //  build-time, and runtime.  It is the only method defined by the 
        //  LicenseProvider base class (though GetKey and IsKeyValid are defined by
        //  LicFileLicenseProvider, which we derive from).
        //

        public override License GetLicense(
            LicenseContext context, 
            Type type, 
            object instance, 
            bool allowExceptions
            )
        {
            License license;      

            if ( traceSwitch.TraceVerbose )
            {
                Trace.WriteLine( String.Format("GetLicense({0}, {1})", context.UsageMode, type ) );
                Trace.Indent();
            }


            //
            // Save the instance and context parameters in our fields, since we
            // will need their values again in IsKeyValid() and GetKey().
            //        
    
            this.instance = instance;
            this.context = context;
            this.licenseComponent = GetLicenseComponent(type, instance);
            this.licenseKeyString = null;

            
            if( traceSwitch.TraceVerbose )
            {
                Trace.WriteLine( "Context is of type:   " + context.GetType() );
                Trace.WriteLine( "Full name of type is: " + type.AssemblyQualifiedName);
                Trace.WriteLine( "...and it has GUID:   " + type.GUID);
                if( this.licenseComponent != null )
                {
                    Trace.WriteLine( "License Component found with type: " + this.licenseComponent.GetType().AssemblyQualifiedName );
                }
                else
                {
                    Trace.WriteLine( "License Component was NOT found." );
                }
            }


            //
            // Call the base class GetLicense(), which will find the appropriate
            // license file or embedded license reference, and subsequently call
            // IsKeyValid() and GetKey().
            //

            license = base.GetLicense(context, type, instance, allowExceptions);


            //
            // At this point IsKeyValid() and GetKey() have been called.
            // If the license object is null, we have an invalid license. 
            // Handle failures.
            //

            if( traceSwitch.TraceVerbose )
            {
                if( license != null )
                {
                    Trace.WriteLine( "License was granted." );
                    Trace.WriteLine( "  License:            " + license.ToString() );
                    Trace.WriteLine( "  License.LicenseKey: " + license.LicenseKey );
                }
                else
                {
                    Trace.WriteLine( "License was NOT granted." );
                }
            }

           
            if( license == null )
            {
                //
                // Raise the license check failed event.  Note that we may 
                // sometimes have a NULL licenseComponent (for example, if the
                // caller used the wrong overload of LicenseManager.Validate()).
                //

                if( licenseComponent != null )
                {
                    licenseComponent.NotifyLicenseCheckFailed(
                        licenseCheckFailureReason, 
                        type, 
                        instance, 
                        licenseKeyString
                        );
                }                

                if( traceSwitch.TraceVerbose )
                {
                    Trace.WriteLine( String.Format("GetLicense: license was not granted.  Reason={0}", licenseCheckFailureReason) );
                    Trace.Unindent();
                }
                return null;
            }


            //
            // At this point, we have a valid license generated by 
            // LicFileLicenseProvider.  We'll create our own License object
            // to return to the caller.
            //

            LicXLicense l = new LicXLicense( license );
            
            if( traceSwitch.TraceVerbose )
            {
                Trace.WriteLine( String.Format("GetLicense: license granted.  Key={0}", license.LicenseKey) );
            }          


            //
            // If this is an evaluation license, and we are set up to nag during
            // evaluation at runtime, raise the "evaluation license used" event.
            //

            if(
                ( context.UsageMode == LicenseUsageMode.Runtime ) && 
                ( l.IsEvaluation ) && 
                ( licenseComponent.NagDuringEvaluation )
                )                
            {
                licenseComponent.NotifyEvaluationLicenseUsed(
                    type, 
                    instance, 
                    l.LicenseKey
                    );
            }                        
            Trace.Unindent();
            return l;
        }