-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAppDomainProxy.cs
More file actions
77 lines (63 loc) · 2.45 KB
/
Copy pathAppDomainProxy.cs
File metadata and controls
77 lines (63 loc) · 2.45 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
using System;
using System.Reflection;
using OperationMessaging;
namespace AppDomainMessaging
{
//***** http://stackoverflow.com/questions/17225276/create-custom-appdomain-and-add-assemblies-to-it
public class AppDomainProxy : MarshalByRefObject
{
private string _path;
private Assembly _serviceAssembly;
private Type _serviceType;
public void Load(string path)
{
ValidatePath(path);
_path = path;
_serviceAssembly = Assembly.Load(_path);
}
public void LoadFrom(string path)
{
ValidatePath(path);
_path = path;
_serviceAssembly = Assembly.LoadFrom(_path);
}
private void ValidatePath(string path)
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (!System.IO.File.Exists(path))
throw new ArgumentException($"path \"{path}\" does not exist");
}
private static bool DerivesFromClass(Type currentType, string baseTypeName)
{
//*****
if (currentType == null) throw new ArgumentNullException(nameof(currentType));
if (string.IsNullOrWhiteSpace(baseTypeName)) throw new ArgumentNullException(nameof(baseTypeName));
//*****
var type = currentType;
while (type != null && type.BaseType != typeof(object))
{
if (type.BaseType != null && type.BaseType.FullName == baseTypeName )
return true;
type = type.BaseType;
}
//*****
return false;
}
public OperationResponse Execute(OperationRequest request)
{
var types = _serviceAssembly.GetTypes();
foreach (var type in types)
if (DerivesFromClass(type, "OperationMessaging.OperationService"))
{
_serviceType = type;
break;
}
//*****
if (_serviceType == null)
return new OperationResponse {Succes = false, NonSuccessMessage = "No type", Result = "No type" };
//*****
var service = (IOperationService) _serviceAssembly.CreateInstance(_serviceType.FullName);
return service == null ? new OperationResponse { Succes = false, NonSuccessMessage = "No type", Result = "No type" } : service.Execute(request);
}
}
}