简体   繁体   English

统一的ASP.NET Web API无法构造字符串类型

[英]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 嗨,我用统一的dll创建一个Web API,当我第一次集成这个API时,我已经遇到了

Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. 无法加载文件或程序集'System.Web.Http,版本= 4.0.0.0,区域性=中性,PublicKeyToken = 31bf3856ad364e35'或其依赖项之一。 The located assembly's manifest definition does not match the assembly reference. 找到的程序集的清单定义与程序集引用不匹配。 (Exception from HRESULT: 0x80131040) (来自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 在Web配置文件中,然后我运行该应用程序,我得到如下错误

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 到构造函数,它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

    }
}

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. 解决方案是将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));

Strings and primitive types need to be injected manually because Unity has no idea of knowing which string you intend to inject. 字符串和原始类型需要手动注入,因为Unity不知道您要注入哪个字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用Unity将依赖项注入ASP.NET Web API控制器 - Cannot Inject Dependencies into ASP.NET Web API Controller using Unity Asp.Net MVC C#其他信息:实体或复杂类型不能在LINQ to Entities查询中构造 - Asp.Net MVC C# Additional information: The entity or complex type cannot be constructed in a LINQ to Entities query Unity:无法构造LogWriter类型 - Unity: The type LogWriter cannot be constructed 使用ASP.NET Web Api进行Unity属性拦截 - Unity Attribute Interception with ASP.NET Web Api 如何在DTO和ASP.NET Web API和Unity中使用接口? - How to use interfaces in DTO with ASP.NET Web API and Unity? 将 Web API 播放器保持在登录状态并在 Unity 中更改场景。 (ASP.NET Web API - 统一) - Keep Web API Player on Login and change Scene in Unity. (ASP.NET Web API - Unity) Asp.Net Web Api - Passing String[] values from swagger to Asp.Net Web API - Asp.Net Web Api - Passing String[] values from swagger to Asp.Net Web API 在正文中使用 int/string(简单类型)发布到 asp.net core web api 2.1 不起作用 - Post with int/string (simple type) in body to asp.net core web api 2.1 not working 如何将jquery中的string []类型参数传递给asp.net Web API方法? - How to pass string[] type parameters from jquery to asp.net web api method? ASP.NET Core Web API - 无法为服务类型实例化实现类型“System.Net.Http.IHttpClientFactory” - ASP.NET Core Web API - Cannot instantiate implementation type 'System.Net.Http.IHttpClientFactory' for service type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM