Note: This is the same Issue affecting the FaceAPI as reported in #4534
Note: v2.0.0-preview does not include an implementation of ServiceClientCredentials, as noted as #4483, which is why I've included class ApiKeyServiceClientCredentials : ServiceClientCredentials in the sample code, below.
The following code, using Microsoft.Azure.CognitiveServices.Language.TextAnalytics v2.0.0-preview, throws Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models.ErrorResponseException:
Operation returned an invalid status code 'NotFound'
readonly TextAnalyticsClient TextAnalyticsApiClient = new TextAnalyticsClient(new ApiKeyServiceClientCredentials(CognitiveServicesConstants.FaceApiKey));
public static async Task<Dictionary<string, double?>> GetSentiment(List<string> textList)
{
var textIdDictionary = new Dictionary<string, string>();
var multiLanguageBatchInput = new MultiLanguageBatchInput(new List<MultiLanguageInput>());
foreach (var text in textList)
{
var textGuidString = Guid.NewGuid().ToString();
textIdDictionary.Add(textGuidString, text);
multiLanguageBatchInput.Documents.Add(new MultiLanguageInput(id: textGuidString, text: text));
}
var sentimentResults = await TextAnalyticsApiClient.SentimentAsync(multiLanguageBatchInput).ConfigureAwait(false);
if (sentimentResults?.Errors?.Any() ?? false)
{
var exceptionList = sentimentResults.Errors.Select(x => new Exception($"Id: {x.Id}, Message: {x.Message}"));
throw new AggregateException(exceptionList);
}
var resultsDictionary = new Dictionary<string, double?>();
foreach (var result in sentimentResults?.Documents)
resultsDictionary.Add(textIdDictionary[result.Id], result?.Score);
return resultsDictionary;
}
class ApiKeyServiceClientCredentials : ServiceClientCredentials
{
readonly string _subscriptionKey;
public ApiKeyServiceClientCredentials(string subscriptionKey) => _subscriptionKey = subscriptionKey;
public override Task ProcessHttpRequestAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request is null)
throw new ArgumentNullException("request");
request.Headers.Add("Ocp-Apim-Subscription-Key", _subscriptionKey);
return Task.FromResult<object>(null);
}
}
Update the BaseUri to use the Uri containing the specific Azure Region of the Cognitive Service instance as noted in the API docs, .e.g https://[location].api.cognitive.microsoft.com/
In this example, I am using the westus region for the location because that is the region in which my Cognitive Service Text Analytics API resource in Azure.
readonly TextAnalyticsClient TextAnalyticsApiClient = new TextAnalyticsClient(new ApiKeyServiceClientCredentials(CognitiveServicesConstants.FaceApiKey))
{
BaseUri = new Uri("https://westus.api.cognitive.microsoft.com/")
};
public static async Task<Dictionary<string, double?>> GetSentiment(List<string> textList)
{
var textIdDictionary = new Dictionary<string, string>();
var multiLanguageBatchInput = new MultiLanguageBatchInput(new List<MultiLanguageInput>());
foreach (var text in textList)
{
var textGuidString = Guid.NewGuid().ToString();
textIdDictionary.Add(textGuidString, text);
multiLanguageBatchInput.Documents.Add(new MultiLanguageInput(id: textGuidString, text: text));
}
var sentimentResults = await TextAnalyticsApiClient.SentimentAsync(multiLanguageBatchInput).ConfigureAwait(false);
if (sentimentResults?.Errors?.Any() is true)
{
var exceptionList = sentimentResults.Errors.Select(x => new Exception($"Id: {x.Id}, Message: {x.Message}"));
throw new AggregateException(exceptionList);
}
var resultsDictionary = new Dictionary<string, double?>();
foreach (var result in sentimentResults?.Documents)
resultsDictionary.Add(textIdDictionary[result.Id], result?.Score);
return resultsDictionary;
}
class ApiKeyServiceClientCredentials : ServiceClientCredentials
{
readonly string _subscriptionKey;
public ApiKeyServiceClientCredentials(string subscriptionKey) => _subscriptionKey = subscriptionKey;
public override Task ProcessHttpRequestAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request is null)
throw new ArgumentNullException("request");
request.Headers.Add("Ocp-Apim-Subscription-Key", _subscriptionKey);
return Task.CompletedTask;
}
}
Adding @noellelacharite for visibility
Please add label:"Cognitive Services"
@LukeBayler I don't have the permission to add labels
Correct me if I'm wrong, but in Microsoft.Azure.CognitiveServices.Language.TextAnalytics v2.8.0-preview TextAnalyticsClient does not contain an accessible property called BaseUri so I can't see how that work around would solve the issue. Do we need to rollback to 2.0.0?
The property is now called EndPoint
The property is now called EndPoint On Dec 21, 2018, at 10:38 AM, Joe notifications@github.com wrote: Correct me if I'm wrong, but in Microsoft.Azure.CognitiveServices.Language.TextAnalytics v2.8.0-preview TextAnalyticsClient does not contain an accessible property called BaseUri so I can't see how that work around would solve the issue. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Yep, apologies, I can see that now.
var client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials(apiKey))
{
Endpoint = "https://northeurope.api.cognitive.microsoft.com",
};
that did in-fact solve the issue.
Hi @brminnick , I am getting same error still now Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models.ErrorResponseException: 'Operation returned an invalid status code 'NotFound''.
Is it resolved
It is not fixed.
The workaround is to assign the Endpoint property.
@DigitalSa1nt’s response shows an example of how to do it in v2.8.0-preview
The workaround is to include the region and specify the URL till microsoft.com
So my endpoint looks like
https://centralindia.api.cognitive.microsoft.com this
and not this
https://centralindia.api.cognitive.microsoft.com/text/analytics/v2.0
Your endpoint should be like this
https://centralindia.api.cognitive.microsoft.com/text/analytics/v2.0
I have created Demo POC and shared here , it may help you
https://bit.ly/2GUW2FV
I have same problem! is it till now not solved?
Do like dev-aritra suggested. Set your endpoint only until microsoft.com, so it looks like, in my case, https://brazilsouth.api.cognitive.microsoft.com
and NOT like
https://brazilsouth.api.cognitive.microsoft.com/text/analytics/v2.0.
It worked for me.
Closing this ticket as the endpoint should be set to just the base URL. Please re-open if the issue persists.
We have also updated the endpoint in the Ibiza portal to match this format
Most helpful comment
Do like dev-aritra suggested. Set your endpoint only until microsoft.com, so it looks like, in my case, https://brazilsouth.api.cognitive.microsoft.com
and NOT like
https://brazilsouth.api.cognitive.microsoft.com/text/analytics/v2.0.
It worked for me.