Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GetLicense question

 
Post new topic   Reply to topic    Infralution Support Forum Index -> Licensing Support
View previous topic :: View next topic  
Author Message
MrDon
Guest





PostPosted: Fri May 12, 2006 1:21 pm    Post subject: GetLicense question Reply with quote

It's not clear, for me, the LicenseContext class. Could you explain what LicenseContext is?

Please, could you give us a sample of the use of EncryptedLicenseProvider.GetLicense method?

Developing components, is GetLicense method the best way to get a License object?

Code:

Overrides  OverloadsPublic Function GetLicense( _
   ByVal context As LicenseContext, _
   ByVal type As Type, _
   ByVal instance As Object, _
   ByVal allowExceptions As Boolean _
) As License


And last: what is the method you can suggest us for getting license informations that will be placed in the "About..." form of our suite of components?

Tnx
Back to top
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Fri May 12, 2006 11:33 pm    Post subject: Reply with quote

Quote:
It's not clear, for me, the LicenseContext class. Could you explain what LicenseContext is?

The LicenseContext class is part of the .NET framework licensing classes (ie it was written by Microsoft). When you are developing components or controls it allows you determine whether your controls is being used at design time or runtime. You generally don't have to create instances of this class as the .NET runtime and VisualStudio do this and set the value in LicenseManager.CurrentContext. You just check what the LicenseManager.CurrentContext is and respond accordingly.

Quote:
Developing components, is GetLicense method the best way to get a License object?


If you are developing components or controls then you shouldn't call the EncryptedLicenseProvider directly to get a license. Instead you MUST do so through the framework LicenseManager methods (IsLicensed or IsValid). This is important because otherwise your runtime licenses for your control will not get generated and embedded in the resources for licensed applications that use your control. You must also ensure that you add the LicenseProvider attribute to the control class - so that the LicenseManager knows the correct LicenseProvider class to use for your component.

See the LicenseControl sample project for a fully worked example.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
MrDon
Guest





PostPosted: Mon May 15, 2006 11:52 am    Post subject: Reply with quote

I successfully used the thecnique described in the topic "Can I license all the controls in a library with one key?" of the FAQ.

What I need now is simply retrieve licensing informations about:
- a given component of the library
- all the components of the library

To achieve this, I'm going to write a generic procedure that can be used:
- from an external application that uses the library
- from a procedure inside the library project, ut outside a component code

This is the code I tried:
Code:

  Dim sProvider As ControlLicenseProvider 'Inherits from EncryptedLicenseProvider
  Dim sType As System.Type
  Dim sAssembly As System.Reflection.Assembly
  Dim sLicense As License
  Dim sEncryptedLicense As EncryptedLicense

  sType = GetType(Components.MyControl1)
  sAssembly = System.Reflection.Assembly.GetAssembly(sType)
  sProvider = New ControlLicenseProvider
  sProvider.SetParameters(LICENSE_PARAMETERS)
  sLicense = sProvider.GetLicense(LicenseManager.CurrentContext, sType, sAssembly, True)
  sEncryptedLicense = CType(sLicense, EncryptedLicense)


...and this is another attempt:
Code:

  Dim sProvider As New ControlLicenseProvider
  Dim sContext As System.ComponentModel.LicenseContext
  Dim sAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
  sProvider.GetLicense(sContext, sAssembly, GetType(Components.MyControl1))
  Dim sEncryptedLicense As EncryptedLicense = sProvider.GetLicense(LICENSE_PARAMETERS, "ControlCollection.lic")
  ControlLicenseProvider.SetParameters(LICENSE_PARAMETERS)
  Dim sComponent As Components.MyControl1 = New Components.MyControl1
  mIsValid = LicenseManager.IsValid(GetType(Components.MyControl1), sComponent, mLicense)


They both does not works.

If possible, check my code, and say me what I missing, because I'm becoming crazy!

Thank you
Back to top
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Tue May 16, 2006 9:18 am    Post subject: Reply with quote

When you say they don't work what do you mean? Do they raise an exception or return null?

Although you normally should use the LicenseManager (IsValid etc) methods when getting licenses for components, I would still expect that your first approach should work

Code:
sType = GetType(Components.MyControl1)
sProvider = New ControlLicenseProvider
sProvider.SetParameters(LICENSE_PARAMETERS)
sLicense = sProvider.GetLicense(LicenseManager.CurrentContext, sType, Nothing, True)


Although the third parameter should be the instance of the component (if any). In this case you can pass Nothing.

Have you purchased the licensing system? If so it is relatively straightforward to trace into the code and see what might be happening. Even without the source code, you can put a break point in your derived GetLicenseFilePath and check that path being returned actually does have a license key in it.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
MrDon
Guest





PostPosted: Tue May 16, 2006 6:25 pm    Post subject: Reply with quote

Quote:
Have you purchased the licensing system?

Yes, I purchased Laughing


According you suggestion, I tried this:
Code:

sType = GetType(Components.MyControl1)
sProvider = New ControlLicenseProvider
sProvider.SetParameters(LICENSE_PARAMETERS)
sLicense = sProvider.GetLicense(LicenseManager.CurrentContext, sType, Nothing, True)

and I got the message "A valid license cannot be granted for the type Components.MyControl1. Contact the manufacturer of the component for more information.". The error occoured at the last line.


In the second approach, I got mIsValid = False and mLicense = Nothing.

Could you please help me with both approaches?

Thank you.
Back to top
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Tue May 16, 2006 11:34 pm    Post subject: Reply with quote

If you haven't already done so, include the source code for the licensing classes (EncryptedLicense, EncryptedLicenseProvider and LicenseInstallForm) in your project and remove the reference to the Infralution.Licensing DLL.

Then you can debug your DLL directly. Put a breakpoint on the line of code where you call GetLicense and step into the method. This method contains the following (in VB):

Code:
Dim licenseKey As String = GetLicenseKey(context, type)
Dim license As license = LoadLicense(context, type, licenseKey)


Check if licenseKey is Nothing following the call to GetLicenseKey. If it is then this means that the license key file was not found. If this returns your key but LoadLicense returns Nothing it means that the license key is not valid.

To debug the first case put a break point in the ReadKeyFromFile method and check whether the filename (check the full path) passed to this method exists. You can then step through the code as it reads the key from file.

If the GetLicenseKey returns a key but LoadLicense fails it means that your saved key is incorrect. I'm assuming this probably isn't the case since you reported that it was working when called from the other code.

My guess is that you simply don't have the license file in the correct location. When you reference a DLL project from another project Visual Studio makes a local copy of the DLL. It sometimes can be difficult to know exactly where Studio is loading the DLL from. This is typically only an issue during development. Search for copies of your DLL and ensure that you have a license file in each directory containing your DLL.

Another option worth considering is changing the location of the license file so that it stored in the Common Application data directory (rather than next to the DLL). The following code in your derived EncryptedLicenseProvider class would achieve this:

Code:

        protected override string GetLicenseDirectory(LicenseContext context, Type type)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            path += @"\MyProduct";
            return path;
        }

         public override EncryptedLicense InstallLicense(Type type, string licenseKey)
        {
            string path = GetLicenseDirectory(LicenseManager.CurrentContext, type);
            Directory.CreateDirectory(path);   
            return base.InstallLicense(type, licenseKey);
        }

        protected override string GetLicenseFilePath(LicenseContext context, Type type)
        {
            string directory = GetLicenseDirectory(context, type);         
            return directory + "\\ControlCollection.lic";
        }


Note that I have to overridden the InstallLicense method to create the directory that will contain the license key if it doesn't already exist.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Wed May 17, 2006 7:16 am    Post subject: Reply with quote

Quote:
If you haven't already done so, include the source code for the licensing classes (EncryptedLicense, EncryptedLicenseProvider and LicenseInstallForm) in your project and remove the reference to the Infralution.Licensing DLL.


Let me better explain my environment: I'm working at designtime. What I need is retrieve the controls collection license informations when the "About" form is displayed or at any other moment, but always at disigntime.
The "About" form is displayed when I call it by code OR when the user clicks on the "About" verb of a control's designer at the bottom of the property browser window of the IDE. This is a very common situation!


Quote:
To debug the first case put a break point in the ReadKeyFromFile method and check whether the filename (check the full path) passed to this method exists. You can then step through the code as it reads the key from file.
[...]
My guess is that you simply don't have the license file in the correct location.


I just discovered that, when my test project starts, the Control1 'ctor calls
Code:
Licensing.ControlLicenseProvider.CheckLicense(GetType(MyControl1), Me)

and ReadKeyFromFile correctly returns the license file qualified name. In fact, the nag screen does not appears. This means that license is valid and accepted.

Later, at designtime, when I click the "About" verb of the property browser of the control, then ReadKeyFromFile fails, returning "c:\documents and settings\user\application data\microsoft\visualstudio\7.1\projectassemblies\rk4vxdcp01\UIComponents.lic". Obviously, this file does ot exist.

I'm thinking that the problem affect both the methods I'm trying for retrieving license informations.

If you think it's better, I can send you a very small project illustrating the problems.

At the moment, I can only wait for your help Smile

Thank you
Back to top
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Wed May 17, 2006 8:09 am    Post subject: Reply with quote

As I said in the previous post Visual Studio may make local copies of assemblies (it will do this if the assembly is not not registered in the GAC). VS2005 makes an additional temporary copy of non-GAC assemblies when they are referenced at design time - hence the strange path you are getting. Even if you do register the assembly in the GAC you still will find that there are two possible locations that VisualStudio may load the assembly from (depending on context) - one is the GAC directory the other is the location of the public assembly that added when you added the reference in Visual Studio.

The best solution to this is to override GetLicenseDirectory to programmatically set the directory where the license is installed as I suggested in my last post.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Wed May 17, 2006 11:27 am    Post subject: Reply with quote

Quote:
VS2005 makes an additional temporary copy of non-GAC assemblies when they are referenced at design time

I'm using VS2003, not VS2005.

Ok, the solution you suggested works, but I cannot say that I like it Rolling Eyes . It seems to be just a patch.

However, at the moment I found very bizarre that I'm not able, simply and merely, to retrieve a *.lic file that IS in the same folder of a DLL. When I say "bizarre", I'm talking about VS behaviour...

Remains just a question, that is strictly related to the statement above: when the Control is initializad ('ctor), the license file is correctly found where it really is (near the DLL). One second later, when I click on "About", the license if searched (and not found) elsewhere . Do you have an idea that explains this?

Thank you...
Back to top
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Wed May 17, 2006 11:38 am    Post subject: Reply with quote

I agree there is some bizarre behaviour with the way Visual Studio handles loading assemblies that are used at design time. VS2003 also makes local shadow copies of non-GAC assemblies - but with VS2005 it gets a whole lot stranger. For this reason we are changing our own controls to store the license file in a known external location (ie common application data). I don't see this as a patch - in fact it has some advantages because it is a little less likely for someone to "accidently" copy your license file when xcopying the DLL (or even worse accidently include the license file in their deployment).
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Wed May 17, 2006 11:53 am    Post subject: Reply with quote

Ok. I agree with you.

Last question, I swear!!!:

When the Control is initializad ('ctor), the license file is correctly found where it really is (near the DLL). One second later, when I click on "About", the license if searched (and not found) elsewhere . Do you have an idea that explains this?

Thank you for your valuable assistance Smile
Back to top
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Wed May 17, 2006 9:55 pm    Post subject: Reply with quote

The difference is probably the LicenseContext. When the control is created VisualStudio does so using a special LicenseContext. I suspect this is not the same as the CurrentContext when you then attempt to click About later.

The GetLicenseDirectory method contains the following code:

Code:
            if (context != null && type != null)
            {
                ITypeResolutionService resolver = (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService));
                if (resolver != null)
                    result = resolver.GetPathOfAssembly(type.Assembly.GetName());
            }


Which will attempt to use the license context to get a TypeResolution service and that in turn to locate the assembly path. If this fails it then resorts to using the Assembly Codebase property. The TypeResolutionService knows about the shadow copies and returns the path to the real assembly. Unfortunately this service is not always available depending on the context and so you can get different paths.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> Licensing Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group