Throw Exception from Custom Attribute

G

Guest

Could someone please tell me how to do the following:

I have a custom Atrribute class that I was hoping to check something in the
constructor and return an exception if not met...kind of like with security

ie:
Sub New(ByVal check As Boolean)

Throw New Exception("No, cant do it")

End Sub

But i dont seem to be able to throw exceptions from the constructor... am i
missing something? How does the SecurityAction.Demand stuff do it?

thanks...
 
C

cody

Who says that you cannot throw exceptions from the contructor? Do you get an
error?
Could you post some complete code?
 
G

Guest

Sure, good question...

First, I have the Attribute class... in that class I have:

Sub New(ByVal useAuthentication As Boolean)

Throw New SoapException("hellp!!!!!", SoapException.ClientFaultCode)

End Sub

Now from the class that is using the Atrribute, I have..

<WebMethod(), UseAuthenticationManager(True)> _
Public Function Test() As String

' Do nothing, exception should have been thrown from Attribute
constructor!

End Function

If you could let me know whats going on that would be great. The attribute
is being detected, but no matter what I do from the constructor, it doesnt
seem to matter (like throw an exception)

thanks,
 
M

Mattias Sjögren

If you could let me know whats going on that would be great. The attribute
is being detected, but no matter what I do from the constructor, it doesnt
seem to matter (like throw an exception)

When are you expecting the exception to be thrown? It will not happen
at compile time or when the method is called if that's what you
expect.

Why do you want to do this anyway? The design idea seems weird at
best.


Mattias
 
G

Guest

Here is exactly why I want to do this.

I am using wse to secure a web service. While an exception gets thrown back
if the credentials are not met, nothing happens if they are not. Therefore
you have to do some check for the tokens in the beginning of the method you
want to secure. Instead I wanted to secure a method declaratively... just
like CAS works in .NET - what is wierd about that...

for instance:

<UseSecurity(True), WebMethd()>_

Just like CAS works

<SecurityAction.Demand... blah blah>
 
R

Richard Blewett [DevelopMentor]

Unfortunately for you, CAS has an advantage that you do not, it is part of the runtime and therefore the runtime ensures its involvement. Normal custom attributes only come into effect when code that is interested in them executes. Unfortunately you checking code isn't getting executed as custom attribute instances are only created when someone calls GetCustomAttributes.

You will not be able to do what CAS does because your code is not part of the runtime. However, I guess you could derive from ConotextBoundObject and intercept the method call, doing your checking there

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


Here is exactly why I want to do this.

I am using wse to secure a web service. While an exception gets thrown back
if the credentials are not met, nothing happens if they are not. Therefore
you have to do some check for the tokens in the beginning of the method you
want to secure. Instead I wanted to secure a method declaratively... just
like CAS works in .NET - what is wierd about that...

for instance:

<UseSecurity(True), WebMethd()>_

Just like CAS works

<SecurityAction.Demand... blah blah>
 
G

Guest

Richard - thank you for your feedback.

Well thats not very good news. Would anyone know what the recommended
solution to this using wse 2.0 ? I dont want to intercept EVERY method call,
just the methods that require credentials to access them.

Here's my problem with the current solution

<WebMethod()>_
Public Function PleaseBeSecure()
CheckforToken()

' a bunch of super secret code.

End Function

The problem with this is it would be WAY TO EASY to accidentially remove the
CheckforToken(), whereas if it was an attribute, it would stand out more.

Any suggestions?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top