CA2255: The ModuleInitializer attribute should not be used in libraries

Value
Rule ID CA2255
Category Usage
Fix is breaking or non-breaking Non-breaking

Cause

Applying ModuleInitializerAttribute to a method within a Class Library.

Rule description

Module initializers are intended to be used by application code to ensure an application's components are initialized before the application code begins executing. If library code declares a method with the ModuleInitializerAttribute, it can interfere with application initialization and also lead to limitations in that application's trimming abilities. Library code should therefore not utilize the ModuleInitializerAttribute attribute.

How to fix violations

Instead of using methods marked with ModuleInitializerAttribute, the library should expose methods that can be used to initialize any components within the library and allow the application to invoke the method during application initialization.

When to suppress warnings

It is safe to suppress warnings from this rule if a solution uses a Class Library for code factoring purposes, and the ModuleInitializerAttribute method is not part of a shared or distributed library or package.

See also