【发布时间】:2021-01-11 00:55:42
【问题描述】:
我在本地机器上有两个应用程序,第一个是客户端应用程序并消耗 restapi 响应,第二个是 ASP.net WebApi 应用程序。 RestApi 不包含任何 DI 类。客户端应用程序有 DI 类并消耗 restapi 响应并得到错误,但如果我将删除 DI 概念,那么它工作正常。
从 http://localhost:7820/ 使用 WebApi
public class Employee : IEmployee
{
string Baseurl = "http://localhost:7820/";
public async Task<IEnumerable<employee>> GetAll(string accessToken)
{
List<employee> employee_ = new List<employee>();
try
{
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("access_token", "Bearer " + accessToken);
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync("api/Employee/Get");
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
employee_ = JsonConvert.DeserializeObject<List<employee>>(EmpResponse);
}
//returning the employee list to view
return employee_;
}
}
catch (Exception er)
{
return employee_;
}
}
Web API 服务:
public IHttpActionResult Get()
{
try
{
using (employeesmgmtEntities entities = new employeesmgmtEntities())
{
List<employee> list = (
from a in new employeesmgmtEntities().employees
select new
{
emplId = a.emplId,
empName = a.empName,
age = a.age,
fullAddress = a.fullAddress,
city = a.city,
pinCode = a.pinCode,
mobileNo = a.mobileNo,
emailId = a.emailId,
bankName=a.bankName,
accountNo = a.accountNo,
ifscCode = a.ifscCode,
salary = a.salary
}).AsEnumerable().Select(o => new employee
{
emplId = o.emplId,
empName = o.empName,
age = o.age,
fullAddress = o.fullAddress,
city = o.city,
pinCode = o.pinCode,
mobileNo = o.mobileNo,
emailId = o.emailId,
bankName = o.bankName,
accountNo = o.accountNo,
ifscCode = o.ifscCode,
salary = o.salary
}).ToList();
if (list != null)
{ return Ok(list); }
else
{ return NotFound(); }
}
}
catch (Exception ex)
{ return BadRequest(ex.ToString()); }
}
HttpResponseMessage 出现错误
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1,
Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
X-SourceFiles: =?UTF-8?B?UzpcU09GVFdBUkVTLVBST0ZFU1NJT05BTFxFU1NFTkNFXFNJVEVTX1VOREVSLUNPTlNUUlVDVElPTlxTQVVSQUJIXGVtcGxveWVlc01nbXRBcGlcZW1wbG95ZWVzTWdtdEFwaVxhcGlcRW1wbG95ZWVcR2V0?=
Cache-Control: no-cache
Date: Sun, 10 Jan 2021 13:29:06 GMT
Server: Microsoft-IIS/10.0
WWW-Authenticate: Bearer
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 71
Content-Type: application/json; charset=utf-8
Expires: -1
}}
【问题讨论】:
-
您的问题中返回的错误信息描述性不够,任何人都无法帮助您。请确保您获得实际的异常信息。这可以通过更改 IDE 中的异常设置或更改在开发环境中输出错误的方式来完成。一旦找到,请发布它们,包括 full 堆栈跟踪。就目前而言,您的问题需要更加明确,另一方面包含too much unnecessary information,因此可能会被关闭。
-
感谢 Steven 的帮助,我终于解决了编码错误的问题。我会在评论中提到。
标签: asp.net-web-api dependency-injection rest