When you want to make a large application thread-safe with PostSharp threading models, it can become difficult to remember to assign a threading model to every single class. In this situation, you can add the thread-safety policy to your project or solution.
The thread-safety policy emits warnings in two situations:
classes that are not assigned to a threading model,
static fields that are not read-only or not of a thread-safe type.
Important
The thread-safety policy does not make your application thread-safe by itself. What the thread-safety policy does is to remind you to use threading models in your code. It is the use of threading models that makes your application thread-safe.
Adding the thread-safety policy using Visual Studio Tools for Metalama and PostSharp.
To apply the thread-safety policy to your application with Visual Studio Tools for Metalama and PostSharp:
Right click on your solution or your project in Solution Explorer, select Add followed by PostSharp Policy...
In the Add PostSharp policy wizard, expand Threading and select Thread Safety.
If you clicked on the solution, select the projects that you would like to add the policy to.
Review the configuration that you have selected and click Next.
Close the wizard when the process had completed by clicking Finish.
If you added the policy to the whole solution, the result of running this wizard is that a pssln file has been added to your project. The pssln file contains an entry that enables the thread-safety policy across all projects in your solution.
<Project xmlns="http://schemas.postsharp.org/1.0/configuration" xmlns:t="clr-namespace:PostSharp.Patterns.Threading;assembly:PostSharp.Patterns.Threading">
<Multicast>
<t:ThreadSafetyPolicy />
</Multicast>
</Project>
Adding the thread-safety policy to a project manually.
To add the thread-safety policy to a project manually:
Add the PostSharp.Patterns.Threading NuGet package to the project.
Add the ThreadSafetyPolicy any C# file. We recommend you add it to a new file named GlobalAspects.cs.
using PostSharp.Patterns.Threading; [assembly: ThreadSafetyPolicy]
Adding the thread-safety policy to a whole solution manually.
To manually add the thread-safety policy to a whole solution:
Open the solution's pssln file. This can be found under the Solution Items folder in Visual Studio's Solution Explorer.
If the pssln file doesn't exist manually add the file at the solution level. Name the file with the same name as your solution and the pssln file extension.
If you had to create the pssln file and add it to your solution add the following XML to it. If the pssln file already existed in your project proceed to the next step.
<?xml version="1.0" encoding="utf-8"?> <Project xmlns="http://schemas.postsharp.org/1.0/configuration" xmlns:t="clr-namespace:PostSharp.Patterns.Threading;assembly:PostSharp.Patterns.Threading"> </Project>
Add a multicast attribute to the Project element that will add ThreadSafetyPolicy to all the projects in the solution.
<?xml version="1.0" encoding="utf-8"?> <Project xmlns="http://schemas.postsharp.org/1.0/configuration" xmlns:t="clr-namespace:PostSharp.Patterns.Threading;assembly:PostSharp.Patterns.Threading"> <Multicast> <t:ThreadSafetyPolicy /> </Multicast> </Project>
Add the PostSharp.Patterns.Threading NuGet package to all projects in the solution.
Once you save the pssln file you will have added thread-safety policy to all projects in your solution.
See Also
Reference