Skip to content
3 changes: 2 additions & 1 deletion src/Messaging/RpcLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void Dispose()

public async void Log(LogLevel logLevel, string message, Exception exception = null, bool isUserLog = false)
{
var invocationId = _invocationId ?? "N/A";
if (isUserLog)
{
// For user logs, we send them over Rpc with details about the invocation.
Expand All @@ -47,7 +48,7 @@ public async void Log(LogLevel logLevel, string message, Exception exception = n
RpcLog = new RpcLog()
{
Exception = exception?.ToRpcException(),
InvocationId = _invocationId ?? _requestId,
InvocationId = invocationId,
Level = logLevel,
Message = message
}
Expand Down
30 changes: 18 additions & 12 deletions src/PowerShell/PowerShellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,8 @@ internal PowerShellManager(RpcLogger logger)
_pwsh.Streams.Warning.DataAdding += streamHandler.WarningDataAdding;
}

internal void InitializeRunspace()
internal void AuthenticateToAzure()
{
// Add HttpResponseContext namespace so users can reference
// HttpResponseContext without needing to specify the full namespace
_pwsh.AddScript($"using namespace {typeof(HttpResponseContext).Namespace}").InvokeAndClearCommands();

// Set the PSModulePath
Environment.SetEnvironmentVariable("PSModulePath", Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Modules"));

// TODO: remove this when we figure out why it fixed #48
_pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module").AddParameter("Name", "AzureRm.Netcore").InvokeAndClearCommands();

// Try to authenticate to Azure
// TODO: The Azure Functions Host might supply these differently. This might change but works for the demo
string applicationId = Environment.GetEnvironmentVariable("ApplicationId");
Expand All @@ -72,7 +62,7 @@ internal void InitializeRunspace()
string.IsNullOrEmpty(applicationSecret) ||
string.IsNullOrEmpty(tenantId))
{
_logger.Log(LogLevel.Warning, "Required environment variables to authenticate to Azure were not present", isUserLog: true);
_logger.Log(LogLevel.Warning, "Required environment variables to authenticate to Azure were not present");
return;
}

Expand All @@ -93,6 +83,19 @@ internal void InitializeRunspace()
}
}

internal void InitializeRunspace()
{
// Add HttpResponseContext namespace so users can reference
// HttpResponseContext without needing to specify the full namespace
_pwsh.AddScript($"using namespace {typeof(HttpResponseContext).Namespace}").InvokeAndClearCommands();

// Set the PSModulePath
Environment.SetEnvironmentVariable("PSModulePath", Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Modules"));

// TODO: remove this when we figure out why it fixed #48
_pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module").AddParameter("Name", "AzureRm.Netcore").InvokeAndClearCommands();
}

internal Hashtable InvokeFunction(
string scriptPath,
string entryPoint,
Expand All @@ -103,6 +106,9 @@ internal Hashtable InvokeFunction(
{
Dictionary<string, ParameterMetadata> parameterMetadata;

// We attempt to authenticate to Azure with every invocation
AuthenticateToAzure();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain why we are doing this for every function invocation? Are you expecting the environment variables may change for each function invocation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we talked offline. I will move it to WorkerInit for the demo but also opened #49 for understanding.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed.


// We need to take into account if the user has an entry point.
// If it does, we invoke the command of that name. We also need to fetch
// the ParameterMetadata so that we can tell whether or not the user is asking
Expand Down