简体   繁体   中英

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

Hi I am crating a web api with unity dll and when i am integrating this first i have faced

Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

error i have resolved it by adding:

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

in the web config file and after that i run the application and i got the error as below

The type String cannot be constructed. You must configure the container to supply this value.

Then i searched the internet and i have added:

[InjectionConstructor]

to the constructor and it doesnot resolves the issue I am using ApiController my controller code is as below

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

    }
}

Any one please help me thanks in advance.

The type String cannot be constructed. You must configure the container to supply this value.

The error indicates that you are passing a string into the constructor of one or more of your classes (that you have not shown here).

The solution is to configure Unity to inject the string into your class.

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));

Strings and primitive types need to be injected manually because Unity has no idea of knowing which string you intend to inject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM