2009-08-16 44 views
3

所以,我一直在關注史蒂芬·桑德森的書叫臨ASP.NET MVC框架,和我運行到一個例外:SportsStore:MVC編程問題[城堡WindsorControllerFactory] ​​

No parameterless constructor defined for this object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object. 

Source Error: 

Line 16:    HttpContext.Current.RewritePath(Request.ApplicationPath, false); 
Line 17:    IHttpHandler httpHandler = new MvcHttpHandler(); 
Line 18:    httpHandler.ProcessRequest(HttpContext.Current); 
Line 19:    HttpContext.Current.RewritePath(originalPath, false); 
Line 20:   } 


Source File: C:\Users\Stephen\Documents\Visual Studio 2008\Projects\SportsStore\WebUI\Default.aspx.cs Line: 18 

這裏是我的WindsorControllerFactory代碼:

public class WindsorControllerFactory : DefaultControllerFactory 
{ 
    WindsorContainer container; 

    // The constructor 
    // 1. Sets up a new IoC container 
    // 2. Registers all components specified in web.config 
    // 3. Registers all controller types as components 
    public WindsorControllerFactory() 
    { 
     // Instantiate a container, taking configuration from web.config 
     container = new WindsorContainer(
         new XmlInterpreter(new ConfigResource("castle")) 
        ); 

     // Also register all the controller types as transient 
     var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes() 
           where typeof(IController).IsAssignableFrom(t) 
           select t; 

     foreach (Type t in controllerTypes) 
      container.AddComponentWithLifestyle(t.FullName, t, Castle.Core.LifestyleType.Transient); 
    } 

    // Constructs the controller instance needed to service each request 
    protected override IController GetControllerInstance(Type controllerType) 
    { 
     return (IController)container.Resolve(controllerType); 
    } 
} 

我的Global.asax.cs代碼:

protected void Application_Start() 
    { 
     RegisterRoutes(RouteTable.Routes); 
     ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory()); 
    } 

而且在web.config值:

<configSections> 
    <section name="castle" 
      type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, 
        Castle.Windsor"/> 
</configSections> 
<castle> 
    <properties> 
     <myConnStr>Server=.\SQLEXPRESS;Database=SportsStore;Trusted_Connection=yes;</myConnStr> 
    </properties> 
    <components> 
     <component id="ProdsRepository" 
       service="DomainModel.Abstract.IProductsRepository, DomainModel" 
       type="DomainModel.Concrete.SqlProductsRepository, DomainModel"> 
     <parameters> 
      <connectionString>#{myConnStr}</connectionString> 
     </parameters> 
     </component> 
    </components> 
</castle> 

謝謝大家! -Steve

+0

您知道Windsor具有流暢的專用註冊API,對嗎?您不必使用自定義反射。 – 2010-06-30 21:35:11

回答

0

我只是迴應這個......因爲這是一個老問題(2009)。長話短說,它與Windsor的配置值有關是在錯誤的配置(視圖,而不是基礎web.config)。這解決了手頭的問題。

0

我想說的問題是,你正試圖實例化一個Linq到SQL DataContext而不傳遞連接字符串。

解決辦法是宣佈類似:

<components> 
    <component id="..." service="..." type="..."> 
     <parameters> 
      <connectionString>blah blah blah</connectionString> 
     </parameters> 
    </component> 
</components> 

另一種選擇,在一個我用我的當前項目,是簡單地創建一個在您的DataContext參數的構造函數,將簡單地調用構造函數將連接字符串的默認值聲明爲該類本身的靜態字符串:

public partial class MyDataContext 
{ 
    private static string standardConnectionString = "blah blah blah"; 

    public MyDataContext() 
      : this(standardConnectionString) {} 
} 

它解決了您的問題嗎?

+0

嘿, 調試的經過,看到這個例外是在構造函數中燒成WindsorControllerFactory: 異常詳細信息:System.Configuration.ConfigurationErrorsException:與此域關聯的配置文件中找不到節「城堡」。 很明顯在web.config中,但由於某種原因,它沒有找到它。我認爲這是手頭的問題。由於Application_Start觸發一次,引發該異常,因此刷新它無法加載控制器,並且由於該WindsorControllerFactory是邏輯應該在的位置,所以這就是問題所在。 – StephenPAdams 2009-08-17 00:31:21

+0

Annnnd,我是一個新手! 沒有意識到有一個網站。配置爲視圖文件夾以及應用程序本身。 Castle的所有適當的配置數據都在Views web.config中,而不是Web應用程序的web.config中。雞蛋在我的臉上。我換了,一切正常! – StephenPAdams 2009-08-17 00:37:55

0

用途: 保護覆蓋一個IController GetControllerInstance(System.Web.Routing.RequestContext的RequestContext,類型controllerType)

insted的的: 保護覆蓋一個IController GetControllerInstance(類型controllerType)

0

我這個問題,它發生在你有一個沒有默認構造函數的實體類時。總是創建一個不接受參數的默認構造函數。

1
namespace WebUI 
{ 
    public class WindsorControllerFactory:DefaultControllerFactory 
    { 
     WindsorContainer container; 

     public WindsorControllerFactory() 
     { 
      container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle"))); 

      container.Register(AllTypes 
       .FromThisAssembly() 
       .BasedOn<IController>() 
       .If(Component.IsInSameNamespaceAs<Controllers.ProductsController>()) 
       .If(t => t.Name.EndsWith("Controller")) 
       .Configure(c=>c.LifeStyle.Transient.Named(c.Implementation.Name))); 
     } 

     protected override IController GetControllerInstance(RequestContext ctx, Type controllerType) 
     { 
      if (controllerType == null) { return null; } 
      return (IController)container.Resolve(controllerType);    
     } 
    } 
}