in src/Microsoft.Azure.WebJobs.Extensions.Dapr/Services/DaprServiceListener.cs [146:223]
internal async Task WarnIfSidecarMisconfigured()
{
var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token;
string resBody;
try
{
var res = await this.daprClient.GetAsync(this.logger, $"{this.daprAddress}/v1.0/metadata", cancellationToken);
resBody = await res.Content.ReadAsStringAsync();
if (res.StatusCode != System.Net.HttpStatusCode.OK)
{
throw new Exception($"Failed to query the Metadata API, received status code {res.StatusCode}, response body: {resBody}");
}
}
catch (Exception ex)
{
this.logger.LogError($"Failed to query the Metadata API, exception: {ex}");
return;
}
JsonDocument jsonDocument;
try
{
jsonDocument = JsonDocument.Parse(resBody);
}
catch (Exception ex)
{
this.logger.LogError($"Failed to deserialize the Metadata API response, exception: {ex}");
return;
}
JsonElement root = jsonDocument.RootElement;
if (root.TryGetProperty("appConnectionProperties", out JsonElement appConnectionProperties))
{
// We set the appAddress, so it's safe to assume it's in the format we expect.
// appAddress is in the format "http://127.0.0.1:3001"
string[] appAddressParts = this.appAddress.Substring("http://".Length).Split(':');
string appChannelAddress = appAddressParts[0];
int appPort = int.Parse(appAddressParts[1]);
try
{
if (appConnectionProperties.TryGetProperty("port", out var port))
{
// port is an int in the Metadata API response.
var portInt = port.GetInt32();
if (portInt != appPort)
{
this.logger.LogWarning($"The Dapr sidecar is configured to listen on port {portInt}, but the app server is running on port {appPort}. This may cause unexpected behavior, see https://aka.ms/azfunc-dapr-app-config-error.");
}
}
else
{
// Daprd sidecar does not have port configured.
this.logger.LogWarning($"The Dapr sidecar is not configured to listen on a port, but the app server is running on port {appPort}. This may cause unexpected behavior, see https://aka.ms/azfunc-dapr-app-config-error.");
}
// channelAddress is always present in appConnectionProperties.
string address = appConnectionProperties.GetProperty("channelAddress").GetRawText().Trim('"');
if (address != appChannelAddress)
{
this.logger.LogWarning($"The Dapr sidecar is configured to listen on host {address}, but the app server is running on host {appChannelAddress}. This may cause unexpected behavior, see https://aka.ms/azfunc-dapr-app-config-error.");
}
}
catch (Exception e)
{
this.logger.LogError($"Failed to parse appConnectionProperties in Metadata API response body, exception: {e}");
return;
}
}
else
{
this.logger.LogDebug("appConnectionProperties not found in metadata API, skipping sidecar configuration check.");
return;
}
}