<table class="text">
<tr class="li1"><td class="ln"><pre class="de1">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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 文章,还未来得及翻译,见谅。 Background There are few things we need to address to make our application modularized: How can MVC know about our controllers when they are in other class libraries,in other folder and not being referenced by the host How can the ViewEngine pick up the right location for the Views in modules How to register services used in modules How to serve static file: js,css,image for modules How to register domain entities in modules to DbContext Using the code Below is general folder structure I have come up with [1]: /img/bVInQD The Modular.WebHost is the ASP.NET Core project and it will act as the host. It will bootstrap the app and load all the modules it found in the Modules folder. Each module contains all the stuff for itself to run including Controllers,Services,Views and event static files. For easy development,in the visual studio solution I create a "Modules" solution items and add module projects in Modular.WebHost/Modules physical folder. In order to prevent Modular.WebHost to compile stuff in Modules folder,we need to exclude them in the project.json. 1. First we will scan all the assemblies in each module and load them up ``` var moduleRootFolder = new DirectoryInfo(Path.Combine(_hostingEnvironment.ContentRootPath,"Modules")); var moduleFolders = moduleRootFolder.GetDirectories(); foreach (var moduleFolder in moduleFolders) { var binFolder = new DirectoryInfo(Path.Combine(moduleFolder.FullName,"bin")); if (!binFolder.Exists) { continue; } foreach (var file in binFolder.GetFileSystemInfos("*.dll",SearchOption.AllDirectories)) { Assembly assembly = null; try { assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName); } catch (FileLoadException ex) { if (ex.Message == "Assembly with same name is already loaded") { // Get loaded assembly assembly = Assembly.Load(new AssemblyName(Path.GetFileNameWithoutExtension(file.Name))); } else { throw; } } if (assembly.FullName.Contains(moduleFolder.Name)) { modules.Add(new ModuleInfo { Name = moduleFolder.Name,Assembly = assembly,Path = moduleFolder.FullName }); } } } ``` Then module assemblies will be added to MVC by ApplicationPart ``` var mvcBuilder = services.AddMvc(); foreach (var module in modules) { // Register controller from modules mvcBuilder.AddApplicationPart(module.Assembly); } ``` 2. For the view,a custom ModuleViewLocationExpander is used to help the view engine lookup up the right module folder the views ``` services.Configure