does MEF apply the SOA paradigm?

 

in this post I'm going to argue that the Manage Extensibility Framework (MEF),

is actually applying to the 4 tenet of Service Orientation Architecture (SOA),

therefore MEF is actually, in-process implementation of the SOA paradigm.

 

Background

when we speaking about SOA we used to think about technologies like web services and WCF,

which is cross process technologies.

but does SOA define that services should always be consumed from the clouds?

local services like loggers, rule engine, cache and more are everyday practice for in-process services.

 
What make your architecture, service oriented?

different architect having different opinions of how to construct SOA architecture,

but most of them agree that SOA design should apply to the 4 tenets of SOA.

 

The 4 tenets of SOA

 MEF, Catalog, Extensibility, Import,Export Service should have Explicit Boundaries.

1264406391_Gear  Service should be Autonomous.

1264403639__bedingungen Service API should rely on Contract.

 1264404491_diagram_v2-26 Service behavior and compatibility should rely on Policy.

 

How MEF apply to the 4 tenets?

 MEF, Catalog, Extensibility, Import,Export MEF boundaries define explicitly by the Export definition.

1264406391_Gear  separating exports into separate dlls will create autonomous unit which has no need

for any reference but the reference for the services contract.

1264403639__bedingungen Imports does not aware of the actual implementation, imports are solely relay on the contract

(composition will find export that mach the import contract).

 1264404491_diagram_v2-26 MEF has different policies mechanism like metadata, creation policy,

instantiation policy, recomposition.

 

Summary

despite MEF being an in-process technology it does apply to the 4 tenet of SOA,

therefore it can consider as such.

 

תגים של Technorati:‏ ,,,


CLR 4.0 tip (Strong Name)

1264355035_binary

CLR 4.0 come with some changes of the security API.

one useful new API is a new way of getting evidence without iteration loop.

 

and this is how it look:

Code Snippet
  1. StrongName sn = typeof(Program).Assembly.Evidence.GetHostEvidence<StrongName>();


Posted by bnaya | with no comments
תגים:, , ,

CLR 4.0 transparent security

security CLR 4.0 .NETCLR 4.0 come with some changes to the security model.

in general the CLR 4.0 no longer assume the sandbox for the application.

 

you can still sandboxed your AppDomain and the CLR 4.0 will onerous that sandbox,

but running the same exe from local computer or from network shared folder

won't grant the exe different privileges (sandbox).

 

as result the security setting goes back into the administrators hands,

as it was before the .NET era and application restriction will be define equally

for both manage and unmanaged code.

the security is back again done at the OS level.

 

what you should be aware of the the transparent code concept,

and that security is no longer can be define at the assembly level (all the definition should done at the AppDomain level).

 

Transparent code concept

the analysis of .NET fundamental operation observe that there are only 2 operation which are dangerous:

  • unverifiable code
  • calling unmanaged code (P-Invoke / COM)

potentially harm operations define as security critical, and cannot be invoke directly from security non critical (transparent) code.

if non security critical (transparent) code need access for security critical operation,

it should do so by calling security safe critical operation (which play the role of the man in the middle).

 

this exactly what happens when you need to persist data into file

using in-browser Silverlight application,

the file open define as security critical so you have no direct access to this operation,

and the isolated storage defined as security safe critical, so it give you indirect

access to the security critical file open but only to restricted file paths.

security CLR 4.0 .NET

 
Code snippet

you can get the code snippet from here.

Code Snippet
  1. [assembly: AllowPartiallyTrustedCallers]
  2. [assembly: SecurityRules(SecurityRuleSet.Level2)]
  3. namespace TransparentSecurity
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             var criticalFoo = new CriticalFoo();
  10.             try
  11.             {
  12.                 criticalFoo.DoDangerousOperation(AppDomain.CurrentDomain.BaseDirectory);
  13.             }
  14.             catch (Exception ex) { Console.WriteLine(ex.Message); }
  15.  
  16.             try
  17.             {
  18.                 criticalFoo.DoSafeOperation(AppDomain.CurrentDomain.BaseDirectory);
  19.             }
  20.             catch (Exception ex) { Console.WriteLine(ex.Message); }
  21.        }
  22.     }
  23.  
  24.     public class CriticalFoo
  25.     {
  26.         [SecuritySafeCritical]
  27.         public void DoSafeOperation (string path)
  28.         {
  29.             var prm = new FileIOPermission(FileIOPermissionAccess.AllAccess, path);
  30.             prm.Demand();
  31.             DoDangerousOperation(path);
  32.         }
  33.  
  34.         [SecurityCritical]
  35.         public void DoDangerousOperation (string path)
  36.         {
  37.             Console.WriteLine(path);
  38.         }
  39.     }
  40. }

line 1: by default your code consider security critical unless you decorate it with [AllowPartiallyTrustedCallers] attribute.

line 2: setting the security rules to level 2, is the default CLR 4.0 security model (level 1 is CLR 2 backward compatibility).

line 34: decorate the method as security critical (therefore it can be assessed only by security critical or security safe critical operations).

line 12: this invocation won't succeed because security non critical (transparent) operations cannot access security critical operations.

line 26: decorate the method as security safe critical (so this code can target security critical operation and be target by
             security non critical code).

lines 29,30: restrict the access using demand for file permission to the specific folder.

 
Summary

CLR 4.0 security model is taking one step back from absolute security management,

and one step to the to the side and forward by giving us more simple and understandable security model.

the transparent model dose not restrict malicious code from accessing critical operation,

(because the malicious code can be decorate with security safe critical), what it does is helping the

CLR to put it's security focus on the dangerous code section.

 

Point of interest

you can learn more about the CLR 4.0 security concepts on this video by Shawn Farkas.

 

תגים של Technorati:‏ ,,

 


kick it on DotNetKicks.com


Posted by bnaya | with no comments

MEF for Beginner (repeatable metadata) - part 9

this is the 9th post of the MEF for Beginner series, the series TOC is available here.

this post will focus on having repeatable metadata definition (cases like definition of multiple categories).

if you not familiar with the MEF metadata concept you may want to read part 8. 

MEF extensibility compose import export importmany

Bad practice for repeatable metadata

In order to explain repeatable metadata, we will start by decorating export with

untyped metadata declaration (which consider as bad practice, use typed metadata

whenever you can, it is mach usable and reducing runtime failure).

Code Snippet
  1. [Export(typeof(IPlug))]
  2. // using untyped metadata considered as bad practice
  3. [ExportMetadata("Categories", Category.Travel, IsMultiple = true)]  
  4. [ExportMetadata("Categories", Category.City, IsMultiple = true)]   
  5. public class NewYork : IPlug
  6. {
  7.     public void Write() { Console.WriteLine("New York"); }
  8. }

the above code snippet decorate the plug-in with categories  metadata for both Travel and City (line 3,4).

notice that we should explicitly declare that those decoration is multiple (IsMultiple=true).

line 1: define the export.

latter on this post we will see the best practice of declaring the metadata using custom attribute.

 

Metadata view contract for repeatable metadata definition

the metadata view contract properties type, for repeatable metadata decoration, should be Array, or IEnumerable.

the following metadata view contract is capable to handle the above metadata decoration:

Code Snippet
  1. public interface IMetadataView
  2. {
  3.     Category[] Categories { get; }
  4. }

as you can see, line 3, define property that match the name of our metadata decoration (Categories)

and it is using array for its data type.

 

Best practice for metadata definition

as in the previous part, the best practice for metadata definition is

using  typed metadata, which mean to define attribute for the metadata.

Code Snippet
  1. [MetadataAttribute]
  2. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  3. public class CategoryAttribute : Attribute
  4. {
  5.     public CategoryAttribute(Category category)
  6.     {
  7.         Categories = category;
  8.     }
  9.     public Category Categories { get; private set; }
  10. }

as you can see it is almost identical for the definition of non-repeatable metadata attribute,

but with 2 differences:

  • we should declare that the attribute allow multiple occurrence (at the end of line 2),
    this definition is equivalent to the IsMultiple definition on the untyped metadata.
  • we should derive from Attribute (instead of ExportAttribute, otherwise we will get multiple
    instantiation of our plug-in
    ).

 

the plug-in decoration will look as follow:

Code Snippet
  1. [Export(typeof(IPlug))]
  2. [Category(Category.Game)]
  3. [Category(Category.News)]
  4. public class Football : IPlug
  5. {
  6.     public void Write() { Console.WriteLine("Football"); }
  7. }

in term of code safety typed metadata leaving less margin for errors,

and it is match more usable because it is easier to understand which value are expected.

 

The Import decoration

our import should use the Lazy<IPlug, IMetadataView> in order to get the metadata information

(through the IMetadataView contract ).

Code Snippet
  1. [ImportMany]
  2. public Lazy<IPlug, IMetadataView>[] Plugins { get; private set; }

 

Interrogating the metadata

the following code will compose and interrogate the metadata.

Code Snippet
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         var instance = new Program();
  6.         var cat = new AssemblyCatalog(typeof(Program).Assembly);
  7.         var container = new CompositionContainer(cat);
  8.         container.ComposeParts(instance);
  9.  
  10.         foreach (var plug in instance.Plugins)
  11.         {
  12.             plug.Value.Write();
  13.             foreach (var category in plug.Metadata.Categories)
  14.             {
  15.                 Console.Write(", {0}", category);
  16.             }
  17.         }
  18.     }
  19.  
  20.     [ImportMany]
  21.     public Lazy<IPlug, IMetadataView>[] Plugins { get; private set; }
  22. }

lines 6-8, is handling the composition.

line 10, iterating for each of the discovered plug-in.

line 13, iterating for each of the items in the metadata categories property upon specific plug-in.

 

Summary

having multiple metadata on the same export is slightly more challenging.

the main difference between working with repeatable metadata relate to the metadata decoration (AllowMultiple=true)

and having Array or IEnumerable at the metadata view properties level.

 

Download the code

the full code snippet for the samples covered by this post available in here,

full project download for the post sample and for WPF sample that using this post concept for

dynamic filtering of widgets by categories (as shown bellow), is available here for 2008, and here for 2010.

image 

filtering widgets by categories sample (2010, 2008).

 

Point of interest
  • do not use setter on your metadata view interface.
  • do not use the [InheritedExport] because for some some strange reason it doesn't functioning well along with metadata.
  • if you want to build advance metadata view contract you can use class instead of interface,
    but that class must have constructor with the following signature:
    public ClassName (IDictionary<string, object> metadata)
    inside the constructor you should map the metadata dictionary to the class properties.

 

 


kick it on DotNetKicks.com


MEF extensibility metadata MEF for Beginner (Metadata) - part 8

 

this is the 8th post of the MEF for Beginner series, the series TOC is available here.

 

this post will focus on the basics of MEF metadata capabilities,

the next post discuss the ability of multiple metadata decorations.

 

What is Metadata?

metadata is piece of compile-time information that can be attached to exported part.

  • metadata is adding peripheral information to the part,
    (metadata information may define which operation system supported by the plug-in).
  • metadata can be read before the instantiation of the part.

 

Typed metadata

MEF support both typed and non typed metadata, as is the case in other .net component

the best practice suggest using the typed metadata paradigm.

(our series will focus on the best practice of type metadata).

 

Which steps is needed for working with typed metadata?

the steps is needed is:

  • define the metadata (attribute)
  • decorate the exported part
  • define the metadata view contract
  • add the metadata view contract into the import signature

 

how to define typed metadata

defining metadata is very simple, all its take is to define attribute which

having public properties and is decorated with the [MetadataAttribute]

Code Snippet
  1. [MetadataAttribute]
  2. public class WeatherAttribute : Attribute
  3. {
  4.     public WeatherConditions Conditions { get; set; }
  5.     public int MinWindLimit { get; set; }
  6.     public int MaxWindLimit { get; set; }
  7. }

as you can see it is no different from any other .net attribute definition,

except of the decoration of [MetadataAttribute] in line 1.

 

Decorate the exported part
Code Snippet
  1. [Export(typeof(PluginBase))]
  2. [WeatherAttribute(MinWindLimit=0, MaxWindLimit=15,
  3.     Conditions = WeatherConditions.SnowyDays)]
  4. public class Rollerblading : PluginBase {... }

as you can see in line 2 the exported part is decorated with some metadata.

line 1 is decorating for the export (see the Improving the usability section for

learning how it can be more usable).

 

Define the metadata view contract

metadata view present the portion of the metadata that we want to interrogate,

(this contract can be class of interface and it can reflect all or part of the attribute public fields).

Code Snippet
  1. public interface IWeatherMetadata
  2. {
  3.     WeatherConditions Conditions { get; }
  4.     int MinWindLimit { get; }
  5.     int MaxWindLimit { get; }
  6. }

in the above case the metadata view contract is reflecting all of the public properties.

 

Import signature

the import definition should use Lazy type with 2 generics definitions,

the first definition for the plug-in contract, and the second for the metadata view contract.

Code Snippet
  1. [ImportMany]
  2. private IEnumerable<Lazy<PluginBase, IWeatherMetadata>> AllPlugIns { get; set; }

as you can see in line 2, MEF will create enumerable of lazy plug-ins which having typed weather metadata.

 

Inquiring the metadata

having the above import we may use Linq query (or any other technique) to filter the plug-in

portion that suite our needs (without having to instantiate the unneeded plug-ins).

Code Snippet
  1. public IEnumerable<PluginBase> SunnyActivities
  2. {
  3.     get
  4.     {
  5.         var plugins = from lazyPlugin in AllPlugIns
  6.                       let metadata = lazyPlugin.Metadata // typed metadata (IWeatherMetadata)
  7.                       where metadata.Conditions == WeatherConditions.Sunny
  8.                       select lazyPlugin.Value;
  9.         return plugins;
  10.     }
  11. }

the above sample is using the typed metadata to filter the sunny activities.

 

Behind the scenes

what is happening behind the scenes it that MEF is using reflection emit in order to construct

the typed metadata view.

 

Improving the usability

we can improve the usability of the previous sample by inheriting the ExportAttribute

(instead of Attribute), that way we can have single simple decoration for our export part.

Code Snippet
  1. [MetadataAttribute]
  2. [AttributeUsage (AttributeTargets.Class)]
  3. public class WeatherAttribute:ExportAttribute
  4. {
  5.     public WeatherAttribute(int minWindLimit, int maxWindLimit, WeatherConditions conditions) :
  6.         base(typeof(PluginBase))
  7.     {
  8.         Conditions = conditions;
  9.         MaxWindLimit = maxWindLimit;
  10.         MinWindLimit = minWindLimit;
  11.     }
  12.  
  13.     public WeatherConditions Conditions { get; private set; }
  14.     public int MinWindLimit { get; private set; }
  15.     public int MaxWindLimit { get; private set; }
  16. }

the above attribute define both typed metadata and export for the PluginBase type.

line 3: is inheriting the ExportAttribute.

 

the exported type decoration can be define using single simple attribute decoration.

Code Snippet
  1. [WeatherAttribute(0, 15, WeatherConditions.SnowyDays)]
  2. public class Rollerblading : PluginBase {...}

you are no longer having to use different attribute for export and metadata,

and as in this case the export type definition is handled by the attribute,

which enhance the consumer usability by having less area for errors.

 

Code sample

the code sample is demonstrating scenario were we want to present only plug-ins that suit

the current weather (the sample is using Google API for getting the NY weather).

you can download the sample for VS 2010 from here and for VS 2008 from here.

 

תגים של Technorati:‏ ,,,,,,

 

kick it on DotNetKicks.com


Changes in the CLR 4.0 security model

 

CAS security .net

The CAS (Code Access Security) model of the .NET is changing in CRL 4.0.

in general Microsoft has obsolete many of the CAS operations, simplify the access

to security related information, and omitting the support for having

security restriction at the assembly level (security restriction will solely manage by the AppDomain sandbox).

 

using obsolete APIs result with warning at the compilation and exception at run-time.

in general you should be suspicious of any APIs that getting Evidence parameter (especially at the assembly level).

 

you can read lot more on this topic at the NET Security Blog.

 

Summary

if you are using CAS or considering using it, you should be aware for the changes, otherwise your

code will fail at runtime.

 

תגים של Technorati:‏ ,,,

 

kick it on DotNetKicks.com


Posted by bnaya | 1 comment(s)

MEF for Beginner (Recomposition policy) - part 7

 MEF Extensibility composition

 

 

this is the 7th post of the MEF for Beginner series, the series TOC is available here.


 

the current post will focus on the recomposition policy.

 

What is the recomposition policy?

by default the composition should assign the Import only once

(trying to recompose on already composed import will result with exception).

using the recomposition policy we can define that our Import

is allowing reassignment whenever the container is having changes.

 

What is it good for?

the recomposition can be handy for dynamic data discovery that can

changed during the application lifecycle

(you can put file watcher upon your DirectoryCatalog folder and refresh the catalog

when the change event occurs, this way you can add new plug-in at runtime).

you can also use it to reduce the pressure on your application startup

(by starting with few most common catalog at startup and latter adding the rest).

 

How to define recompose-able Import / ImportMany?

the following snippet demonstrate the decoration that needed in

order to define recompose-able Import / ImportMany:

Code Snippet
  1. [ImportMany(AllowRecomposition = true)]
  2. public string[] Strings { get; set; }
  3.  
  4. [Import(AllowRecomposition=true)]
  5. public string Str { get; set; }

as you can see, all it take is to set the AllowRecomposition to true.

 

Code Sample

the following code sample can be download from here,

it is demonstrating recomposition scenario.

 

Exported types:
Code Snippet
  1. internal class ExpMtd1
  2. {
  3.     [Export]
  4.     public string GetString1 { get { return "GetString 1"; } }
  5.     [Export]
  6.     public string GetString2 { get { return "GetString 2"; } }
  7. }
  8. internal class ExpMtd2
  9. {
  10.     [Export]
  11.     public string GetString3 { get { return "GetString 3"; } }
  12.     [Export]
  13.     public string GetString4 { get { return "GetString 4"; } }
  14. }
  15. internal class ExpMtdRuntime
  16. {
  17.     public ExpMtdRuntime(string s) { GetString = s;  }
  18.     [Export]
  19.     public string GetString { get; private set; }
  20. }

the above code having 3 classes,

each of the 2 first classes ExpMtd1 and ExpMtd2 is simply exporting 2 string.

class ExpMtdRuntime is exporting single string that initialized at the construction time.

looking at the constructor it has no [ImportingConstructor] decoration which mean that

it won't be construct by MEF (we will manually instantiate it at runtime and then we will

introduce the instance to MEF).

 

Import
Code Snippet
  1. [ImportMany(AllowRecomposition = true)]
  2. public string[] Strings { get; set; }

the import is decorated with the AllowRecomposition attribute, which define

that MEF can assign this property upon multiple composition.

 

Continues composition

the following code will demonstrate multiple compositions,

you can think on scenario like startup plug-ins loading, then latter adding less frequency used plug-ins,

or on demand plug-ins, that maybe adding new plug-ins that was initialize from data source

Code Snippet
  1. private static ImpCls s_imp = new ImpCls();
  2. private static string[] s_beforeState;
  3.  
  4. static void Main(string[] args)
  5. {
  6.     var c = new TypeCatalog(typeof(ExpMtd1));
  7.     var catalog = new AggregateCatalog(c);
  8.     var container = new CompositionContainer(catalog);
  9.     container.ComposeParts(s_imp);
  10.     Write(s_imp.Strings); // writing the state before recomposition
  11.  
  12.     container.ExportsChanging += (s, e) => {
  13.             s_beforeState = s_imp.Strings; // snapshot of the value before the recomposition assignment
  14.         };
  15.     container.ExportsChanged += (s, e) =>  {// getting the changes gap
  16.             var gap = s_imp.Strings.Except(s_beforeState);
  17.             s_beforeState = null;
  18.             Write(gap);
  19.         };
  20.     // adding new catalog (will result in recomposition)
  21.     catalog.Catalogs.Add(new TypeCatalog(typeof(ExpMtd2)));
  22.     for (int i = 0; i < 10; i++)
  23.     {   // adding export instance (will result in recomposition)
  24.         container.ComposeParts(new ExpMtdRuntime ("Runtime " + i.ToString()));
  25.     }
  26. }
  27.  
  28. private static void Write(IEnumerable<string> strings)
  29. {
  30.     foreach (var item in strings) { Console.WriteLine(item); }
  31. }

line 1: s_imp is the instance of the class that having the imports.

line 2: this is temporary variable that we use for storing the state of the collection before the recomposition so

we can get the gap of the changes after the recomposition complete.

lines 6-9: standard MEF catalog initialization, at this stage we only adding the import instance (line 9)

and the ExpMtd1 type (line 6), the catalog does not yet includes the ExpMtd2 and ExpMtdRuntime.

lines 12-18: listening to the changing and changed events, so we will able to write the gap of the

newly discovered parts to the console, whenever the container will recompose.

line 21: adding the ExpMtd2 (by adding new TypeCatalog), it will cause recomposition.

line 22-25: continuously adding ExpMtdRuntime instances, it will cause recomposition for each iteration.

 

Summary

The recomposition can be very useful in real-life scenarios and it is quite simple to implement.

 

kick it on DotNetKicks.com

 

תגים של Technorati:‏ ,,,,,,


MEF

MEF for Beginner (Part Creation Policy) - part 6



this is the 6th post of the MEF for Beginner series, the series TOC is available here.

 

this post and a few that will follow will cover some of the policies options available with MEF.

we will start with the instantiation policy.

 

How do we define policy?

we will define policy as instruction that will guide behavioral decisions that related

to the compose-able parts (the imported and exported parts behaviors).

 

What is instantiation policy (part creation policy)?

the instantiation policy, is the instruction for how parts should be instantiate.

there is 2 instantiation options:

  • Shared: is a singleton model, which mean that exported part instantiation will instantiate once
    (not matters how many time this part was requested).
  • Non Shared: will create new instance each time we are asking for exportable part.

 

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.

 

in cases that the singleton model is not suitable for you, it is recommended

to use builder pattern (to separate the exported part from the actual instantiation).

you should remember that using the non shared model is quite costly because

it is using reflection for the actual instantiation (by using the builder pattern you

can gain the same result with less pain).

 

How to define our instantiation model?

out of the box we having the [PartCreationPolicy] attribute which make it fairly simple

to define instantiation policy.

here is code snippet that using the [PartCreationPolicy] attribute, which I will immediately discuss:

Code Snippet
  1. [InheritedExport]
  2. public abstract interface IPerson { ... }
  3.     
  4. [PartCreationPolicy (CreationPolicy.Shared)]
  5. public class Person1 : IPerson { ... }
  6.  
  7. [PartCreationPolicy (CreationPolicy.NonShared)]
  8. public class Person2 : IPerson { ... }
  9.  
  10. [PartCreationPolicy (CreationPolicy.Any)]
  11. public class Person3 : IPerson { ... }

at lines 1,2: we can see the contract definition (the contract does not dictate the creation policy)

the InheritedExport attribute means that any type derived from the contract will be automatically exported.

line 4: is decorating Person1 class to apply to the shared (singleton) model.

so no matter how many times Person1 will be imported it will always be the same instance.

line 7: is decorating Person2 class to apply to the non shared model.

so different instance of Person2 will be assigned for each import.

line 10: at the moment this could be somewhat puzzling but we will shortly clear this out.

in terms of instantiation this is the default and it is equivalent to the shared declaration

(unless the import decoration is explicitly asking for non shared policy).

so you may ask yourself why should they add this option if it equivalent to the shared one?

the answer for this question can be found in the next paragraph.

 

How to restrict our import discovery for specific instantiation model?

the default setting of Import / ImportMany is equivalent to the next snippet:

Code Snippet
  1. // [Import] equals to the below decoration
  2. [Import (RequiredCreationPolicy=CreationPolicy.Any)]
  3. public IPerson PersonDefault { get; private set;}
  4.  
  5. // [ImportMany] equals to the below decoration
  6. [ImportMany (RequiredCreationPolicy=CreationPolicy.Any)]
  7. public IEnumerable<IPerson> PersonsDefault { get; private set;}

CreationPolicy.Any means that our import does not care about the instantiation model

of the parts, so the MEF discovery can assign any parts that match the IPerson contract.

the PersonsDefault property will be assigned with Person1, Person2 and Person3.

 

sometimes we want to be more explicit about what type of instantiation allowed for the import.

for those scenarios we can use the following decorations:

Code Snippet
  1. [ImportMany(RequiredCreationPolicy = CreationPolicy.Shared)]
  2. public IEnumerable<IPerson> PersonsShared { get; private set; }
  3.  
  4. [ImportMany(RequiredCreationPolicy = CreationPolicy.NonShared)]
  5. public IEnumerable<IPerson> PersonsNonShared { get; private set; }

line 1: restrict the MEF assignment of PersonShared property to exported parts that created

under the shared policy, which is means parts that decorated with CreationPolicy.Shared or CreationPolicy.Any.

PersonsShared property will be assigned with Person1 and Person3.

line2: restrict the MEF assignment of PersonNonShared property to exported parts that created

under the non shared policy, which is means parts that decorated with CreationPolicy.NonShared or CreationPolicy.Any

(in this case the part that decorated with the CreationPolicy.Any will not instantiate as a singleton).

PersonsShared property will be assigned with Person2 and Person3.

 

Summary

MEF instantiation is singleton by default, but this policy can be altered.

 

you can find code sample that demonstrate the different creation policies here.

 

תגים של Technorati:‏ ,,,,,,


MEF for beginner is a blog series for developers that want to learn

How To use the MEF (Manage Extensibility Framework) technology.

 

the following post is currently available for this series:

  • Concept
  • How To build your first MEF application
  • Hello Silverlight
  • A-sync Silverlight loading
  • Import 
  • Part Creation policy
  • Recomposition policy 
  • Metadata (part 1) 
  • Metadata (part 2 – repeatable) 
  • more post is about to be written.

     



    MEF, Bnaya, CLR 4 ExtensibilitySecured directory catalog

     

    I was discussing the question of how to secure your MEF application on this post 

    where I was publishing replica of the MEF bits hopping that the Secured Directory Catalog will

    find its way into the MEF core.

     

    but as sad as it is :(, it won't make into the core.

    you can read more about the MEF team consideration here.

     

    anyway Glenn Block has suggest a solution for haw to build the secured catalog

    without rewriting half of the MEF core, so as you might guessed,

    I had created new directory catalog that let you control the assembly loading of your plug-ins.

    the implementation of the Secured Directory Catalog can be found here.

     

    it is given without any warranty, but with full source code.

    actually it is real small implementation which you should find easy to review.

    so enjoy the secured way of the plug-ins injection.



    MEF for Beginner – Part 5 (Import)

    this is the 5th post on MEF for beginner series, you can see the previous post here, here and here.

    this post will focus on the Import aspect.

    we will soon learn about the different signature that can be apply with the Import decoration,

    but before starting with that, we should introduce the Lazy<T> class

    which is a first citizen at the MEF world, and part of the CLR 4 types.

     

    Lazy<T> definition

    Lazy<T> is used in order of delaying the actual instantiation until it actually needed.

    in fact using Lazy<T> the actual instantiation will occur the first time the Value property is accessed or ToString method is called.

    you can use the Lazy<T> when ever you want to delay the actual instantiation of the

    object that were discovered by the MEF composition.

     

    The Import / ImportMany decorations

    MEF is trying to discover parts that apply to Import definition constraint.

    we can think at the Import definition as a where clause of the Linq query

    (the from clause will be define by the Catalog / Export provider which will discuss latter on this series).

    out of the box MEF offer an attribute model for the Import definition.

     

    Decoration options

    out of the box MEF is offering 2 type of Import decorations:

    1. [Import] for single discoverable part.

    2. [ImportMany] for discovering multiple parts. 

    Note: you should use [ImportMany] if there is a possibility that MEF will discover multiple Exported parts

    even if you need only one of those parts (you can latter filter one using Linq query).

    if you ask for single part and MEF discovering multiple it will throw an exception or return null

    (depending on the MEF policy which will discuss in the next post on this series).

     

    Import single instance

    as I said in the previous paragraph, you should not use the [Import] attribute unless

    you really expecting single discovery (like the case of discovering UI shell).

    you apply the [Import] decoration to properties that having a getter and a setter (the setter can be private),

    actually you can decorate a private property too (not in Silverlight).

    Note: you can also decorate fields but it considered as bad practice.

    and you can use the Lazy<T> if you want delay instantiation.

    Import decoration may look like the following code:

    Code Snippet
    1.  
    2. [Import]
    3. public ISay Hellow { get; private set; }
    4.  
    5. [Import]
    6. public Lazy<ISay> HellowLazy { get; private set; }

    in the sample above the Import contract is concluded from the type of the property,

    while in the next sample it is explicitly define.

    note that the Lazy<T> is not part of the contract.

    Code Snippet
    1.  
    2. [Import(typeof(ISay))]
    3. public ISay Hellow { get; private set; }
    4.  
    5. [Import(typeof(ISay))]
    6. public Lazy<ISay> HellowLazy { get; private set; }

    using explicit contract is useful while we want MEF to search for contract of specific derived

    type and not for the type of the property itself.

    in case that the Imported property can have null value you should add the AllowDefault=true

    to the Import decoration ( it will look like the following [Import (AllowDefault=true)] ).

     

    Import multiple values

    Importing multiple values is done by using the [ImportMany] attribute.

    we can assign this attribute to properties that having IEnumerable<T>, Array ([]) and

    any type that derived from ICollection<T>.

    the types within the above sets can be Lazy<T> for having delay instantiation.

    the following code snippet will demonstrate the use of [ImportMany] decoration:

    Code Snippet
    1. [ImportMany]
    2. public ISay[] AsArray { get; private set; }
    3.  
    4. [ImportMany]
    5. public IEnumerable<ISay> AsEnumerable { get; private set; }
    6.  
    7. [ImportMany]
    8. public List<ISay> AsCollection { get; private set; }
    9.  
    10. [ImportMany]
    11. public Lazy<ISay>[] AsLazyArray { get; private set; }
    12.  
    13. [ImportMany]
    14. public IEnumerable<Lazy<ISay>> AsLazyEnumerable { get; private set; }
    15.  
    16. [ImportMany]
    17. public List<Lazy<ISay>> AsLazyCollection { get; private set; }

     

    Construction time import

    the last capability I'm going to discuss in this post,

    is one that I advice against using whenever you can, because it can lead to

    cross composition reference. just for knowing what to avoid from

    I will now introduce you to the [ImportingConstructor].

    the [ImportingConstructor] can be decorate constructor that should be use by

    the MEF composition, so any parameter of this constructor is

    treated as import which should be satisfied.

    the constructor may look as follow:

    Code Snippet
    1. [ImportingConstructor]
    2. public Rumors(ISay hello)
    3. {
    4. }

     

    Summary

    in this post we learn which signature can be apply to the import definitions

    and we present the Lazy<T> type and its role under the MEF world.

    on the next post at this series we will survey different MEF policies that

    can affects the behavior of the MEF composition process.

     

    code snippet (console application) can be download from here.

     

    תגים של Technorati:‏ ,,,,,


    Securing the MEF Directory Catalog

     

    during my MEF lecture, on of the attendant (a fellow from check-point) ask the following question:

    "How can we secure our catalog?"

     

    this is a really good point, because without securing our catalog,

    hackers can exploit our extensibility model for injecting malicious code.

     

    so can we defend our MEF gates?

    the short answer is yes.

    and the long one is yes but…

     

    Secure techniques

    I will mention 3 techniques, each defend our code on different layer:

    • IT layer
    • CAS (code access security) layer
    • Catalog layer (which involving some changes to the Directory Catalog)
      (I'm currently speaking with the MEF team trying to convince them to add the 3rd technique into the MEF core).

    you may use all the 3 techniques together, for getting a multi layer protection.

    IT level protection

    the IT layer protection is very simple and straight forward,

    all you have to do is to restrict the access privilege to the plug-ins directory,

    in order to prevent none authorized  plug-ins deployment.

     

    CAS (code access security) protection

    because MEF is contract based (means that MEF will only discover parts that match specific contract),

    we can defend our contract by using CAS technique like demanding specific strong name (or any other evidence).

    the secured contract may look like:

    Code Snippet
    1. [StrongNameIdentityPermission(SecurityAction.Demand, PublicKey = "00240000048...")]
    2. public interface ILogger
    3. {
    4.     void Write(string message);
    5. }

    as we can see in line 1, by restricting the access to our contract we can prevent
    none authorized code from implementing it.

     

    Adding predicate to the MEF Directory Catalog

    Note: the secured version of the Directory Catalog is not currently in the box,

    but i hope it will get in before the release time.

    if you want to use it now you can download the modified MEF bits from my sky drive here.

    thinking about the security risk I came to conclusion that the

    mitigation should handle just before the assembly loading.

    first it is more efficient and second we cannot handle the security after the instantiation

    because it will be to late (constructor code can be malicious too).

     

    Directory Catalog API modifications

    the only change needed in terms of  the public API is to add new constructor

    to the Directory Catalog, which will get lambda expressions (predicate)

    that will authorize the assembly loading (just before the actual loading).

    the new constructor definition is:

    Code Snippet
    1. public DirectoryCatalog(string path, string searchPattern, Predicate<AssemblyName> isAuthorized)

    all you have to do is give a predicate with AssemblyName parameter so you restrict the

    assembly loading upon the AssemblyName information.

    using it may look like:

    Code Snippet
    1. var catalog = new DirectoryCatalog(path, "*.*",
    2.                     info => info.KeyPair.PublicKey == ...);

     

    Summary

    the MEF release is getting close, but I'm hoping that

    this minor yet very important API change will get into the release.

    so we will have an elegant way to secure our extension points out of the box.

     

    Point of Interest

    the code for the modified MEF code can be download from here.

     

    if you interesting is which modification was needed at the MEF core level

    in order to have this new API, I will describe it in this section.

    actually as you will shortly see, I was changing only a few lines of code.

    the reason that I decided to change the core rather of creating new catalog type was:

    1. it seem like a feature that should ship as part of the MEF core.

    2. because some of the changes was needed on internal methods,
        building new catalog would result with coping half of the MEF core.

     

    Changes made step by step:

    Step 1:

    Code Snippet
    1. private Predicate<AssemblyName> _isAuthorized = (name) => true;
    2.  
    3. public DirectoryCatalog(string path, string searchPattern, Predicate<AssemblyName> isAuthorized)
    4. {
    5.     Requires.NotNullOrEmpty(path, "path");
    6.     if (isAuthorized != null)
    7.         _isAuthorized = isAuthorized;
    8.  
    9.     this.Initialize(path, searchPattern);
    10. }

    the above is the code of the new DirectoryCatalog constructor that include the authorization predicate.

    as you can see in lines 6,7 I'm storing the predicate in private member (line 1)

     

    Step 2:

    the DirectoryCatalog class is having private method that responsible for creating AssemblyCatalog.

    Code Snippet
    1. private AssemblyCatalog CreateAssemblyCatalogGuarded(string assemblyFilePath)
    2. {
    3.     Exception exception = null;
    4.  
    5.     try
    6.     {
    7.         return new AssemblyCatalog(assemblyFilePath, this, _isAuthorized);
    8.     }
    9.     catch (FileNotFoundException ex) {   // Files should always exists but don't blow up here if they don't
    10.         exception = ex;
    11.     }
    12.     catch (FileLoadException ex) {   // File was found but could not be loaded
    13.         exception = ex;
    14.     }
    15.     catch (BadImageFormatException ex) {   // Dlls that contain native code are not loaded, but do not invalidate the Directory
    16.         exception = ex;
    17.     }
    18.     catch (ReflectionTypeLoadException ex) {   // Dlls that have missing Managed dependencies are not loaded, but do not invalidate the Directory
    19.         exception = ex;
    20.     }
    21.     catch (Security.SecurityException ex) {
    22.         exception = ex;
    23.     }
    24.     CompositionTrace.AssemblyLoadFailed(this, assemblyFilePath, exception);
    25.     return null;
    26. }

    I was making 2 changes in this class:

    1. at line 7, I added the authentication predicate to the AssemblyCatalog creation.

    2. at lines 21-23, I added catch for security exception (so none authorized assemblies will be traced while the application will not crash)

     

    Step 3:

    adding internal constructor to the AssemblyCatalog

    Code Snippet
    1. internal AssemblyCatalog(string codeBase, ICompositionElement definitionOrigin,
    2.     Predicate<AssemblyName> isAuthorized)
    3.     : this(LoadAssembly(codeBase, isAuthorized), definitionOrigin)
    4. {
    5. }

    at line 3 you can see that I forward the authentication predicate to the LoadAssembly method.

     

    Step 4:

    the last modification was at the assembly loading level

    Code Snippet
    1. private static Assembly LoadAssembly (string codeBase, Predicate<AssemblyName> isAuthorized)
    2. {
    3.     Requires.NotNullOrEmpty(codeBase, "codeBase");
    4.  
    5.     AssemblyName assemblyName;
    6.  
    7.     try
    8.     {
    9.         assemblyName = AssemblyName.GetAssemblyName(codeBase);
    10.     }
    11.     catch (ArgumentException)
    12.     {
    13.         assemblyName = new AssemblyName();
    14.         assemblyName.CodeBase = codeBase;
    15.     }
    16.  
    17.     if (!isAuthorized(assemblyName))
    18.             throw new SecurityException(assemblyName.Name + " is not authorized");
    19.     return Assembly.Load(assemblyName);            
    20. }

    1. at the method signature I added the authorization predicate

    2. at lines 17,18 we use the authentication predicate to determine whether the assembly

    is authorized (this happens before the actual loading of the assembly)

     

    Conclusion

    with a few minor steps we got match secured catalog,

    once again i hope that i will manage to get into the release bits.



    Code cartoons – MEF 

     
    The super hero agency

    MEF Extensibility SELA Bnaya Eshet



    Why and When slides from my MEF lectureMEF, Extensibility, bnaya eshet

     

    some time we are so enthusiastic about the technology,

    that we forgot to explain the Why and When.

     

    so I thought it could be nice to share those aspects with you.

     

     

     

    when we asking Why to use the MEF technology we can think on the following reasons:

    Simple to understand

    Simple to implement

    • The CLR 4 Extensibility standard

    Contract based model (type safe)

    • Easy deployment (support copy / paste deployment)

    • Zero configuration

    • Ease of Maintenance

    Silverlight support

     

     

    rules of thumb for When should we consider using MEF.

    when ever we are using or considering one of the following,

    we may use MEF instead:

    Reflection

    Factory pattern

    Command pattern

    • Any other creational pattern

    IoC (Inversion of control)



    Posted by bnaya | with no comments

    MEF at the SDP

    today i was lecturing at the SELA SDP about building composite WPF shell application using MEF.

    first I want to thanks all attendant that was there despite of the latency in the schedule.

    second I want to thanks Yaniv Rodenski for his part in the lecture.

    Yaniv was demonstrating real world solution that combine the

    WCF power with the MEF charm :)

     

    as I promised you can find the presentation and the code sample that I was showing

    (and Yaniv was kind enough to let me publish his code as-well) ,

    at the following location:

    http://cid-9bf7c1a515d76a9a.skydrive.live.com/browse.aspx/Code%20Samples/MEF/SDP

     

    if you having question, or any thing else (except junk mail and viruses of course)

    you can reach me at bnayae@sela.co.il

    Digg This
    More Posts Next page »