Sujeet Raman

Sujeet Raman

  • 864
  • 927
  • 352.9k

Error:exception was thrown by the target of invocation” C# Httpclient

Jun 24 2021 7:37 AM

I have two API created in .net core 3.1 (lets say api A & api B) api A will call  api B and merging 2 out put.But when call,
I am getting :"Error:: Exception has been thrown by the target of an invocation.: Error Executing C# Script"

so as i said i have 2 api ,s are running diffrent port api A and api B.

I need to get the result from api A  to api B.so In my api a has the combination of abi A and api B.I am using the same request for both.

the problem is when i call a external api call to api B from api B.But it works when i run api B only.I dont understand the issue

in api B

public async Task GetKeyData(string key){
    string dataKey = string.Empty;
    var requestUri = string.Format("{0}/{1}", _KeyUri, key);
    HttpClient client = new HttpClient(new HttpClientHandler(){
      UseDefaultCredentials = true });
    client.DefaultRequestHeaders.Accept.Add(new 
    MediaTypeWithQualityHeaderValue("application/json"));
    var rawData = await client.GetAsync(requestUri);
    // below  line are getting error when i call from api A to this 
    var jsonResponse = await rawData.Content.ReadAsStringAsync();//error showing here
    string value = JsonConvert.DeserializeObject(jsonResponse);
    //balance code
} 

What mistake i am doing here?


Answers (2)