forked from SolrNet/SolrNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobal.asax.cs
113 lines (100 loc) · 4.62 KB
/
Global.asax.cs
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
#region license
// Copyright (c) 2007-2010 Mauricio Scheffer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using log4net.Config;
using Microsoft.Practices.ServiceLocation;
using SampleSolrApp.Models;
using SampleSolrApp.Models.Binders;
using SolrNet;
using SolrNet.Commands.Parameters;
using SolrNet.Exceptions;
using SolrNet.Impl;
namespace SampleSolrApp {
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : HttpApplication {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new {controller = "Home", action = "Index"} // Parameter defaults
);
}
private static readonly string solrURL = ConfigurationManager.AppSettings["solrUrl"];
protected void Application_Start() {
XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(Server.MapPath("/"), "log4net.config")));
RegisterRoutes(RouteTable.Routes);
var connection = new SolrConnection(solrURL);
var loggingConnection = new LoggingConnection(connection);
Startup.Init<Product>(loggingConnection);
RegisterAllControllers();
ControllerBuilder.Current.SetControllerFactory(new ServiceProviderControllerFactory(Startup.Container));
ModelBinders.Binders[typeof (SearchParameters)] = new SearchParametersBinder();
AddInitialDocuments();
}
/// <summary>
/// Adds some sample documents to Solr
/// </summary>
private void AddInitialDocuments() {
try {
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
solr.Delete(SolrQuery.All);
var connection = ServiceLocator.Current.GetInstance<ISolrConnection>();
foreach (var file in Directory.GetFiles(Server.MapPath("/exampledocs"), "*.xml")) {
connection.Post("/update", File.ReadAllText(file, Encoding.UTF8));
}
solr.Commit();
solr.BuildSpellCheckDictionary();
} catch (SolrConnectionException) {
throw new Exception(string.Format("Couldn't connect to Solr. Please make sure that Solr is running on '{0}' or change the address in your web.config, then restart the application.", solrURL));
}
}
/// <summary>
/// Gets a controller instance with its dependencies injected.
/// Picks the first constructor on the controller.
/// </summary>
/// <param name="container"></param>
/// <param name="t">Controller type</param>
/// <returns></returns>
public IController GetContainerRegistration(IServiceProvider container, Type t) {
var constructor = t.GetConstructors()[0];
var dependencies = constructor.GetParameters().Select(p => container.GetService(p.ParameterType)).ToArray();
return (IController) constructor.Invoke(dependencies);
}
public string GetControllerName(Type t) {
return Regex.Replace(t.Name, "controller$", "", RegexOptions.IgnoreCase);
}
/// <summary>
/// Registers controllers in the DI container
/// </summary>
public void RegisterAllControllers() {
var controllers = typeof (MvcApplication).Assembly.GetTypes().Where(t => typeof (IController).IsAssignableFrom(t));
foreach (var controller in controllers) {
Type controllerType = controller;
Startup.Container.Register(GetControllerName(controller), controller, c => GetContainerRegistration(c, controllerType));
}
}
}
}