Skip to content

Commit 014f307

Browse files
committed
Document obtaining MEF components from projects
In a chat with a partner team today I was looking for a doc to explain how to pull a MEF component from a project, when not a suitably-scoped MEF component yourself. This information is touched on in `finding_CPS_in_a_VS_project.md`, but feels more discoverable in `mef.md`.
1 parent 9b9f28b commit 014f307

1 file changed

Lines changed: 39 additions & 5 deletions

File tree

doc/overview/mef.md

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ it could occasionally be out of sync with extensions. In this situation,
7575
running the following commands inside command-line window will reset the
7676
cache:
7777

78-
devenv /UpdateConfiguration
79-
devenv /ClearCache
78+
```
79+
devenv /UpdateConfiguration
80+
devenv /ClearCache
81+
```
8082

8183
VS MEF provides a detailed error report when it finds errors inside MEF
8284
compositions. It always tries to keep the rest of the components working by
@@ -86,7 +88,9 @@ error report file will help to diagnose issues such as the reason why a
8688
certain component is never being loaded into VS. The error report file
8789
can be found at
8890

89-
[User]\AppData\Local\Microsoft\VisualStudio\[Version]\ComponentModelCache\Microsoft.VisualStudio.Default.err
91+
```
92+
%LOCALAPPDATA%\Microsoft\VisualStudio\[Version]\ComponentModelCache\Microsoft.VisualStudio.Default.err
93+
```
9094

9195
Because a MEF error may cause chains of errors in other components, one
9296
should always start with investigating level 1 composition errors.
@@ -138,10 +142,10 @@ fact that the component carries the correct `AppliesTo` metadata. It is
138142
important to use the correct `AppliesTo` metadata when defining a component.
139143
Normally, the `AppliesTo` metadata is the new project type the component
140144
supports; in the advanced scenario, `AppliesTo` metadata can be an expression
141-
like this --
145+
like this:
142146

143147
```csharp
144-
[AppliesTo("MyLanaguageProject + DeviceProject")]
148+
[AppliesTo("MyLanaguageProject & DeviceProject")]
145149
```
146150

147151
Also, when a component exports additional properties or methods, the
@@ -196,6 +200,36 @@ are provided by CPS and cannot be replaced by extensions. Such services
196200
include the `ConfiguredProject`, `UnconfiguredProject`, `IProjectLockService`,
197201
etc.
198202

203+
### Obtaining MEF components in project scopes
204+
205+
As described in [scopes](scopes.md), MEF project-specific components instances
206+
can exist in the scope of an `UnconfiguredProject` or a `ConfiguredProject`.
207+
208+
If your component is a MEF part and in a compatible scope, then the best way
209+
to obtain an instance is just to import it using a standard MEF import.
210+
211+
However, if your component is not participating in MEF, or is in the global
212+
or `ProjectService` scopes, then you can obtain project-scoped components
213+
via the relevant instance of `UnconfiguredProject` or `ConfiguredProject`.
214+
215+
Note that several key CPS services are available directly on via the project's
216+
`Services` property. However if you need a MEF component that is not already
217+
exposed this way, you can use the project's `ExportProvider` as follows.
218+
219+
For example:
220+
221+
```csharp
222+
UnconfiguredProject unconfiguredProject = ...;
223+
ConfiguredProject configuredProject = ...;
224+
225+
IMyUnconfiguredComponent c1 = unconfiguredProject.Services.ExportProvider.GetExportedValue<IMyUnconfiguredComponent>();
226+
227+
IMyConfiguredComponent c2 = configuredProject.Services.ExportProvider.GetExportedValue<IMyConfiguredComponent>();
228+
```
229+
230+
For details on obtaining an instance of `UnconfiguredProject` or `ConfiguredProject`,
231+
see [Finding CPS in a VS project](..\automation\finding_CPS_in_a_VS_project.md)
232+
199233
## MEF and C# Nullable Reference Types
200234

201235
When using the C# nullable feature in your code, you may need to tweak the above

0 commit comments

Comments
 (0)