C# 无法构造类型字符串为unity的Asp.net web api
大家好,我正在用unity dll编写一个web api,当我集成这个api时,我首先遇到了一个问题 无法加载文件或程序集“System.Web.Http,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。定位的程序集清单定义与程序集引用不匹配。(来自HRESULT的异常:0x8013100) 错误我已通过添加以下内容解决此问题:C# 无法构造类型字符串为unity的Asp.net web api,c#,asp.net-web-api,dependency-injection,inversion-of-control,unity-container,C#,Asp.net Web Api,Dependency Injection,Inversion Of Control,Unity Container,大家好,我正在用unity dll编写一个web api,当我集成这个api时,我首先遇到了一个问题 无法加载文件或程序集“System.Web.Http,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。定位的程序集清单定义与程序集引用不匹配。(来自HRESULT的异常:0x8013100) 错误我已通过添加以下内容解决此问题: <dependentAssembly> <as
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
但它并不能解决这个问题
我正在使用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
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用System.Web.Http;
使用系统配置;
使用Microsoft.Practices.Unity;
命名空间检查.Api.Controllers
{
公共类CommonController:ApicController
{
#区域变量
///
///业务层。
///
私人IBLPIP_blPIP;
私人公用(blCommon);;
#端区
#区域构造函数
///
///登录API控制器的构造函数
///
///
[注入构造函数]
公共公共控制器(IBLComon blCommon)
{
这个._blCommon=blCommon;
}
#端区
#区域方法
[HttpGet]
公共HttpResponseMessage Get_CountryList()
{
尝试
{
List CountryList=\u blCommon.GetCountryList();
return Request.CreateResponse(HttpStatusCode.OK,CountryList);
}
捕获(例外情况除外)
{
LogUtil.Error(“LoginServiceController\\Login\n”+ex.Message);
返回请求.CreateResponse(HttpStatusCode.NotFound);
}
}
#端区
}
}
任何人请帮助我提前谢谢
无法构造类型字符串。必须配置容器以提供此值
该错误表示您正在将字符串传递到一个或多个类的构造函数中(此处未显示)
解决方案是将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));
公共类SomeType:ISomeType
{
私有只读字符串someString;
公共SomeType(字符串someString)
{
this.someString=someString;
}
}
string someString=“这是一个字符串值”;
container.RegisterType(
“someString”,新的注入构造函数(someString));
字符串和基元类型需要手动注入,因为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));