Skip to content

Commit 004dece

Browse files
committed
Refactoring code to make it cleaner. Fixing bug to filter out Roslyn assemblies.
1 parent e45d3f8 commit 004dece

8 files changed

+205
-88
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
22
[Bb]in/
33
[Oo]bj/
4+
lib/
5+
*.nupkg
46

57
# mstest test results
68
TestResults
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.Composition;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Common.Logging;
9+
10+
namespace ScriptCs.WebApi
11+
{
12+
[Export(typeof(IControllerTypeManager))]
13+
public class ControllerTypeManager : IControllerTypeManager
14+
{
15+
private readonly ILog _logger;
16+
private const string RoslynAssemblyNameCharacter = "ℛ";
17+
18+
private static readonly string[] IgnoredAssemblyPrefixes = new[]
19+
{
20+
"Autofac,",
21+
"Autofac.",
22+
"Common.Logging",
23+
"log4net,",
24+
"Nancy,",
25+
"Nancy.",
26+
"NuGet.",
27+
"PowerArgs,",
28+
"Roslyn.",
29+
"scriptcs,",
30+
"ScriptCs.",
31+
"ServiceStack.",
32+
};
33+
34+
35+
[ImportingConstructor]
36+
public ControllerTypeManager(ILog logger)
37+
{
38+
_logger = logger;
39+
}
40+
41+
public IEnumerable<Type> GetLoadedTypes()
42+
{
43+
var types = new List<Type>();
44+
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().Where(a => !IgnoredAssemblyPrefixes.Any(p => a.GetName().Name.StartsWith(p))))
45+
{
46+
try
47+
{
48+
types.AddRange(assembly.GetTypes());
49+
_logger.Debug("Scriptcs.WebApi - Loaded assembly " + assembly);
50+
}
51+
catch (ReflectionTypeLoadException ex)
52+
{
53+
_logger.Warn(string.Format("ScriptCs.WebApi - Count not load types from {0}", assembly.FullName));
54+
foreach (var load in ex.LoaderExceptions)
55+
{
56+
_logger.Warn(string.Format("ScriptCs.WebApi - Exception {0}", load));
57+
}
58+
}
59+
catch (Exception ex)
60+
{
61+
_logger.Warn(string.Format("ScriptCs.WebApi - Count not load types from {0}: {1}", assembly.FullName, ex.Message));
62+
}
63+
64+
}
65+
return types;
66+
}
67+
68+
public IEnumerable<Type> GetControllerTypes()
69+
{
70+
var types = GetLoadedTypes();
71+
var tempControllerTypes = ControllerResolver.WhereControllerType(types).ToList();
72+
var controllerTypes = tempControllerTypes.Where(t => t.Assembly.FullName.Substring(0, 1) != RoslynAssemblyNameCharacter).ToList();
73+
var roslynTypes = tempControllerTypes.Where(t => t.Assembly.FullName.StartsWith(RoslynAssemblyNameCharacter));
74+
controllerTypes.Add(roslynTypes.Last());
75+
foreach (var controller in controllerTypes)
76+
{
77+
_logger.Debug(string.Format("ScriptCs.WebApi - Found controller: {0}", controller.FullName));
78+
}
79+
return controllerTypes;
80+
}
81+
82+
public IEnumerable<Type> GetControllerTypes(Assembly[] assemblies)
83+
{
84+
IEnumerable<Assembly> controllerAssemblies =
85+
new List<Assembly>(AppDomain.CurrentDomain.GetAssemblies()).Union(assemblies);
86+
Type[] types = controllerAssemblies.SelectMany(a => a.GetTypes()).ToArray();
87+
List<Type> controllerTypes = ControllerResolver.WhereControllerType(types).ToList();
88+
return controllerTypes;
89+
}
90+
91+
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
5+
namespace ScriptCs.WebApi
6+
{
7+
public interface IControllerTypeManager
8+
{
9+
IEnumerable<Type> GetControllerTypes(Assembly[] assemblies);
10+
IEnumerable<Type> GetControllerTypes();
11+
}
12+
}

src/ScriptCs.WebApi/ScriptCs.WebApi.csproj

+24-13
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@
1717
<DebugSymbols>true</DebugSymbols>
1818
<DebugType>full</DebugType>
1919
<Optimize>false</Optimize>
20-
<OutputPath>bin\Debug\</OutputPath>
20+
<OutputPath>lib\net45\</OutputPath>
2121
<DefineConstants>DEBUG;TRACE</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
24+
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2627
<DebugType>pdbonly</DebugType>
2728
<Optimize>true</Optimize>
28-
<OutputPath>bin\Release\</OutputPath>
29+
<OutputPath>lib\net45\</OutputPath>
2930
<DefineConstants>TRACE</DefineConstants>
3031
<ErrorReport>prompt</ErrorReport>
3132
<WarningLevel>4</WarningLevel>
3233
</PropertyGroup>
3334
<ItemGroup>
35+
<Reference Include="Common.Logging">
36+
<HintPath>..\..\packages\Common.Logging.2.1.2\lib\net40\Common.Logging.dll</HintPath>
37+
</Reference>
3438
<Reference Include="Microsoft.Owin">
3539
<HintPath>..\..\packages\Microsoft.Owin.2.0.0\lib\net45\Microsoft.Owin.dll</HintPath>
3640
<Private>True</Private>
3741
</Reference>
3842
<Reference Include="Newtonsoft.Json">
39-
<HintPath>..\..\packages\Newtonsoft.Json.5.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
40-
<Private>True</Private>
43+
<HintPath>..\..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath>
4144
</Reference>
4245
<Reference Include="Owin">
4346
<HintPath>..\..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
@@ -50,17 +53,18 @@
5053
<Reference Include="System.ComponentModel.Composition" />
5154
<Reference Include="System.Core" />
5255
<Reference Include="System.Net.Http" />
53-
<Reference Include="System.Net.Http.Formatting">
54-
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Client.5.0.0-rc1\lib\net45\System.Net.Http.Formatting.dll</HintPath>
55-
<Private>True</Private>
56+
<Reference Include="System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
57+
<SpecificVersion>False</SpecificVersion>
58+
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Client.5.0.0\lib\net45\System.Net.Http.Formatting.dll</HintPath>
5659
</Reference>
5760
<Reference Include="System.Net.Http.WebRequest" />
58-
<Reference Include="System.Web.Http">
59-
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Core.5.0.0-rc1\lib\net45\System.Web.Http.dll</HintPath>
60-
<Private>True</Private>
61+
<Reference Include="System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
62+
<SpecificVersion>False</SpecificVersion>
63+
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Core.5.0.0\lib\net45\System.Web.Http.dll</HintPath>
6164
</Reference>
62-
<Reference Include="System.Web.Http.SelfHost">
63-
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.SelfHost.5.0.0-rtm-130828\lib\net45\System.Web.Http.SelfHost.dll</HintPath>
65+
<Reference Include="System.Web.Http.SelfHost, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
66+
<SpecificVersion>False</SpecificVersion>
67+
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.SelfHost.5.0.0\lib\net45\System.Web.Http.SelfHost.dll</HintPath>
6468
</Reference>
6569
<Reference Include="System.Xml.Linq" />
6670
<Reference Include="System.Data.DataSetExtensions" />
@@ -70,12 +74,19 @@
7074
</ItemGroup>
7175
<ItemGroup>
7276
<Compile Include="ControllerResolver.cs" />
77+
<Compile Include="IControllerTypeManager.cs" />
7378
<Compile Include="Properties\AssemblyInfo.cs" />
7479
<Compile Include="ScriptPack.cs" />
80+
<Compile Include="ControllerTypeManager.cs" />
7581
<Compile Include="WebApi.cs" />
7682
</ItemGroup>
7783
<ItemGroup>
78-
<None Include="packages.config" />
84+
<None Include="packages.config">
85+
<SubType>Designer</SubType>
86+
</None>
87+
<None Include="ScriptCs.WebApi.nuspec">
88+
<SubType>Designer</SubType>
89+
</None>
7990
</ItemGroup>
8091
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
8192
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
3+
<metadata>
4+
<id>ScriptCs.WebApi</id>
5+
<version>0.5.0</version>
6+
<title>ScriptCs.WebApi</title>
7+
<authors>Glenn Block</authors>
8+
<owners>Glenn Block</owners>
9+
<licenseUrl>https://raw.github.com/scriptcs/scriptcs-webapi/dev/LICENSE.md</licenseUrl>
10+
<projectUrl>https://github.com/scriptcs/scriptcs-webapi</projectUrl>
11+
<iconUrl>http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120</iconUrl>
12+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
13+
<description>Script pack for ASP.NET Web API.
14+
15+
* Use the script pack to easily create a self-host, or configure an exsisting host such as an OWIN host.
16+
* The host is wired up to discover in-memory controllers.
17+
* Includes helper methods for creating formatters and message handlers.</description>
18+
<summary />
19+
<releaseNotes>0.1.0 - Initial version
20+
0.5.0 - Added support for additional hosts and helper methods for formatters and handlers.</releaseNotes>
21+
<copyright>Copyright Glenn Block 2013</copyright>
22+
<tags>scriptcs webapi</tags>
23+
<dependencies>
24+
<dependency id="Common.Logging" version="2.1.2" />
25+
<dependency id="Microsoft.AspNet.WebApi.SelfHost" version="5.0" />
26+
<dependency id="Microsoft.Owin" version="2.0.0"/>
27+
<dependency id="ScriptCs.Contracts" version="0.8.0" />
28+
</dependencies>
29+
</metadata>
30+
<files>
31+
<file src="lib\net45\ScriptCs.WebApi.Pack.dll" target="lib\net45\ScriptCs.WebApi.Pack.dll" />
32+
</files>
33+
</package>

src/ScriptCs.WebApi/ScriptPack.cs

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
using System.Collections.Generic;
2+
using System.ComponentModel.Composition;
23
using System.Linq;
4+
using System.Runtime.InteropServices;
35
using System.Text;
46
using System.Threading.Tasks;
7+
using Common.Logging;
58
using ScriptCs;
69
using ScriptCs.Contracts;
710

811
namespace ScriptCs.WebApi
912
{
1013
public class ScriptPack : IScriptPack
1114
{
15+
private readonly ILog _logger;
16+
private readonly IControllerTypeManager _typeManager;
17+
18+
[ImportingConstructor]
19+
public ScriptPack(ILog logger, IControllerTypeManager typeManager)
20+
{
21+
_logger = logger;
22+
_typeManager = typeManager;
23+
}
24+
1225
IScriptPackContext IScriptPack.GetContext()
1326
{
14-
return new WebApi();
27+
return new WebApi(_logger, _typeManager);
1528
}
1629

1730
void IScriptPack.Initialize(IScriptPackSession session)
1831
{
32+
session.AddReference("System.Net.Http");
1933
var namespaces = new[]
2034
{
2135
"System.Web.Http",
@@ -25,8 +39,6 @@ void IScriptPack.Initialize(IScriptPackSession session)
2539
}.ToList();
2640

2741
namespaces.ForEach(session.ImportNamespace);
28-
29-
session.AddReference(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Net.Http.dll");
3042
}
3143

3244
void IScriptPack.Terminate()

0 commit comments

Comments
 (0)