What is HostProtectionAttribute and why do we use it?

617 views Asked by At

I would like to learn more about the HostProtectionAttribute in C#. Certainly, I read the MSDN documentations this and this but I couldn't figure out why we use it. In the documentation there are terms like "host programming model" which made the topic even more confusing for me.

Suppose that we have a method that has this attribute and what happens if I delete it?

1

There are 1 answers

0
Chris N. Tyler On

As the documentation spells out below;

Given these attributes, SQL Server specifies a list of HPAs that are disallowed in the hosted environment through code access security (CAS). The CAS requirements are specified by one of three SQL Server permission sets: SAFE, EXTERNAL_ACCESS, or UNSAFE. One of these three security levels is specified when the assembly is registered on the server, using the CREATE ASSEMBLY statement. Code executing within the SAFE or EXTERNAL_ACCESS permission sets must avoid certain types or members that have the System.Security.Permissions.HostProtectionAttribute attribute applied.

The HostProtectionAttribute is not a security permission as much , in that **it identifies specific code constructs, either types or methods, that the host may disallow

I couldn't figure out why we use it.

We use it to help us write predictable code in the host environment because

it identifies specific code constructs, either types or methods, that the host may disallow

Suppose that we have a method that has this attribute and what happens if I delete it?

When you delete it, in case the method contains types or certain code constructs that maybe disallowed by the host, you will never know and as a result it may result in unpredictable behavior of your code in the host environment such as SQL server. I hope that makes it more helpful.