RestApi依赖性注射



我在本地机器中有两个应用程序,第一个是客户端应用程序和消费restapi响应,第二个是ASP.net WebApi应用程序。RestApi不包含任何DI类。客户端应用程序有DI类,并且消耗restapi响应并得到错误,但如果我要删除DI概念,那么它就可以正常工作。

从消费WebApihttp://localhost:7820/

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
}}

我在两行代码中犯了一个错误,这两行代码耗时restapi,即

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");

解决方案是删除获取和替换";access_token";关键字到";Bearer";

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",accessToken);
//Sending request to find web api REST service resource GetAllEmployees using HttpClient  
HttpResponseMessage Res = await client.GetAsync("api/Employee");

最新更新