PrincipalPermission attribute in service contract

Is there any technical reason why the principle PrincipalPermission cannot be placed in the service contract interface? It works only with a class that implements a contract, or directly on class methods.

This does not work.

[ServiceContract] public interface IMyService { [PrincipalPermission(SecurityAction.Demand, Role="Admin")] [OperationContract] void MyFunction(string str); } 

But it works if I put the attribute of the mapping method in a class that implements IMyService.

+4
source share
2 answers

The role is checked at runtime using the attributes placed in the implementation method, and not the attributes placed on the interface that implements the method. More generally, in .NET there is no direct relationship between custom attributes placed in a method and custom attributes placed in the corresponding interface method, if any.

+3
source

I know that the message is old, trying to give a concrete answer to the OPs question, the reason lies in the difference between the interface and the class.

Think about it; An interface is a description of an implementation; it is not an implementation behavior. The interface simply describes the methods and events of the class, etc.

Or, as Microsoft says:

The interface defines the signatures for the set of elements that developers must provide. Interfaces cannot provide implementation details ~ (behavior) for members.

The class that inherits the interface provides the behavior of these methods and events. - that is, it implements [behavior ~ functionality] of an interface

So why the interface:

Define an interface if you need to provide a polymorphic hierarchy of value types.

Consider the definition of interfaces to achieve an effect similar to the multiple inheritance effect.

https://msdn.microsoft.com/library/ms229013(v=vs.100).aspx

Hope this helps someone understand.

+1
source

Source: https://habr.com/ru/post/1447783/


All Articles