1

嗨,我裝箱與團結DLL一個網頁API,當我整合這首先我必須面對與團結Asp.net網頁API String類型不能構建

無法加載文件或程序集「系統.Web.Http,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'或其依賴項之一。定位的程序集清單定義與程序集引用不匹配。 (異常來自HRESULT:0x80131040)

的錯誤,我已經通過添加解決它:

<dependentAssembly> 
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> 
</dependentAssembly> 

在Web配置文件之後,我運行應用程序並 我得到了如下錯誤

無法構造String類型。您必須配置容器以提供此值。

然後我在網上搜索和我說:

[InjectionConstructor] 

的構造,它不列入解決問題 我使用ApiController我的控制器代碼如下

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using System.Configuration; 
using Microsoft.Practices.Unity; 


namespace Check.Api.Controllers 
{ 
    public class CommonController : ApiController 
    { 

     #region Variables 
     /// <summary> 
     /// Business layer of . 
     /// </summary> 
     private IBLPIP _blPIP; 
     private IBLCommon _blCommon; 
     #endregion 

     #region Constructor 
     /// <summary> 
     /// Constructor of Login API Controller 
     /// </summary> 
     /// <param name="auth"></param> 
     [InjectionConstructor] 
     public CommonController(IBLCommon blCommon) 
     { 
      this._blCommon = blCommon; 
     } 
     #endregion 

     #region Methods 

     [HttpGet] 
     public HttpResponseMessage Get_CountryList() 
     { 
      try 
      { 
       List<Country> CountryList = _blCommon.GetCountryList(); 
       return Request.CreateResponse<List<Country>>(HttpStatusCode.OK, CountryList); 
      } 
      catch (Exception ex) 
      { 
       LogUtil.Error("LoginServiceController\\Login\n" + ex.Message); 
       return Request.CreateResponse(HttpStatusCode.NotFound); 
      } 
     } 

     #endregion 

    } 
} 

任何人請提前幫助我。

回答

2

無法構造String類型。您必須配置容器以提供此值。

該錯誤表明您正在將字符串傳遞到一個或多個類的構造函數中(您未在此處顯示)。

解決方案是配置Unity以將字符串注入您的類。

public class SomeType : ISomeType 
{ 
    private readonly string someString; 
    public SomeType(string someString) 
    { 
     this.someString = someString; 
    } 
} 

string someString = "This is a string value"; 
container.RegisterType<ISomeType, SomeType>(
    "someString", new InjectionConstructor(someString)); 

字符串和原始類型需要進行手動注入,因爲團結沒有辦法知道你打算注入其中串的想法。