Azure WebJob does not get response from PostAsync call - azure

I have taken a Windows Service, modified it to be a Console app, then Published it as a WebJob. When it does a PostAsync command it gets no response AND does not execute the Console.WriteLine immediately following the PostAsync command.
Here's the code:
Console.WriteLine("Querying for access credentials...");
HttpResponseMessage responsePut = null;
try
{
Console.WriteLine("PostAsync call");
responsePut = await client.PostAsync("/oauth/access_token", new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded"));
Console.WriteLine("Returned from PostAsync call");
}
catch (System.Net.Http.HttpRequestException err)
{
Console.WriteLine("Unable to connect to the server (HttpRequestException)..." + err.InnerException);
throw new Exception("Unable to connect to the server (HttpRequestException)..." + err.InnerException);
}
catch (Exception err)
{
Console.WriteLine("Unable to connect to the server (Exception)..." + err.Message + "::" + err.InnerException);
throw new Exception("Unable to connect to the server (Exception)..." + err.InnerException);
}
The Azure Console Log is as follows:
[05/01/2018 05:45:31 > e559bd: INFO] Querying for access credentials...
[05/01/2018 05:45:31 > e559bd: INFO] PostAsync call
[05/01/2018 05:45:32 > e559bd: SYS INFO] Status changed to Success
[05/01/2018 05:45:32 > e559bd: SYS INFO] Process went down, waiting for 60 seconds
This code works as a Windows Service and as a Windows Console App, but fails on Azure.
What can I do to determine what Azure is doing to me and/or suggestions to do this differently.

I created a support ticket and got a reasonably quick response.
The line of code that includes the PostAsync had to be modified as follows:
responsePut = client.PostAsync("/oauth/access_token", new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded")).Result;
Note the removal of the await and the addition of the .Result.
This was considered a 'syntax error', which I disagree with! But it solved the problem.

Related

Unable to catch CosmosException when token expired in Blazor WebAssembly

I am using Azure CosmosDB with Blazor WebAssembly (client-side).
I want to catch CosmosException When connection token expires, But not getting cosmos exception for that, (Null exception found)
I have also tried same code in console application, In that I am able to catch cosmos exception that showing token expired.
Code sample :
using (CosmosClient client = new CosmosClient(account, token))
{
Database db = null;
db = client.GetDatabase("databaseName");
Container orgContainer = client.GetContainer("databaseName","containerName");
try
{
ItemResponse<CosmosException> response = await orgContainer.ReadItemAsync<CosmosException>("test", new PartitionKey("test"));
var data = response.Resource;
}
catch(CosmosException ex)
{
}
catch(Exception ex)
{
}
}
Exception Details of Console Application
This is probably because the exception occurs outside the try block. Also, make sure you do not instantiate a new cosmos client on each request. This does not perform well. Use singleton and keep alive between requests.

Microsoft Bot Framework - Call from Azure app service to external Web API

I had a Microsoft Bot Framework project that connect to Web API(that not hosted in azure).
in local host everything was work fine. but when i was deployed the Bot to an Azure app service. it's seem that the call is failed(and the Bot not return any response).
this is my post request:
public static string Post(List<ActionInputParams> data, string url)
{
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/json;charset=utf-8";
try
{
var res = wc.UploadString(url, "POST", JsonConvert.SerializeObject(data));
return res;
}
catch (Exception ex)
{
return "error " + ex.Message;
}
}
}
my question: there is a problem to call to external Web API from Azure app service? or i just missing something?
in the "log stream" i getting a 500 error:
HTTP Error 500.0 - Internal Server Error
The page cannot be displayed because an internal server error has occurred.

Error when enabling diagnostic logs - Azure Botframework

I'm having a problem where my Bot Framework bot stops responding to Kik.
I'd like to enable the diagnostic logs in Azure, but when I click "save", I get the error message "Failed to update web app logs settings for autoCompeteBot. There was an error processing your request. Please try again in a few moments."
Any idea how I can avoid this error and see my logs?
Thanks!
I followed the answer by #hailiu2586 here: https://github.com/Microsoft/BotBuilder/issues/1909.
Replace the following line in your messages/index.js
module.exports = { default: connector.listen() }
With:
var listener = connector.listen();
var withLogging = function(context, req) {
console.log = context.log;
listener(context, req);
}
module.exports = { default: withLogging }
After that done, console.log() should work.

Notification Hub RegistrationAuthorizationException

I followed this link yesterday to receive notification from azure notification hub on my windows 8.1 phone app and it was working but
But today it is throwing me this error
An exception of type 'Microsoft.WindowsAzure.Messaging.RegistrationAuthorizationException' occurred in mscorlib.ni.dll but was not handled in user code
Additional information: HTTP request failed.
HTTP Details:
Status: 401
Reason: Unauthorized
Full content: <Error><Code>401</Code><Detail>ExpiredToken: .TrackingId:e27f22fb-5c9c-4028-8594-eacb71e5a35e_G1,TimeStamp:4/14/2016 10:29:33 AM</Detail></Error>
It is throwing this exception in the InitNotificationsAsync method in my App.xaml.cs when my app is trying to register
private async void InitNotificationsAsync()
{
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
var hub = new NotificationHub("nameofhub", "myconnectionstring");
var result = await hub.RegisterNativeAsync(channel.Uri);
//Displays the registration ID so you know it was successful
if (result.RegistrationId != null)
{
var dialog = new MessageDialog("Registration successful: " + result.RegistrationId);
dialog.Commands.Add(new UICommand("OK"));
await dialog.ShowAsync();
}
}
Do you try it in the emulator or..?
It can be the time syncronization.
If that is the emulator, can you check the time and set it up manually?

erratic failure of sessionAsSignerWithFullAccess

The code below is a some test code that I have in an action button on an XPage. I need to get a handle on the current database with FullAccess in the code WFSUtils.sysOut just formats a printed message to the server console. Periodically the error message in the catch returns
2015-01-09 12:49:11 PM HTTP JVM: WFS ~~~ Error in Update Demo 'sessionAsSignerWithFullAccess' not found
I am the signer of the XPage and the database is signed by the server ID. Sometimes it runs through just fine other time it fails with the message above. If I shut everything down then restart it will generally run but then start failing. It is very random and unpredictable.
debug = true;
try{
if (debug) WFSUtils.sysOut("Starting YYY New SetupDemo");
var serverName = database.getServer();
var repID = database.getReplicaID();
thisDB = sessionAsSignerWithFullAccess.getDatabase("","");
thisDB.openByReplicaID(serverName,repID);
if (debug) WFSUtils.sysOut( "SetupDemo Success" + thisDB.getTitle());
}catch(e){
WFSUtils.sysOut("Error " + e.toString())
}finally{
try{
WFSUtils.recycleObjects([]);
if (debug) WFSUtils.sysOut("SetupDemo Recycle Success");
}catch(e){
if (debug) WFSUtils.sysOut("SetupDemo recycle Failed");
}
}
For one reason or another, it's important that all XPage elements be signed by the same ID when running. When they're not, sessionAsSigner and sessionAsSignerWithFullAccess get erratic as you describe - that may be the cause.

Resources