Post­Sharp Documentation / Multithreading / Writing Thread-Safe Code with Threading Models / Making a Whole Project or Solution Thread Safe

Making a Whole Project or Solution Thread Safe

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 note 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.

This topic contains the following sections:

Adding the thread-safety policy using the UI.

To apply the thread-safety policy to your application with the UI:

  1. Right click on your solution or your project in Solution Explorer, select Add followed by PostSharp Policy...

    deadlockdetection 2
  2. In the Add PostSharp policy wizard, expand Threading and select Thread Safety.

  3. If you clicked on the solution, select the projects that you would like to add the policy to.

  4. Review the configuration that you have selected and click Next.

  5. 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 deadlock detection 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:

  1. Add the PostSharp.Patterns.Threading NuGet package to the project.

  2. Add the ThreadSafetyPolicy any C# file. We recommend you add it to a new file named GlobalAspects.cs.

    C#
    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:

  1. Open the solution's pssln file. This can be found under the Solution Items folder in Visual Studio's Solution Explorer.

    deadlockdetection 7

    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.

  2. 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>
  3. 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>
  4. 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