HttpClient 在 Web 窗体页面中注入 Unity 错误 - 类型或命名空间名称 'HttpClient' 在命名空间 'System.Net.Http' 中不存在
HttpClient injected with Unity error in Web Forms page - Type or namespace name 'HttpClient' does not exist in the namespace 'System.Net.Http'
我正在维护一个旧的 ASP.NET 4.8 Web 窗体应用程序并尝试使用 Unity 容器将 HttpClient
注入 Page
。我的 Global.asax.cs 页面看起来是这样的:
var container = this.AddUnity();
container.RegisterType<HttpClient>(new InjectionFactory(x => new HttpClient()));
(我也尝试过将其注册为 Singleton 以及其他几种注册方式,但这似乎不是问题所在。)
在我页面后面的代码中,我有以下代码:
public partial class page : Page
{
[Dependency]
private HttpClient _httpClient { get; }
public page() { }
public page(HttpClient client)
{
_httpClient = client;
}
}
我的依赖注入在其他地方运行良好,所以我认为这不是 Unity 的错。我在这个页面的其他地方也有 HttpClient
,所以我使用 System.Net.Http;它肯定已安装。事实上,当我将 HttpClient 用作静态对象(未注入页面)时,它工作正常——所以我确定 System.Net.Http 已作为参考安装并且工作正常。
问题是,当我 运行 页面时,出现以下错误:
The type or namespace name 'HttpClient' does not exist in the
namespace 'System.Net.Http' (are you missing an assembly reference?)
[System.Diagnostics.DebuggerNonUserCodeAttribute()] public
shared_page_aspx(System.Net.Http.HttpClient client) :
base(client) {
this.@__Init();
为什么?这个奇怪的代码是从哪里来的?
改为单例:
container.RegisterSingleton<HttpClient>(new InjectionFactory(x => new HttpClient()));
并将此添加到 web.config:
<system.web>
<compilation debug="true" targetFramework="4.8">
<assemblies>
<add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/>
</assemblies>
</compilation>
</system.web>
我正在维护一个旧的 ASP.NET 4.8 Web 窗体应用程序并尝试使用 Unity 容器将 HttpClient
注入 Page
。我的 Global.asax.cs 页面看起来是这样的:
var container = this.AddUnity();
container.RegisterType<HttpClient>(new InjectionFactory(x => new HttpClient()));
(我也尝试过将其注册为 Singleton 以及其他几种注册方式,但这似乎不是问题所在。)
在我页面后面的代码中,我有以下代码:
public partial class page : Page
{
[Dependency]
private HttpClient _httpClient { get; }
public page() { }
public page(HttpClient client)
{
_httpClient = client;
}
}
我的依赖注入在其他地方运行良好,所以我认为这不是 Unity 的错。我在这个页面的其他地方也有 HttpClient
,所以我使用 System.Net.Http;它肯定已安装。事实上,当我将 HttpClient 用作静态对象(未注入页面)时,它工作正常——所以我确定 System.Net.Http 已作为参考安装并且工作正常。
问题是,当我 运行 页面时,出现以下错误:
The type or namespace name 'HttpClient' does not exist in the namespace 'System.Net.Http' (are you missing an assembly reference?) [System.Diagnostics.DebuggerNonUserCodeAttribute()] public shared_page_aspx(System.Net.Http.HttpClient client) : base(client) { this.@__Init();
为什么?这个奇怪的代码是从哪里来的?
改为单例:
container.RegisterSingleton<HttpClient>(new InjectionFactory(x => new HttpClient()));
并将此添加到 web.config:
<system.web>
<compilation debug="true" targetFramework="4.8">
<assemblies>
<add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/>
</assemblies>
</compilation>
</system.web>