【问题标题】:ASP.NET 5, Microsoft.AspNet.WebApi.Client package, not found error in ASP.NET 5 beta3ASP.NET 5,Microsoft.AspNet.WebApi.Client 包,在 ASP.NET 5 beta3 中找不到错误
【发布时间】:2015-05-16 21:12:48
【问题描述】:

我有 ASP.NET MVC 6 应用程序,它调用一些外部 Web 服务。 我使用本指南:http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client 代码如下:

using System;
using System.Net.Http;
using System.Net.Http.Headers;

namespace TestService.Web.Code
{
    internal class ServiceProxy
    {
        internal string Get(string predicate)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:8001/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = client.GetAsync(string.Format("TestService/LookupService" +
                                                                         "/GetCountries?term={0}", predicate)).Result;

                if (response.IsSuccessStatusCode)
                {
                     var answer = response.Content.ReadAsStringAsync().Result;
                     return answer;
                }

                return string.Empty;
            }
        }
    }
}

但这里有一个意想不到的问题:构建项目后我有 3 个错误:

  • 错误 CS0246 找不到类型或命名空间名称“HttpClient”

  • 错误 CS0246 找不到类型或命名空间名称“MediaTypeWithQualityHeaderValue”

  • 错误 CS0246 找不到类型或命名空间名称“HttpResponseMessage”

看起来 Microsoft.AspNet.WebApi.Client 包版本与我的 KRE 版本不兼容。

我说的对吗?我该如何解决这个问题?

这里是 project.json 内容:

{

    "webroot": "wwwroot",
    "version": "1.0.0-*",
    "dependencies": {
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta3",
    "Microsoft.AspNet.Mvc": "6.0.0-beta3",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta3",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta3",
    "Microsoft.AspNet.Security.Cookies": "1.0.0-beta3",
    "System.Net.Http": "4.0.0-beta-22605",
    "Microsoft.AspNet.WebApi.Client": "5.2.3",
    "Microsoft.AspNet.Mvc.WebApiCompatShim": "6.0.0-beta3"
},
"frameworks": {
    "aspnet50": {
        "frameworkAssemblies": {
            "System.Net.Http": "4.0.0.0",
            "System.Net": "4.0.0.0"
        }
    },
    "aspnetcore50": {
        "dependencies": {
            "System.Net.Http": "4.0.0-beta-22605"
        }
    }
},
"exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
],
"bundleExclude": [
    "node_modules",
    "bower_components",
    "**.kproj",
    "**.user",
    "**.vspscc"
]
}

【问题讨论】:

  • 你能发布你的 project.json 文件吗?
  • 抱歉,忘记了 project.json。完成!

标签: c# asp.net asp.net-mvc asp.net-web-api asp.net-core


【解决方案1】:

你基本上是对的。在您的示例中,Microsoft.AspNet.WebApi.Client` 包同时用于 clrcore 和 clr 运行时。此包只能用于 clr 运行时。

随着最新的变化(重命名和所有这些),这是一个工作部分:

using System;
using System.Net.Http;
using System.Net.Http.Headers;

public class Program
{
    public void Main(string[] args)
    {
    }

    internal class ServiceProxy
    {
        internal string Get(string predicate)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:8001/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = client.GetAsync(string.Format("TestService/LookupService" +
                                                                         "/GetCountries?term={0}", predicate)).Result;

                if (response.IsSuccessStatusCode)
                {
                     var answer = response.Content.ReadAsStringAsync().Result;
                     return answer;
                }

                return string.Empty;
            }
        }
    }
}

project.json:

{
    "dependencies": {
    },
    "frameworks": {
        "dnx451": { 
            "dependencies": {
                                "Microsoft.AspNet.WebApi.Client": "5.2.3"
            }
        }
    }
}

【讨论】:

  • 好的,我做了 File --> New project --> Web --> ASP.NET 5 Console Application,然后我复制了你的 project.json 和 Program.cs 的内容,然后恢复了包.不幸的是问题仍然存在(
  • 我忘记了安装包 Microsoft.AspNet.WebApi.Client!现在可以了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多