Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,
I have a problem with validation of License in the server side of our application.
What I want is when the method
C#
CreateProject(User user)
is invoked
Behind the scenes that will lead to check with the License service if the user has license to create new project, in case the user has no permission that will lead to exception.
If I'm right there way to do it with c#'s mechanism named:
C#
ValidationAttribute

Am i right? If yes, how to do it? If no, what the right way?

What I have tried:

When I've tried to write a small program it's not working.
the program as below:

The MyValidationAttribute attribute definition:

C#
[AttributeUsage(AttributeTargets.Method)]
public class MyValidationAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        int id = (int)value;
        if (id > 0)
            return true;
        throw new Exception("Error");
    }
}


The service to validate:
C#
public class Service
  {
      [MyValidation]
      public bool GetService(int id)
      {
          if (id > 100)
          {
              return true;
          }
          return false;
      }
  }


The main uses this service:
C#
static void Main(string[] args)
       {
           Service service = new Service();
           service.GetService(-8);
       }


But even with negative id the service does non throws exception.
Thanks!
Posted
Updated 17-Apr-16 0:18am

1 solution

GetService is just returning true or false. Nothing else.
 
Share this answer
 
Comments
Member 11090364 17-Apr-16 6:29am    
Yes, but why the MyValidationAttribute does not lead to exception even the id is a negative number?
Have you debugged?
Member 11090364 17-Apr-16 6:53am    
Yes.
When I've put break-point here:
public override bool IsValid(object value)
{
int id = (int)value;
if (id > 0)
return true;
throw new Exception("Error");
}
it does not reach this code.
Then something is wrong. Find it out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900