簡體   English   中英

統一的ASP.NET Web API無法構造字符串類型

[英]Asp.net web api with unity The type String cannot be constructed

嗨,我用統一的dll創建一個Web API,當我第一次集成這個API時,我已經遇到了

無法加載文件或程序集'System.Web.Http,版本= 4.0.0.0,區域性=中性,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配置文件中,然后我運行該應用程序,我得到如下錯誤

無法構造字符串類型。 您必須配置容器以提供該值。

然后我搜索了互聯網,並添加了:

[InjectionConstructor]

到構造函數,它ApiController問題,我正在使用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

    }
}

任何人請先幫助我,謝謝。

無法構造字符串類型。 您必須配置容器以提供該值。

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

解決方案是將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));

字符串和原始類型需要手動注入,因為Unity不知道您要注入哪個字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM