spring.net整合mvc5+ef遇到的巨坑

配置spring.net+mvc花了將近一天的時間,現在把之前遇到的問題列下來。

首先安裝在程序包控制器安裝:

install-package spring.core
 install-package spring.web 
install-package spring.web.mvc5 
install-package spring.web.extensions

添加配置文件:controllers.xml和services.xml

Global.asax裏修改繼承類:Spring.Web.Mvc.SpringMvcApplication

web.config配置:

  <!--spring.net+mvc配置-->
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc5" />
      <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <!--EF配置-->
  <connectionStrings>
    <add name="DataModelContainer" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=aotoBox; MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="aotoBoxEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot; MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <!--spring.net的容器的配置節點-->
  <spring>
    <context>
      <resource uri="file://~/Config/services.xml" />
      <!--有順序的-->
      <resource uri="file://~/Config/controllers.xml" />
    </context>
  </spring>

controller裏添加:

public UserInfoService userInfoService { get; set; }

到這裏一般就完成了,但一個個坑出現了

其中兩個:未能加載文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一個依賴項。找到的程序集清單定義與程序集引用不匹配。 (異常來自 HRESULT:0x80131040)

未能加載文件或程序集“System.Web.Http.WebHost, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一個依賴項。找到的程序集清單定義與程序集引用不匹配。 (異常來自 HRESULT:0x80131040)

解決方法:引用System.Web.Http,System.Web.Http.WebHost兩個DLL即可,沒有的 話

Install-Package Microsoft.aspNet.WebApi這樣也可以得到

2.Spring.Core.InvalidPropertyException: 'UserInfoService' node cannot be resolved for the specified context  ...

這個搞了一上午,最終。。。

 <object name="controllers" type="Solution.OA.UI.Portal2.Controllers.UserInfoController, Solution.OA.UI.Portal2" singleton="false" ><!--singleton代表是否單例模式,默認是TRUE-->
    <!--<property name="Message" value="Welcome to ASP.NET MVC3 powered by Spring.NET!" />-->
    <property name="userInfoService" ref="userInfoService1" />
  </object>

controllers配置文件裏的name=userInfoService需與controller的屬性一致

 

EF學習網站:https://www.e-learn.cn/content/net/788934

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章