assembly discovery from library code - c#

I have some code in a common library to support internationalization. The basic idea is that given the fully qualified name of a RESX file location, you can look up values using a markup extension:
resx:ResxProperty.Name="SampleApp.Common.Resources.MainWindow"
Title="{Resx Key=Window.Title}"
Icon="{Resx Key=Window.Icon}"
To find the RESX file there is a routine to search all assemblies as shown below, and it works fine when the RESX file is in the same assembly as the xaml. BUT it breaks down when it is not.
Consider the solution structure below, where SampleApp.Wpf has the calling XAML and has a dependency on both the LocalizationLib and SampleApp.Common.
AppDomain.CurrentDomain.GetAssemblies() does not have SampleApp.Common at run time (although it does at design time).
How can I modify this code so it will know about SampleApp.Common at runtime?
Cheers,
Berryl
Library code
/// <summary>
/// Find the assembly that contains the type
/// </summary>
/// <returns>The assembly if loaded (otherwise null)</returns>
public static Assembly FindResourceAssembly(string resxName)
{
// check the entry assembly first - this will short circuit a lot of searching
//
var assembly = Assembly.GetEntryAssembly();
if (assembly != null && HasSpecifiedResx(assembly, resxName))
return assembly;
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var searchAssembly in assemblies)
{
// skip system assemblies
var name = searchAssembly.FullName;
if (_isSystemAssembly(name)) continue;
if (HasSpecifiedResx(searchAssembly, resxName))
return searchAssembly;
}
return null;
}
UPDATE
More details.
SampleApp.Wpf is targeting .net 4.0. Below are the references for it as displayed in Visual Studio.
The only project references in there are Infralution.Localization.Wpf and SampleApp.Common, both of which also target .Net 4.0.
Based on Metro Smurf's input I tried using assembly.GetReferencedAssemblies(). When I use this method, the VS designer does NOT find SampleApp.Common, and the following AssemblyNames are known in the debugger at runtime:
{System.Reflection.AssemblyName[6]}
[0]: {PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35}
[1]: {mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[2]: {System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[3]: {Infralution.Localization.Wpf, Version=2.1.2.0, Culture=neutral, PublicKeyToken=547ccae517a004b5}
[4]: {System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[5]: {PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35}
And when I use the origional AppDomain.CurrentDomain.GetAssemblies(), the designer DOES know SampleApp.Common, and these assemblies are known at runtime:
?AppDomain.CurrentDomain.GetAssemblies()
{System.Reflection.RuntimeAssembly[21]}
[0]: {mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[1]: {Microsoft.VisualStudio.HostingProcess.Utilities, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[2]: {System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[3]: {System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[4]: {System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[5]: {Microsoft.VisualStudio.HostingProcess.Utilities.Sync, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[6]: {Microsoft.VisualStudio.Debugger.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[7]: {vshost32, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[8]: {System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[9]: {System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[10]: {System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[11]: {Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[12]: {System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[13]: {System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[14]: {System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[15]: {WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35}
[16]: {PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35}
[17]: {PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35}
[18]: {SampleApp.Wpf, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[19]: {System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[20]: {Infralution.Localization.Wpf, Version=2.1.2.0, Culture=neutral, PublicKeyToken=547ccae517a004b5}
In both cases, SampleApp.Common isn't known at runtime. For the fun of it, I loaded that assembly in the debugger and got:
?Assembly.Load("SampleApp.Common")
{SampleApp.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[System.Reflection.RuntimeAssembly]: {SampleApp.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
CodeBase: "file:///C:/.../SampleApp.Wpf/bin/Release/SampleApp.Common.DLL"
EntryPoint: null
EscapedCodeBase: "file:///C:.../SampleApp.Wpf/bin/Release/SampleApp.Common.DLL"
Evidence: {System.Security.Policy.Evidence}
FullName: "SampleApp.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
GlobalAssemblyCache: false
HostContext: 0
ImageRuntimeVersion: "v4.0.30319"
IsDynamic: false
IsFullyTrusted: true
Location: "C:\\...\\SampleApp.Wpf\\bin\\Release\\SampleApp.Common.dll"
ManifestModule: {SampleApp.Common.dll}
PermissionSet: {<PermissionSet class="System.Security.PermissionSet"
version="1"
Unrestricted="true"/>
}
ReflectionOnly: false
SecurityRuleSet: Level2
I also tried Metro Smurf's suggestion to reference a static string from SampleApp.Common in SampleApp.Wpf. No change on that one, although I guess it further proves that SampleApp.Common is properly referenced.
Other things to try on SampleApp.Common might include adding an EntryPoint, a SNK, or a HashAlgorithm.
the FIX
asm.GetReferencedAssemblies sounds like the ticket, but it only returns assemblies which are loaded in the memory, and assemblies aren't loaded if they aren't referenced in the code (as in it's confusing that while also quite proper to say one project has a reference to another, but that isn't the sort of reference this method returns).
It turns out that AppDomain.CurrentDomain.GetAssemblies() works the same way.
Metro Smurf was on the right track, but apparently a static reference won't get the assembly loaded, but creating a type instance from the assembly will. So:
Console.WriteLine(Class1.MyDummyString) // this won't do it
Console.WriteLine(new Class1()) // finally, the assembly is loaded
As much as I dislike creating a dummy class, in this case it is an easy fix; I can leave the original library code as is.

You may want to consider using the Assembly.GetReferencedAssemblies Method. However, if I'm not mistaken, you'll need to exclude the framework assemblies as well.

Related

Why does my Azure Function App, which generates PDF files from data, time out?

I've created an Azure Function App in Visual Studio 2019 on .NET Core 2.1. My Function takes a set of data (less than 1 MB) and generates a PDF report of the data based on an HTML template (using the NuGet package OpenHtmlToPdf).
The function is triggered by an Azure Service Bus Queue.
It works perfectly when I run the function app locally, with my local.settings.json pointing to the queue on Azure. I send a message to the queue and everything works/completes as expected within a matter of seconds. But when I deploy it to Azure and when it is triggered, it times out during the PDF generation process after working for several minutes. Everything else works up until it reaches this block of code (after the LogInformation() call):
_log.LogInformation("Converting template and content into PDF file...");
pdf = Pdf
.From(content)
.OfSize(PaperSize.A4)
.WithoutOutline()
.WithMargins(1.25.Centimeters())
.Portrait()
.Comressed()
.Content();
_log.LogInformation("Finished converting template and content into PDF file");
It never reaches the second LogInformation("Finished....") call, timing out before that point while executing the Pdf calls.
Here is a sample of the App Insights log (first my logged message, then the timeout exception):
12/4/2019, 2:30:18.351 PM Converting template and content into PDF file... 1 trace {"prop__{OriginalFormat}":"Converting template and content into PDF file...","HostInstanceId":"fe52d18f-9830-40bc-a92b-f0a90cf37dc9","Category":"Function.PDFProcessorFunction.User","LogLevel":"Information","ProcessId":"4456","InvocationId":"cab3b347-95d3-45ff-b24d-c7c5048e6a6b"} PDFProcessorFunction 3f325922eef58b46b2f635d283deeebe |3f325922eef58b46b2f635d283deeebe.60bad8bb00011f46. PC 0.0.0.0 df-eo-pdf-queue-function-dev c8ddc12737b5c414811dd541ac7507946788a5bbd653984da9e4a88632659933 9f5886c8-fb23-4ef3-a0af-5cb7333f460e df-eo-pdf-queue-function-dev 7a528756-2a38-4511-8b8d-04365870a91c azurefunctions: 2.0.12888.0 87ec212c-16cc-11ea-888f-5f9ca4eaab67 1
timestamp [UTC]
2019-12-04T19:30:18.351427Z
message
Converting template and content into PDF file...
severityLevel
1
itemType
trace
customDimensions
{"prop__{OriginalFormat}":"Converting template and content into PDF file...","HostInstanceId":"fe52d18f-9830-40bc-a92b-f0a90cf37dc9","Category":"Function.PDFProcessorFunction.User","LogLevel":"Information","ProcessId":"4456","InvocationId":"cab3b347-95d3-45ff-b24d-c7c5048e6a6b"}
operation_Name
PDFProcessorFunction
12/4/2019, 2:35:14.307 PM Timeout value of 00:05:00 exceeded by function 'PDFProcessorFunction' (Id: 'cab3b347-95d3-45ff-b24d-c7c5048e6a6b'). Initiating cancellation. 3 trace {"prop__{OriginalFormat}":"Timeout value of 00:05:00 exceeded by function 'PDFProcessorFunction' (Id: 'cab3b347-95d3-45ff-b24d-c7c5048e6a6b'). Initiating cancellation.","HostInstanceId":"fe52d18f-9830-40bc-a92b-f0a90cf37dc9","LogLevel":"Error","Category":"Function.PDFProcessorFunction","ProcessId":"4456","InvocationId":"cab3b347-95d3-45ff-b24d-c7c5048e6a6b"} PDFProcessorFunction 3f325922eef58b46b2f635d283deeebe |3f325922eef58b46b2f635d283deeebe.60bad8bb00011f46. PC 0.0.0.0 df-eo-pdf-queue-function-dev c8ddc12737b5c414811dd541ac7507946788a5bbd653984da9e4a88632659933 9f5886c8-fb23-4ef3-a0af-5cb7333f460e df-eo-pdf-queue-function-dev 7a528756-2a38-4511-8b8d-04365870a91c azurefunctions: 2.0.12888.0 32061e7c-16cd-11ea-ae75-6364d66f68e2 1
timestamp [UTC]
2019-12-04T19:35:14.307144Z
message
Timeout value of 00:05:00 exceeded by function 'PDFProcessorFunction' (Id: 'cab3b347-95d3-45ff-b24d-c7c5048e6a6b'). Initiating cancellation.
severityLevel
3
itemType
trace
customDimensions
{"prop__{OriginalFormat}":"Timeout value of 00:05:00 exceeded by function 'PDFProcessorFunction' (Id: 'cab3b347-95d3-45ff-b24d-c7c5048e6a6b'). Initiating cancellation.","HostInstanceId":"fe52d18f-9830-40bc-a92b-f0a90cf37dc9","LogLevel":"Error","Category":"Function.PDFProcessorFunction","ProcessId":"4456","InvocationId":"cab3b347-95d3-45ff-b24d-c7c5048e6a6b"}
operation_Name
PDFProcessorFunction
operation_Id
3f325922eef58b46b2f635d283deeebe
operation_ParentId
|3f325922eef58b46b2f635d283deeebe.60bad8bb00011f46.
client_Type
PC
client_IP
0.0.0.0
cloud_RoleName
df-eo-pdf-queue-function-dev
cloud_RoleInstance
c8ddc12737b5c414811dd541ac7507946788a5bbd653984da9e4a88632659933
appId
9f5886c8-fb23-4ef3-a0af-5cb7333f460e
appName
df-eo-pdf-queue-function-dev
iKey
7a528756-2a38-4511-8b8d-04365870a91c
sdkVersion
azurefunctions: 2.0.12888.0
itemId
32061e7c-16cd-11ea-ae75-6364d66f68e2
itemCount
1
And here are the actual timeout exception details:
12/4/2019, 2:09:01.106 PM Microsoft.Azure.WebJobs.Host.FunctionTimeoutException at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryHandleTimeoutAsync>d__29.MoveNext Microsoft.Azure.WebJobs.Host.FunctionTimeoutException Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryHandleTimeoutAsync>d__29.MoveNext Microsoft.Azure.WebJobs.Host.FunctionTimeoutException Timeout value of 00:05:00 was exceeded by function: PDFProcessorFunction Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryHandleTimeoutAsync>d__29.MoveNext 3 [{"severityLevel":"Error","outerId":"0","message":"Timeout value of 00:05:00 was exceeded by function: PDFProcessorFunction","parsedStack":[{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryHandleTimeoutAsync>d__29.MoveNext","level":0,"line":660,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":1,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":2,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":3,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter`1.GetResult","level":4,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<InvokeAsync>d__27.MoveNext","level":5,"line":576,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":6,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":7,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":8,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.GetResult","level":9,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<ExecuteWithWatchersAsync>d__26.MoveNext","level":10,"line":532,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":11,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":12,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":13,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.GetResult","level":14,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<ExecuteWithLoggingAsync>d__25.MoveNext","level":15,"line":468,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":16,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":17,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":18,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.GetResult","level":19,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<ExecuteWithLoggingAsync>d__19.MoveNext","level":20,"line":278,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":21,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<ExecuteWithLoggingAsync>d__19.MoveNext","level":22,"line":322,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":23,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":24,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":25,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter`1.GetResult","level":26,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryExecuteAsyncCore>d__16.MoveNext","level":27,"line":117,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"}],"type":"Microsoft.Azure.WebJobs.Host.FunctionTimeoutException","id":"15999211"}] exception {"FormattedMessage":"Executed 'PDFProcessorFunction' (Failed, Id=6b0cebaa-0876-428d-a324-03265563651b)","HostInstanceId":"d7abe331-beea-4165-86b4-0323f307939a","InvocationId":"6b0cebaa-0876-428d-a324-03265563651b","ProcessId":"5856","LogLevel":"Error","Category":"Function.PDFProcessorFunction"} PDFProcessorFunction 3f325922eef58b46b2f635d283deeebe |3f325922eef58b46b2f635d283deeebe.30b1b04f0e993f4a. PC 0.0.0.0 df-eo-pdf-queue-function-dev 34c90aa5d115bbf81d1cb11cf8ef01321f11d527a73c6ae5f86ec717cb94c776
timestamp [UTC]
2019-12-04T19:09:01.1061419Z
problemId
Microsoft.Azure.WebJobs.Host.FunctionTimeoutException at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryHandleTimeoutAsync>d__29.MoveNext
type
Microsoft.Azure.WebJobs.Host.FunctionTimeoutException
assembly
Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
method
Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryHandleTimeoutAsync>d__29.MoveNext
outerType
Microsoft.Azure.WebJobs.Host.FunctionTimeoutException
outerMessage
Timeout value of 00:05:00 was exceeded by function: PDFProcessorFunction
outerAssembly
Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
outerMethod
Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryHandleTimeoutAsync>d__29.MoveNext
severityLevel
3
details
[{"severityLevel":"Error","outerId":"0","message":"Timeout value of 00:05:00 was exceeded by function: PDFProcessorFunction","parsedStack":[{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryHandleTimeoutAsync>d__29.MoveNext","level":0,"line":660,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":1,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":2,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":3,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter`1.GetResult","level":4,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<InvokeAsync>d__27.MoveNext","level":5,"line":576,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":6,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":7,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":8,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.GetResult","level":9,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<ExecuteWithWatchersAsync>d__26.MoveNext","level":10,"line":532,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":11,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":12,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":13,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.GetResult","level":14,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<ExecuteWithLoggingAsync>d__25.MoveNext","level":15,"line":468,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":16,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":17,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":18,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.GetResult","level":19,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<ExecuteWithLoggingAsync>d__19.MoveNext","level":20,"line":278,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":21,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<ExecuteWithLoggingAsync>d__19.MoveNext","level":22,"line":322,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":23,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":24,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":25,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter`1.GetResult","level":26,"line":0},{"assembly":"Microsoft.Azure.WebJobs.Host, Version=3.0.15.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryExecuteAsyncCore>d__16.MoveNext","level":27,"line":117,"fileName":"C:\\projects\\azure-webjobs-sdk-rqm4t\\src\\Microsoft.Azure.WebJobs.Host\\Executors\\FunctionExecutor.cs"}],"type":"Microsoft.Azure.WebJobs.Host.FunctionTimeoutException","id":"15999211"}]
itemType
exception
customDimensions
{"FormattedMessage":"Executed 'PDFProcessorFunction' (Failed, Id=6b0cebaa-0876-428d-a324-03265563651b)","HostInstanceId":"d7abe331-beea-4165-86b4-0323f307939a","InvocationId":"6b0cebaa-0876-428d-a324-03265563651b","ProcessId":"5856","LogLevel":"Error","Category":"Function.PDFProcessorFunction"}
operation_Name
PDFProcessorFunction
operation_Id
3f325922eef58b46b2f635d283deeebe
operation_ParentId
|3f325922eef58b46b2f635d283deeebe.30b1b04f0e993f4a.
client_Type
PC
client_IP
0.0.0.0
cloud_RoleName
df-eo-pdf-queue-function-dev
cloud_RoleInstance
34c90aa5d115bbf81d1cb11cf8ef01321f11d527a73c6ae5f86ec717cb94c776
appId
9f5886c8-fb23-4ef3-a0af-5cb7333f460e
appName
df-eo-pdf-queue-function-dev
iKey
7a528756-2a38-4511-8b8d-04365870a91c
sdkVersion
azurefunctions: 2.0.12888.0
itemId
88417461-16c9-11ea-9da1-1b43f8d3fcf0
itemCount
1
And yes, I have tried increasing the timeout value (up to our service plan's maximum of 10 minutes) - but it still times out. Even though it only takes a few seconds to execute on my local machine.
In addition, I have checked, double-checked, triple-checked all the Function App configuration settings. (I should mention that my company has several other Azure Function Apps written in .NET Core 2.1 that work fine).
What am I doing wrong? Is is simply that Azure Functions are not meant to handle resource-intensive operations such as generating PDFs?
I am facing a similar problem which also demands intensive CPU. Seems to me that the problem is something in the Azure Functions side. What you can do is use a Durable Functions and pass this PDF generation to an activity. I am not sure if it will solve for you or not, but worths a try.

Transient System.Security.Cryptography.CryptographicException in TwilioRequestValidator

So we have our Twilio callbacks setup to validate incoming requests, via the TwilioRequestValidator documented here.
However, something we're seeing, is that the production server will run for weeks with no problems, and then suddenly start failing with a CryptographicException. This causes all incoming Twilio requests to fail.
We have a call-stack (thanks to Azure Application Insights):
System.Security.Cryptography.CryptographicException:
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Security.Cryptography.Utils.HashData (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Security.Cryptography.SHA1CryptoServiceProvider.HashCore (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Security.Cryptography.HashAlgorithm.TransformBlock (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Security.Cryptography.HMAC.HashCore (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Security.Cryptography.HashAlgorithm.ComputeHash (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at Twilio.Security.RequestValidator.GetValidationSignature (Twilio, Version=5.14.1.0, Culture=neutral, PublicKeyToken=null)
at Misc.TwilioRequestValidator.ValidateTwilioRequestAttribute.IsValidRequest (RCHHRATool, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)
at Misc.TwilioRequestValidator.ValidateTwilioRequestAttribute.OnActionExecuting (RCHHRATool, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)
at System.Web.Mvc.Async.AsyncControllerActionInvoker+AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncControllerActionInvoker+AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncControllerActionInvoker+<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__31 (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase`1.Begin (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeActionMethodWithFilters (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncControllerActionInvoker+<>c__DisplayClass21.<BeginInvokeAction>b__19 (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase`1.Begin (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Controller.<BeginExecuteCore>b__1c (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncVoid`1.CallBeginDelegate (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase`1.Begin (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Controller.BeginExecuteCore (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase`1.Begin (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Controller.BeginExecute (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4 (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncVoid`1.CallBeginDelegate (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase`1.Begin (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.Mvc.MvcHandler.BeginProcessRequest (System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
at System.Web.HttpApplication+<>c__DisplayClass285_0.<ExecuteStepImpl>b__0 (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
at System.Web.HttpApplication.ExecuteStepImpl (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
at System.Web.HttpApplication.ExecuteStep (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
And we have some application code, here:
[AttributeUsage(AttributeTargets.Method)]
public class ValidateTwilioRequestAttribute : ActionFilterAttribute
{
private readonly RequestValidator _requestValidator;
public ValidateTwilioRequestAttribute()
{
var authToken = ConfigurationManager.AppSettings["TwilioAuthToken"];
_requestValidator = new RequestValidator(authToken);
}
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
var context = actionContext.HttpContext;
if (!IsValidRequest(context.Request))
{
actionContext.Result = new HttpStatusCodeResult(HttpStatusCode.Forbidden);
}
base.OnActionExecuting(actionContext);
}
private bool IsValidRequest(HttpRequestBase request)
{
var signature = request.Headers["X-Twilio-Signature"];
Debug.WriteLine(request.Headers["X-Twilio-Signature"]);
//var requestUrl = request.RawUrl;
var requestUrl = rewriteUri(request.Url.AbsoluteUri);
Debug.WriteLine("URI is: " + rewriteUri(request.Url.AbsoluteUri));
return _requestValidator.Validate(requestUrl, request.Form, signature);
}
private string rewriteUri(string absoluteUri)
{
//check to make sure we're not replacing 'https' with 'httpss'
if (!absoluteUri.Contains("https"))
{
return Regex.Replace(absoluteUri, #"http", "https");
}
return absoluteUri;
}
}
Any ideas on what could be causing this? Ideally, I'd like to handle this exception state, but I'm not sure how to handle the exception without knowing what the cause is.
EDIT: I'm getting this error message when I drill into the exception in application insights:
"Hash not valid for use in specified state."
EDIT #2: A quick search on that error messages digs up this interesting discussion on the thread safety of the Cryptography object.
Looks like this is a bug in the Twilio-csharp library. I believe they're using their Sha1 member in a non-thread safe manner.
I've hacked up a solution to re-init the class in the event of a CryptographicException. Since a server restart is fixing the issue, I think a force re-init of the class is called for.
try
{
return _requestValidator.Validate(requestUrl, request.Form, signature);
}
catch (CryptographicException e)
{
//if the request fails, re-init the class and try again
var authToken = ConfigurationManager.AppSettings["TwilioAuthToken"];
_requestValidator = new RequestValidator(authToken);
return IsValidRequest(request);
}
EDIT: Worth noting that this fix is causing knock-on issues related to the non-thread safe manner in which the library is used.

Writing to CloudAppendBlob: The remote server returned an error: (409) Conflict & ProcessExpectedStatusCodeNoException

I am trying to understand what causes the The remote server returned an error: (409) Conflict Exception.
I am writing lines to an AppendBlob by acquiring a lease, appending the text, and breaking the lease, like so. The error only happens every now and then, so I am curious what scenario would cause it to happen. My suspicion would be something may happen if multiple users hit the endpoint where this code executes and somehow to a call to acquire a lease happens at the same time.
// Get the AppendBlob reference
CloudAppendBlob blob = ….
// Acquire a lease on the blob until a call to breaking the lease is made
string leaseId = Guid.NewGuid().ToString();
await blob.AcquireLeaseAsync(null, leaseId);
// Append a new entry to the blob
await blob.AppendTextAsync(
textToBeAppended,
Encoding.UTF8,
new AccessCondition { LeaseId = leaseId },
new BlobRequestOptions(),
new OperationContext());
// break the lease on the blob
await blob.BreakLeaseAsync(null);
The Failed Method is Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException.
I see from telemetry that a call to PUT on the blob initially fails with the 409 status code and the exception above, but immediately after a successful PUT call with a 202 result code occurs. I'm assume Azure Storage is doing a retry, however I do not see the line of text in the file it was supposed to be appended to.
Here is the full call stack if that is helpful, I am not sure what the meaning of ProcessExpectedStatusCodeNoException is from this stack trace.
Microsoft.WindowsAzure.Storage.StorageException: at
Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndExecuteAsync
(Microsoft.WindowsAzure.Storage, Version=9.3.1.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35Microsoft.WindowsAzure.Storage,
Version=9.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:
c:\Program Files
(x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.csMicrosoft.WindowsAzure.Storage,
Version=9.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 50)
at
Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions+<>c__DisplayClass2`1.b__0
(Microsoft.WindowsAzure.Storage, Version=9.3.1.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35Microsoft.WindowsAzure.Storage,
Version=9.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:
c:\Program Files
(x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Util\AsyncExtensions.csMicrosoft.WindowsAzure.Storage,
Version=9.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 69)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw
(mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
(mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089) at
Microsoft.Community.Data.Providers.BlobStorageProvider+d__1.MoveNext
(Microsoft.Community.Data, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=nullMicrosoft.Community.Data, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null:
C:\BA\813\s\Microsoft.Community\Microsoft.Community.Data\Providers\BlobStorageProvider.csMicrosoft.Community.Data,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 67) at
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw
(mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089) at
Microsoft.Community.Data.Providers.BlobStorageProvider+d__1.MoveNext
(Microsoft.Community.Data, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=nullMicrosoft.Community.Data, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null:
C:\BA\813\s\Microsoft.Community\Microsoft.Community.Data\Providers\BlobStorageProvider.csMicrosoft.Community.Data,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 72) at
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw
(mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
(mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089) at
Microsoft.Community.Web.Repositories.AzureMLRecommendationsRepository+d__6.MoveNext
(Microsoft.Community.Web, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=nullMicrosoft.Community.Web, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null:
C:\BA\813\s\Microsoft.Community\Microsoft.Community.Web\Repositories\AzureMLRecommendationsRepository.csMicrosoft.Community.Web,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 95)
Inner exception System.Net.WebException handled at
Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndExecuteAsync:
at
Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException
(Microsoft.WindowsAzure.Storage, Version=9.3.1.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35Microsoft.WindowsAzure.Storage,
Version=9.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:
c:\Program Files
(x86)\Jenkins\workspace\release_dotnet_master\Lib\Common\Shared\Protocol\HttpResponseParsers.Common.csMicrosoft.WindowsAzure.Storage,
Version=9.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 54)
at
Microsoft.WindowsAzure.Storage.Blob.CloudBlob+<>c__DisplayClass39.b__38
(Microsoft.WindowsAzure.Storage, Version=9.3.1.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35Microsoft.WindowsAzure.Storage,
Version=9.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:
c:\Program Files
(x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlob.csMicrosoft.WindowsAzure.Storage,
Version=9.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:
3830) at
Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndGetResponse
(Microsoft.WindowsAzure.Storage, Version=9.3.1.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35Microsoft.WindowsAzure.Storage,
Version=9.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:
c:\Program Files
(x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.csMicrosoft.WindowsAzure.Storage,
Version=9.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:
299)
I'm wondering why you need to lease the blob and then append the text to the blob. If there are multiple writers simultaneously, it's EXPECTED that you'll meet (409) Conflict in your code.
As you can see in the official doc, acquiring lease (B) when a blob is still being leased by lease (A) will encounter 409 error:
Basically, blob lease is to achieve exclusive write access on a blob, it's inappropriate to use blob lease if you want to append against a blob from multiple writers simultaneously.
What are you afraid of if the AcquireLease and BreakLease operations are removed from your code? To be honest, I don't see any harm.
Sounds like an issue with with concurrency possibly:
Breaks the lease, if the blob has an active lease. Once a lease is broken, it cannot be renewed. Any authorized request can break the lease; the request is not required to specify a matching lease ID. When a lease is broken, the lease break period is allowed to elapse, during which time no lease operation except break and release can be performed on the blob. When a lease is successfully broken, the response indicates the interval in seconds until a new lease can be acquired.
https://learn.microsoft.com/en-us/rest/api/storageservices/lease-blob#outcomes-of-use-attempts-on-blobs-by-lease-state
If you look at link above you will see a huge table on how 409s can occur.
Can you run a fiddler to get the trace on inner exception?

Load Assembly in new Appdomain, needs parent assembly to be fully trusted

I run a macro assembly inside my main application. Macro does not need to access parent assembly. This is the snippet:
Assembly ParentAssembly
{
class c1
{
void RunMacro()
{
System.Security.PermissionSet PS = new System.Security.PermissionSet(PermissionState.None);
PS.AddPermission(new SOME_PERMISSIONS....);
AppDomainSetup ADS = new AppDomainSetup();
ADS.ApplicationBase = "c:";
AppDomain domain = AppDomain.CreateDomain(SomeName, null, ADS, PS);
System.Runtime.Remoting.ObjectHandle handle = Activator.CreateInstanceFrom(domain, typeof(Sandboxer2).Assembly.ManifestModule.FullyQualifiedName, typeof(Sandboxer2).FullName);
Sandboxer2 m = (Sandboxer2)handle.Unwrap();
m.Execute();
}
}
}
I receive this exception:
Attempt by security transparent method
'SandBoxer.Sandboxer2.Execute()' to access security critical method
'System.AppDomain.add_AssemblyResolve(System.ResolveEventHandler)'
failed.
Assembly 'Parent Assembly full name...' is partially trusted, which
causes the CLR to make it entirely security transparent regardless of
any transparency annotations in the assembly itself. In order to
access security critical code, this assembly must be fully trusted.
My question:
Is there any way to avoid loading parent assembly in child assembly?
In second line of my code, what permissions can solve the problem?
There are some assemblies that will be loaded by AssemblyResolve event of SandBoxer at runtime. Assemblies get loaded from database as binary array or from GAC. They are not fully trusted. I control their behavior with permission objects added at second line of code. Are there special permissions that I have to add for letting them be only loaded as partially trusted assemblies?
I think everything can be done by adding security permissions like second line of code, If I'm misunderstanding the concept, I would be grateful to be guided.
EDIT1:
Parent Assembly is the assembly of main application that creates instance of SandBoxr and runs it. Please take a look at SandBoxer2 class and its Execute method:
public class Sandboxer2 : MarshalByRefObject
{
public void Execute()
{
AppDomain ad = AppDomain.CurrentDomain;
ad.AssemblyResolve += MyHandler;
.
.
.
}
}
Inside of Execute method, after ad is instantiated, I used ad.GetAssemblies() and this was the list of all assemblies already loaded. Line number 2 holds ParentAssembly from the very beginning of execution of sandboxer.
[0] {mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} System.Reflection.Assembly
{System.Reflection.RuntimeAssembly}
[1] {System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a} System.Reflection.Assembly
{System.Reflection.RuntimeAssembly}
[2] {ParentAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null} System.Reflection.Assembly
{System.Reflection.RuntimeAssembly}
[3] {System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} System.Reflection.Assembly
{System.Reflection.RuntimeAssembly}
[4] {System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} System.Reflection.Assembly
{System.Reflection.RuntimeAssembly}
[5] {MacroBase_IO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null} System.Reflection.Assembly
{System.Reflection.RuntimeAssembly}
[6] {System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} System.Reflection.Assembly
{System.Reflection.RuntimeAssembly}
[7] {System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a} System.Reflection.Assembly
{System.Reflection.RuntimeAssembly}
[8] {System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} System.Reflection.Assembly
{System.Reflection.RuntimeAssembly}
Answer to questions 1 and 2:
Sandboxer must be in a separate assembly (Another DLL) and this separate assembly must be signed with a key. Then, main application will not be automatically loaded and this exception won't be raised.
Edit:
1- signing with a key is done through Properties of Assembly-Signing tab.
2- This sample helps understanding how to define an assembly as full trust and introduce strong names to Sandbox.

NGEN Display Returns A different result than Elevated CMD when run using Process.Start() in C# winforms

i have been trying to figure this out with my co-workers for a while now.
i use Ngen to Make Native images and boost my Applications performance on the clients.
lets assume the Exe Filename is Example.exe
what i try to do in the app is that i do ngen install example.exe,and the ngen will install native images for the assebmlies
however here is the problem :
if i try an Elevated CMD and run C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen Display example.exe
the example.exe file name and the version of the native image is displayed,however,using the following code which does the same thing in the windows forms c#,it does not display the filename and version of the example.exe file,and displays the dependencies instead,such as Telerik and so on.
so my question is why does the CMD return the correct result while the process returns the dependencies instead of the filename and version itself?
here is some of my code:
public static bool Run()
{
var f = new Loading();
bool state = false;
var b = new BackgroundWorker();
string runtimeStr = RuntimeEnvironment.GetRuntimeDirectory();
string ngenStr = Path.Combine(runtimeStr, "ngen");
Process process = new Process
{
StartInfo =
{
FileName = ngenStr,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = false
}
};
if (Environment.OSVersion.Version.Major >= 6)
{
process.StartInfo.Verb = "runas";
}
process.StartInfo.Arguments = "display " + Application.ProductName;
process.Start();
// process.WaitForExit();
string stdoutx = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
however the stdoutx string does not contain the example.exe filename or version
EDIT : examples of what should be and what is :
this is what the cmd returns which is the correct result :
Example, Version=1.0.0.19, Culture=neutral, PublicKeyToken=null <debug>
this is what is :
Microsoft (R) CLR Native Image Generator - Version 4.6.79.0
Copyright (c) Microsoft Corporation. All rights reserved.
NGEN Roots:
C:\Project\Example\bin\Debug\Example.EXE
NGEN Roots that depend on "Example":
C:\Project\Example\bin\Debug\Example.EXE
Native Images:
Klik.Windows.Forms.EntryLib.V2.2008, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
NCalc, Version=1.3.8.0, Culture=neutral, PublicKeyToken=973cde3f1cafed03
Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
RedGate.Migrations.Core, Version=1.0.0.1401, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 <debug>
RedGate.Shared.ComparisonInterfaces, Version=1.0.2.278, Culture=neutral, PublicKeyToken=7f465a1c156d4d57
RedGate.Shared.SQL, Version=9.9.0.0, Culture=neutral, PublicKeyToken=7f465a1c156d4d57
RedGate.Shared.Utils, Version=9.9.0.0, Culture=neutral, PublicKeyToken=7f465a1c156d4d57
RedGate.SOCCompareInterface, Version=3.0.50.59, Culture=neutral, PublicKeyToken=7f465a1c156d4d57
RedGate.SQLCompare.ASTParser, Version=11.0.0.414, Culture=neutral, PublicKeyToken=7f465a1c156d4d57
RedGate.SQLCompare.Engine, Version=11.1.0.2, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 <debug>
RedGate.SQLCompare.Rewriter, Version=11.1.0.2, Culture=neutral, PublicKeyToken=7f465a1c156d4d57
System.Configuration.Install, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Data.SQLite, Version=1.0.85.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139
System.Deployment, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Telerik.Reporting, Version=9.0.15.324, Culture=neutral, PublicKeyToken=a9d7983dfcc261be
Telerik.ReportViewer.WinForms, Version=9.0.15.324, Culture=neutral, PublicKeyToken=a9d7983dfcc261be
Telerik.WinControls.ChartView, Version=2015.1.331.40, Culture=neutral, PublicKeyToken=5bb2a467cbec794e
Telerik.WinControls, Version=2015.1.331.40, Culture=neutral, PublicKeyToken=5bb2a467cbec794e
Telerik.WinControls.GridView, Version=2015.1.331.40, Culture=neutral, PublicKeyToken=5bb2a467cbec794e
Telerik.WinControls.Themes.Office2010Black, Version=2015.1.331.40, Culture=neutral, PublicKeyToken=5bb2a467cbec794e
Telerik.WinControls.Themes.Office2010Silver, Version=2015.1.331.40, Culture=neutral, PublicKeyToken=5bb2a467cbec794e
Yes, you are not doing this correctly. The syntax of the command you are using is:
ngen.exe display [assemblyName | assemblyPath]
Note the difference between assemblyName and assemblyPath. When you ran it from cmd.exe you typed "example". And thus specified the (partial) assembly name. In your code, you used "example.exe". And the Environment.CurrentDirectory is set correctly by accident, a common accident. So Ngen.exe can find the file and it gets chatty. Listing not just the assembly name but also the native images for all of its dependencies. Like Klik.Windows.Forms.EntryLib etcetera.
Not the only problem, note that you specified the partial assembly name. You want to make sure that the specific version of your assembly was ngen-ed. Make the code look like this instead:
process.StartInfo.Arguments = "display \"" +
System.Reflection.Assembly.GetEntryAssembly().FullName + "\"";
And test it with Project > Properties > Debug tab > untick the "Enable the Visual Studio hosting process" checkbox.
Secondary things to fret about are the bitness of the native images (x86 vs x64) and getting your app elevated so you can install the native images, something you normally only have a shot at when your app is installed.

Categories