This repository was archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceManager.cs
More file actions
164 lines (142 loc) · 4.46 KB
/
ServiceManager.cs
File metadata and controls
164 lines (142 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using SimpleStack.Logging;
using SimpleStack.Serializers;
using System.Reflection;
using System.Collections.Generic;
using SimpleStack.Interfaces;
using Funq;
using SimpleStack.Extensions;
namespace SimpleStack
{
public class ServiceManager
: IDisposable
{
private static readonly ILog Log = Logger.CreateLog ();
public Container Container { get; private set; }
public ServiceController ServiceController { get; private set; }
public ServiceMetadata Metadata { get; internal set; }
//public ServiceOperations ServiceOperations { get; set; }
//public ServiceOperations AllServiceOperations { get; set; }
public ServiceManager(params Assembly[] assembliesWithServices)
{
if (assembliesWithServices == null || assembliesWithServices.Length == 0)
throw new ArgumentException(
"No Assemblies provided in your AppHost's base constructor.\n"
+ "To register your services, please provide the assemblies where your web services are defined.");
this.Container = new Container { DefaultOwner = Owner.External };
this.Metadata = new ServiceMetadata();
this.ServiceController = new ServiceController(() => GetAssemblyTypes(assembliesWithServices), this.Metadata);
}
public ServiceManager(bool autoInitialize, params Assembly[] assembliesWithServices)
: this(assembliesWithServices)
{
if (autoInitialize)
{
this.Init();
}
}
public ServiceManager(Container container, params Assembly[] assembliesWithServices)
: this(assembliesWithServices)
{
this.Container = container ?? new Container();
}
/// <summary>
/// Inject alternative container and strategy for resolving Service Types
/// </summary>
public ServiceManager(Container container, ServiceController serviceController)
{
if (serviceController == null)
throw new ArgumentNullException("serviceController");
this.Container = container ?? new Container();
this.Metadata = serviceController.Metadata; //always share the same metadata
this.ServiceController = serviceController;
}
private List<Type> GetAssemblyTypes(Assembly[] assembliesWithServices)
{
var results = new List<Type>();
string assemblyName = null;
string typeName = null;
try
{
foreach (var assembly in assembliesWithServices)
{
assemblyName = assembly.FullName;
foreach (var type in assembly.GetTypes())
{
typeName = type.Name;
results.Add(type);
}
}
return results;
}
catch (Exception ex)
{
var msg = string.Format("Failed loading types, last assembly '{0}', type: '{1}'", assemblyName, typeName);
Log.Error(msg, ex);
throw new Exception(msg, ex);
}
}
private ContainerResolveCache typeFactory;
public void Init()
{
typeFactory = new ContainerResolveCache(this.Container);
this.ServiceController.Register(typeFactory);
this.Container.RegisterAutoWiredTypes(this.Metadata.ServiceTypes);
}
[Obsolete("Old API")]
public void RegisterService<T>()
{
// if (!typeof(T).IsGenericType
// || typeof(T).GetGenericTypeDefinition() != typeof(IService<>))
// throw new ArgumentException("Type {0} is not a Web Service that inherits IService<>".Fmt(typeof(T).FullName));
//
// this.ServiceController.RegisterGService(typeFactory, typeof(T));
// this.Container.RegisterAutoWired<T>();
}
[Obsolete("Old API")]
public Type RegisterService(Type serviceType)
{
return null;
// var genericServiceType = serviceType.GetTypeWithGenericTypeDefinitionOf(typeof(IService<>));
// try
// {
// if (genericServiceType != null)
// {
// this.ServiceController.RegisterGService(typeFactory, serviceType);
// this.Container.RegisterAutoWiredType(serviceType);
// return genericServiceType;
// }
//
// var isNService = typeof(IService).IsAssignableFrom(serviceType);
// if (isNService)
// {
// this.ServiceController.RegisterNService(typeFactory, serviceType);
// this.Container.RegisterAutoWiredType(serviceType);
// return null;
// }
//
// throw new ArgumentException("Type {0} is not a Web Service that inherits IService<> or IService".Fmt(serviceType.FullName));
// }
// catch (Exception ex)
// {
// Log.Error(ex);
// return genericServiceType;
// }
}
public object Execute(object dto)
{
return this.ServiceController.Execute(dto, null);
}
public void Dispose()
{
if (this.Container != null)
{
this.Container.Dispose();
}
}
public void AfterInit()
{
this.ServiceController.AfterInit();
}
}
}