annotate Implab.ServiceHost/Unity/UnityContainerExtensions.cs @ 279:8714471e8d78 v3

Container configuration cleanup, RC2
author cin
date Fri, 04 May 2018 18:12:42 +0300
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
1 using System.IO;
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
2 using Unity;
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
3
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
4 namespace Implab.ServiceHost.Unity
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
5 {
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
6 public static class UnityContainerExtensions
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
7 {
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
8 public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, string file, ContainerConfigurationSchema schema) {
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
9 Safe.ArgumentNotNull(container, nameof(container));
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
10 var builder = new ContainerBuilder(container,schema);
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
11 builder.LoadConfig(file);
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
12 return builder.Container;
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
13 }
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
14
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
15 public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, Stream stream, ContainerConfigurationSchema schema) {
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
16 Safe.ArgumentNotNull(container, nameof(container));
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
17 Safe.ArgumentNotNull(stream, nameof(stream));
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
18
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
19 if (schema == null)
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
20 schema = ContainerConfigurationSchema.Default;
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
21
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
22 var builder = new ContainerBuilder(container,schema);
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
23 var config = (ContainerElement)schema.Serializer.Deserialize(stream);
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
24 if (config.Items != null) {
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
25 foreach(var item in config.Items)
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
26 item?.Visit(builder);
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
27 }
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
28
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
29 return builder.Container;
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
30 }
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
31
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
32 public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, Stream stream) {
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
33 return LoadXmlConfiguration(container, stream, ContainerConfigurationSchema.Default);
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
34 }
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
35
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
36 public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, string file) {
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
37 return LoadXmlConfiguration(container, file, ContainerConfigurationSchema.Default);
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
38 }
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
39 }
8714471e8d78 Container configuration cleanup, RC2
cin
parents:
diff changeset
40 }