LicenseProvider Problem

Hello Experts,
I'm developing a user control in VB.NET 2003, using a LicenseProvider Class to validate the license code.
Everything works fine when I drag and drop the control from the Toolbox Window to the Designer Form.
But the problem is copy and paste.  When I copy and paste the control, the designer doesn't paste anything.
What is wrong? and how can I solve this problem?


-------------------------------------------------------------------------------------------------------------

'The class that handles the license validation
Friend Class MyLicenser
     Inherits System.ComponentModel.LicenseProvider
     ...
   
   Public Overrides Function GetLicense(ByVal context As System.ComponentModel.LicenseContext, ByVal [type] As System.Type, ByVal instance As Object, ByVal allowExceptions As Boolean) As System.ComponentModel.License
     ....
    'Returns true when the code is validated
   End Function

End Class

---------------------------------------------------------------------------------------------------------------------

'The user control implements the LicenseProviderAttribute attribute:
<LicenseProviderAttribute(GetType(MyLicenser))> Public NotInheritable Class MyUserControl
      ....
     Private _license As License = Nothing
     Public Sub New()
          _license = LicenseManager.Validate(GetType(MyUserControl), Me)
          'An exception is thrown is the code is incorrect.
     End Sub
End Class

LVL 3
PigtorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Here is a possibility:

http://www.dotnet247.com/247reference/msgs/53/268999.aspx

I'm able to reproduce this issue, seems the LicenseContext was not set
correctly in copy & paste operation, I'll forward this issue to the product
group to let them know.

In the mean time, I think you may try way to workaround this issue:
1. If you put the license validation code in the constructor of the user
control, remove the License Validation code from the constructor
2. Define a customized LicenseContext class derive from DesigntimeLicenseContext class like below:

<code>
Public Class MyDesignTimeLicenseContext
  Inherits System.ComponentModel.Design.DesigntimeLicenseContext

    Private provider As IDesignerHost
    Public Sub New(ByVal host As IDesignerHost)
        provider = host
    End Sub
    Public Overrides Function GetService(ByVal type As Type) As Object
        Return provider.GetService(type)
    End Function
End Class
</code>

2. Override OnLoad event for you user control , insert the code snippet like below:
<code>
If DesignMode Then
    Dim host As IDesignerHost = CType(Me.GetService(GetType(IDesignerHost), IDesignerHost)
    LicenseManager.CurrentContext = New MyDesignTimeLicenseContext(host)
    _license = LicenseManager.Validate(GetType(MyUserControl), Me)
    LicenseManager.CurrentContext = Nothing
Else
   _license = LicenseManager.Validate(GetType(MyUserControl), Me)
End If
</code>

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PigtorAuthor Commented:
Thanks Bob, but I have already see that link and I agree with Brock opinion:
"The constructor will always be called and we are guaranteed that the licensing code will get
called. Users could override the On* event and not call the base version and get a free control."

I have tested the OnLoad Event and works fine, but when I create a new UserControl and override the OnLoad event, the control doesn't validate anything.

Do you know any other way to fix this behavior?

I was thinking about implementing a ControlDesigner class.
Using a control designer I can customize the menu that is displayed when the user right clicks the control in the designer, but I don't know how to control the keyboard (CTRL+C, CTRL+V) and the menu Copy and Paste.

'The user control implements the Designer Attribute
<Designer(GetType(MyControlDesigner)), LicenseProviderAttribute(GetType(MyLicenser))> _
Public NotInheritable Class MyUserControl
'....
End Class

'The Designer determines the behavior of the control in design time.
Friend Class MyControlDesigner
     Inherits System.Windows.Forms.Design.ControlDesigner

    Public Overrides ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection
         Get
              '.....
         End Get
    End Property
End Class



Thanks

Victor
0
Bob LearnedCommented:
I don't think that you need to implement a special designer to do that.  I have a 3rd party control as a reference that lets you copy/paste controls without apparently having a control designer, but only a LicenseProvider.  It sounds like a problem with the GetLicense function, but I am not sure what that could be.  

Are you making a call like this in GetLicense?

    text2 = context.GetSavedLicenseKey(type, Nothing)

Bob
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

PigtorAuthor Commented:
No Bob,  I use a LicenseKey Property that the user can change before creating the control.
The GetLicense method reads that property to determine if the key is correct or incorrect.

Public NotInheritable Class Licenser
           Private Shared mvarKey As String = String.Empty
           Public Shared Property LicenseKey() As String
                    Get
                         Return mvarKey
                    End Get
                    Set(ByVal Value As String)
                         mvarKey = Value
                    End Set
          End Property
End Class

For example:
Dim obj as MyControl
obj.Licenser.LicenseKey="123"
obj = New MyControl

What do you recommend?



0
Bob LearnedCommented:
I think that I am starting to see the problem.  When you copy/paste, you are instantiating a control, without setting the LicenseKey, and so the control doesn't validate.  I would think that you would need to do some fancy footwork to get the LicenseKey property from the control when pasting and instantiating a new control.

Bob
0
PigtorAuthor Commented:
What do you mean with fancy footwork, do you mean writing to disk the key or something like that?
What is the difference between draging the control from the Toolbox, and copy-paste?  One works and the other not, and both doesn't validate.

Victor


0
Bob LearnedCommented:
Since confession is good for the soul, then I should probably confess that I haven't done that much with the LicenseProvider (just dabbling), so I really have no idea what I mean.  I was hoping that I could find an answer and learn something and help you at the same time.

Bob
0
PigtorAuthor Commented:
Thanks anyway Bob.
I know there is something to do about, but I will try to find out.
0
Bob LearnedCommented:
1) Do you have a LicenseManager instance?

2) I found a pretty good article for licensing:

http://windowsforms.net/articles/Licensing.aspx

3) We need to find a way to debugging the licensing portion (if it's possible).

Bob
0
PigtorAuthor Commented:
That was not the answer I was looking for, because I already don't know how to do It,  but I appreciate your help Bob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.