ASP.NET 5,Microsoft.AspNet.WebApi.Client程序包,在ASP.NET 5 beta3中未发现错误

酿造者

我有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"
]
}
拖船

你基本上是对的。在您的示例中,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"
            }
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

发布的ASP.NET核心网站上的错误:找不到包'Microsoft.AspNet.WebApi.Client'的编译库位置

ASP.NET Core 2.0类库中Microsoft.AspNet.WebApi.Client的替代

名称空间“ Microsoft”中不存在“ AspNet”。在类库包中不能引用IdentityUser-ASP.NET 5

ASP.NET 5无法安装ASP.NET 5 NuGet程序包

Nuget程序包,其内容在ASP.net 5中

MVC 6(Asp.net 5)中损坏的基本NuGet程序包

Microsoft.Net.Http与Microsoft.AspNet.WebApi.Client

使用虚拟目录/应用程序在IIS中托管ASP.NET 5 WebAPI

.NET Core是否支持Microsoft.AspNet.WebApi.Client?

WebAPI One Asp.Net(ASP5 / vNext)中的分页

ASP.NET 5中的.NET版本

返回自定义错误WebApi ASP.NET 5 HttpResponseException错误的状态代码

我无法为ASP.NET5(核心CLR)安装Moq nuget程序包无法找到依赖项moq.netcore> = 4.4.0-beta8

ASP.NET 5 WebApi OPTIONS 预检中不允许使用 405 方法

同一解决方案中的ASP.NET Core 5 MVC / RazorPages和WebAPI项目

在Asp.net Core 5 WebAPI中获取SqlException和ObjectDisposedException

在ASP.NET 5 / MVC 6中区分MVC和WebAPI

Visual Studio 2015(ASP.NET 5):显示“管理Bower程序包...”上下文菜单

在新的ASP.NET MVC 5项目中更新NuGet程序包引用后,如何解决与JSON.NET的程序集版本冲突?

Microsoft Asp.Net 5 RC1

Asp.Net 5 Nuget软件包错误

ASP.NET 5中的OverrideAuthorizationAttribute

ASP .NET MVC 5中的异步任务

Asp.net 5中的Ajax操作

ASP.Net MVC 5中的Cookie

在ASP.NET 5中获取AuthenticationProperties

ASP.NET 5中的RoutePrefixAttribute

ASP.NET 5中的HandleUnknownAction

在ASP.NET 5中重写URL