Create a new world

きっと誰もが新しい世界を作り続けているんだよ。

C#小話 : MEFってスレッドセーフなの?

「MEFって何?」という方は

Managed Extensibility Framework (MEF)

とか

.NET: はじめての MEF - sardineの日記

を見ていただくとして。 早速タイトルの解答です。

Ans. スレッドセーフです。安心して別スレッドから呼び出したりできます。

調べてみたのです。

仕事でコードを書いているときに、「MEFで使っているメソッドって、別スレッドから動かしても元のスレッドに影響しないの?」と聞かれ、分からなかったので調べてみたのです。

答えはこちら。

MEF for Beginner (Part Creation Policy) - part 6 |

に書いてありました。

What is the default instantiation?

the default instantiation model of MEF is the singleton model.

Best practice

the best-practice is to use the Shared (singleton) model. this bring us to design consideration which suggest that you should design your exportable parts as stateless and thread safe, so they won’t be affected by multiple calls (maybe on different threads) upon the same instance.

デフォルトでシングルトンになってる。ステートレスでスレッドセーフ。 複数呼び出しがかかることを前提に設計していたのですね。素晴らしい。 これで、安心してMEFを非同期で使ったりできますね。

それにしても、まさかイスラエルのサイトまで探しにいくとは思わなかったよ。Microsoftのサイトだけどさ。