Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AggregateException (Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll') in ASP.NET Web API project #202

Open
andresjacobo opened this issue Feb 6, 2017 · 88 comments
Assignees

Comments

@andresjacobo
Copy link

Similar to Issue #118.

I am running an ASP.NET project in .NET 4.6 using the latest 1.11.3 DocumentDB SDK and whenever I try to do any query using CreateDocumentQuery, the query is created successfully but trying to iterate through the results throws an AggregateException, making DocumentDB useless since queries can't be performed.

I've verified that Microsoft.Azure.Documents.ServiceInterop.dll is indeed in the project's bin folder.

What's interesting (and frustrating!) is that in a regular .NET console application using the same .NET SDK version and the same DocumentDB SDK version, the queries are executed without a problem, leading me to think the problem is specific to ASP.NET.

Please help.

Below is a stack trace of the exception thrown:

at Microsoft.Azure.Documents.ServiceInteropWrapper.CreateServiceProvider(String configJsonString, IntPtr& serviceProvider)\r\n at Microsoft.Azure.Documents.Query.QueryPartitionProvider.InitializeServiceProvider()\r\n at Microsoft.Azure.Documents.Query.QueryPartitionProvider.GetPartitionedQueryExecutionInfo(SqlQuerySpec querySpec, PartitionKeyDefinition partitionKeyDefinition)\r\n at Microsoft.Azure.Documents.Query.DocumentQueryExecutionContextBase.<GetPartitionedQueryExecutionInfoAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Documents.Query.DocumentQueryExecutionContextFactory.<CreateDocumentQueryExecutionContextAsync>d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Documents.Linq.DocumentQuery1.d__11.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Documents.Linq.DocumentQuery1.<ExecuteAllAsync>d__1b.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Documents.Linq.DocumentQuery1.d__d.MoveNext()`

@mingaliu
Copy link
Contributor

mingaliu commented Feb 6, 2017

What is the exact exception? Was it looks like:
System.DllNotFoundException: Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

If that is the case, you can try workaround in http://stackoverflow.com/questions/344608/unmanaged-dlls-fail-to-load-on-asp-net-server/4598747.

@andresjacobo
Copy link
Author

@mingaliu Thanks for the quick response. You are correct, the exact exception is:

InnerException {"Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"} System.Exception {System.DllNotFoundException}

I will try the workaround you mentioned and report back. Since our project is deployed to Azure from Visual Studio on my local machine, I wonder if the deployed version will have any problems?

@andresjacobo
Copy link
Author

The workaround suggested works on my local machine. I wonder if it will work once deployed in production?

@rnagpal
Copy link
Contributor

rnagpal commented Feb 6, 2017

@andresjacobo Does your ASP.NET Web project directly references DocumentDB Nuget package or is it through a second level dependency? I mean, is it like: ASP.NET Web project referencing a ASP.NET class library which references DocumentDB Nuget package.

If it's second level, try to directly reference the DocumentDB Nuget package and ensure that
your ASP.NET Web project gets the following import:

Let me know if you still see the issue.

@andresjacobo
Copy link
Author

@rnagpal To use DocumentDB on our ASP.NET Web project, I simply used Nuget inside Visual Studio to download the DocumentDB SDK. This, I believe, automatically adds a direct reference in the project's 'references' to Microsoft.Azure.Documents.Client.

So yes, I would say that even with a direct reference, the problem occurs. What can it be?

@rnagpal
Copy link
Contributor

rnagpal commented Feb 7, 2017

@andresjacobo Apart from the assembly reference to Microsoft.Azure.Documents.Client.dll ensure that you also get the following import:
<Import Project="..\packages\Microsoft.Azure.DocumentDB.1.11.3\build\Microsoft.Azure.DocumentDB.targets" Condition="Exists('..\packages\Microsoft.Azure.DocumentDB.1.11.3\build\Microsoft.Azure.DocumentDB.targets')" />
If you have it(which I believe), then try pasting the following in your project:
<Content Include="$(NugetPack)\Microsoft.Azure.DocumentDB.1.11.3\runtimes\win7-x64\native\DocumentDB.Spatial.Sql.dll"> <Link>DocumentDB.Spatial.Sql.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="$(NugetPack)\Microsoft.Azure.DocumentDB.1.11.3\runtimes\win7-x64\native\Microsoft.Azure.Documents.ServiceInterop.dll"> <Link>Microsoft.Azure.Documents.ServiceInterop.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content>

Let me know if that helps.

@andresjacobo
Copy link
Author

@rnagpal I do indeed have the first import. I then linked the other two libraries but when I ran the project I got the same exception (DLLNotFound).

Is there a particular place I should paste the content you mentioned? I don't think it should make a difference, but I can't get it to work.

@PratikChavda
Copy link

@rnagpal
I am getting same error but scenario is different
.net SDK version : 1.11.3
.Net version : 4.5.2
.Net Project Type : .net Class library.

I am refering this class library in unit test project.
I am getting this error when code performs await query.ExecuteNextAsync().
I have two sets of similar test

  1. for single partition and it execute successfully
  2. for two partition and it fails every time.

Below is stack trace:

Result StackTrace:
at Microsoft.Azure.Documents.ServiceInteropWrapper.CreateServiceProvider(String configJsonString, IntPtr& serviceProvider)
at Microsoft.Azure.Documents.Query.QueryPartitionProvider.Initialize()
at Microsoft.Azure.Documents.Query.QueryPartitionProvider.GetPartitionedQueryExecutionInfoInternal(SqlQuerySpec querySpec, PartitionKeyDefinition partitionKeyDefinition, Boolean requireFormattableOrderByQuery, Boolean isContinuationExpected)
at Microsoft.Azure.Documents.Routing.PartitionRoutingHelper.GetProvidedPartitionKeyRanges(SqlQuerySpec querySpec, Boolean enableCrossPartitionQuery, Boolean parallelizeCrossPartitionQuery, PartitionKeyDefinition partitionKeyDefinition, QueryPartitionProvider queryPartitionProvider, String clientApiVersion, QueryInfo& queryInfo)
at Microsoft.Azure.Documents.Query.DefaultDocumentQueryExecutionContext.d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Query.DefaultDocumentQueryExecutionContext.d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.BackoffRetryUtility1.<>c__DisplayClass2.<<ExecuteAsync>b__0>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.Documents.BackoffRetryUtility1.d__1b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Azure.Documents.BackoffRetryUtility1.<ExecuteRetry>d__1b.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.Documents.BackoffRetryUtility1.d__a.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Query.DocumentQueryExecutionContextBase.d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Query.DefaultDocumentQueryExecutionContext.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Query.DocumentQueryExecutionContextBase.d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Linq.DocumentQuery1.<ExecuteNextPrivateAsync>d__181.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at MyApp.Data.DocumentDB.Azure.DocumentRepository1.d__38.MoveNext() in C:\Users\username\Source\Repos\MyApp\MyApp.Data.DocumentDB\Azure\DocumentRepository.cs:line 67
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at MyApp.Data.DocumentDB.Tests.DocumentRepositoryTest.d__5.MoveNext() in C:\Users\username\Source\Repos\MyApp\MyApp.Data.DocumentDB.Tests\DocumentRepositoryTest.cs:line 34
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Result Message:
Test method MyApp.Data.DocumentDB.Tests.DocumentRepositoryTest.QueryAsync threw exception:
System.DllNotFoundException: Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

@rnagpal
Copy link
Contributor

rnagpal commented Feb 7, 2017

@PratikChavda The issue in the first portion of your Stack trace has been fixed in 1.11.4 release. Please upgrade and let me know if you get DllNotFound exception. I'm investigating the issue reported by @andresjacobo for the ASP.Net project.

@rnagpal
Copy link
Contributor

rnagpal commented Feb 7, 2017

@andresjacobo As long as it's in an ItemGroup tag under top level Project tag, you should be good.

Can you confirm that you have both the native dlls: Microsoft.Azure.Documents.ServiceInterop.dll and DocumentDB.Spatial.Sql.dll in the bin dir? I've seen reports wherein if the Spatial dll is not there at the same place as ServiceInterop, you get this exception as ServiceInterop depends upon Spatial dll and both need to be present. Unfortunately, I haven't been able to repro this on my end using an ASP.NET Web app hosted on azure but given some folks have reported it to us, I believe there is some configuration missing that causes this behavior.

Can you also try the workarounds mentioned in https://blogs.msdn.microsoft.com/jorman/2007/08/31/loading-c-assemblies-in-asp-net/ specifically:

`In web.config, add the following…use a path where the Native C++ DLL is located:

In global.asax, add the following:

protected void Application_Start(object sender, EventArgs e){
String _path = String.Concat(System.Environment.GetEnvironmentVariable("PATH"), ";", ConfigurationSettings.AppSettings["NativePath"]);
System.Environment.SetEnvironmentVariable("PATH", _path, EnvironmentVariableTarget.Process);
}
`
Let me know if that helps!

@rnagpal
Copy link
Contributor

rnagpal commented Feb 7, 2017

Just to have this post contain all possible cases when you run into this, if you are seeing this exception from a VS Unit test project trying to test an ASP.NET class library project that has a reference to DocumentDB Nuget package, you may have to add the following as an attribute to the test class:
[DeploymentItem(@"Microsoft.Azure.Documents.ServiceInterop.dll")] //force copy of dependent dll. [DeploymentItem(@"DocumentDB.Spatial.Sql.dll")] //force copy of dependent dll.

@andresjacobo
Copy link
Author

@rnagpal Thanks for the info. I can confirm both DLLs you mention are in the bin dir of the ASP.NET project.

I've tried every combination of all the advice you've given me, and this is what worked in the end:

I added the following code to the global.asax file:
String _path = String.Concat(System.Environment.GetEnvironmentVariable("PATH"), ";", System.AppDomain.CurrentDomain.RelativeSearchPath); System.Environment.SetEnvironmentVariable("PATH", _path, EnvironmentVariableTarget.Process);

With this in place, the DLLs seem to load fine and queries don't throw an AggregateException. This seems like a fairly good solution given that a) I don't pollute the System32 directory b) There's no version specific path that would break when the DocumentDB SDK is updated.

My only remaining question is why would we need to do this? Surely there is something specific in my project that causes this to happen. Given how often DocumentDB is used inside ASP.NET, I wouldn't think this is a widespread issue.

@rnagpal
Copy link
Contributor

rnagpal commented Feb 8, 2017

@andresjacobo Glad to hear that. I already reported this on our ASP.NET Discussions alias but haven't got any response yet. I'll update you when I hear from them.

This is not specific to DocumentDB as other packages with native dlls have the same issue. Also since I'm unable to repro this on my end, I don't believe this is a widespread issue as we would have heard this loud enough.

Do you mind creating a small repro app for me and attaching that to my email(listed in my profile) and I'll see if I can repro it? Just run this query on click of a button to repro this. Keep it as simple as possible. I can follow up with you offline in case I've any queries on the sample app.

@andresjacobo
Copy link
Author

@rnagpal Sorry it took me some time.

I've created a super simple ASP.NET project in VS and (un) fortunately I was able to reproduce the problem.

To recreate the problem, all I did was:

  1. Create a new project.
  2. Download DocumentDB SDK from Nuget.
  3. Create a test controller using the built in template from VS.
  4. Perform a query inside the controller.
  5. Exception is thrown 💥

I've omitted the DB key from the project. If you need to connect to our specific instance, I'd be happy to give it to you in a secure manner.

Let me know if you are able to reproduce the issue.

Download link

@clifflo
Copy link

clifflo commented Feb 22, 2017

I have faced a weird sitution. I was working for a traditional ASP.NET 4.5.2 Framework (not Core). When I use the DocumentDB in IIS express in Visual Studio it works. After that I publish the whole project to Azure and it still works. However when I use that in local IIS it does not work and have the same exception as the start of this issue. Afterwards I have referred to the workaround solition suggested by @mingaliu and I put the DocumentDB.Spatial.Sql.dll and the Microsoft.Azure.Documents.ServiceInterop.dll into the C:\Windows\System32. It works!!! So I think the problem is due to some dependencies error of the .NET Framework itself, rather than the DocumentDB SDK.

@thuru-zz
Copy link

thuru-zz commented Mar 3, 2017

@rnagpal : I think we're still stuck in that. I referenced the new 1.12.0 version of the Document DB NuGet and still having the error.

Could not load file or assembly 'file:///D:\home\site\wwwroot\bin\DocumentDB.Spatial.Sql.dll' or one of its dependencies. The module was expected to contain an assembly manifest.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.BadImageFormatException: Could not load file or assembly 'file:///D:\home\site\wwwroot\bin\DocumentDB.Spatial.Sql.dll' or one of its dependencies. The module was expected to contain an assembly manifest.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Assembly Load Trace: The following information can be helpful to determine why the assembly 'file:///D:\home\site\wwwroot\bin\DocumentDB.Spatial.Sql.dll' could not be loaded.

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Stack Trace:

[BadImageFormatException: Could not load file or assembly 'file:///D:\home\site\wwwroot\bin\DocumentDB.Spatial.Sql.dll' or one of its dependencies. The module was expected to contain an assembly manifest.]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +485
System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark) +190
System.Reflection.Assembly.LoadFrom(String assemblyFile) +54
WebActivator.PreApplicationStartCode.Start() +157

[InvalidOperationException: The pre-application start initialization method Start on type WebActivator.PreApplicationStartCode threw an exception with the following error message: Could not load file or assembly 'file:///D:\home\site\wwwroot\bin\DocumentDB.Spatial.Sql.dll' or one of its dependencies. The module was expected to contain an assembly manifest..]
System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection1 methods, Func1 setHostingEnvironmentCultures) +903
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +170
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +169
System.Web.Compilation.BuildManager.ExecutePreAppStart() +172
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +907

[HttpException (0x80004005): The pre-application start initialization method Start on type WebActivator.PreApplicationStartCode threw an exception with the following error message: Could not load file or assembly 'file:///D:\home\site\wwwroot\bin\DocumentDB.Spatial.Sql.dll' or one of its dependencies. The module was expected to contain an assembly manifest..]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +539
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +118
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +730

@clifflo
Copy link

clifflo commented Mar 4, 2017

Hi @thuru with regard to your case I think you should try adding the file to D:\home\site\wwwroot\bin\ or perhaps as of my case, adding it to C:\Windows\System32. In general I would say the problem is somehow related to .NET Framework itself not being able to the load the file in Nuget.

@rnagpal
Copy link
Contributor

rnagpal commented Mar 4, 2017

@thuru I'll send you an invite for a quick call so that we can close this issue asap as it seems that you have been having this issue from long.

@clifflo Instead of that hack, can you try the following code to the global.asax file:
String _path = String.Concat(System.Environment.GetEnvironmentVariable("PATH"), ";", System.AppDomain.CurrentDomain.RelativeSearchPath); System.Environment.SetEnvironmentVariable("PATH", _path, EnvironmentVariableTarget.Process);

and see if that resolves the issue?

@rnagpal
Copy link
Contributor

rnagpal commented Mar 4, 2017

@clifflo : The issue you are running into is different from Thuru. You might be seeing DllNotFoundException but Thuru is seeing BadImageFormatException which have different causes.

@thuru I see that this exception is coming from an external package WebActivator that seems to load our native dlls. Since these are native dlls, Assembly.LoadFrom() call is bound to fail with BadImageFormatException. Please refer to this for more details: http://stackoverflow.com/questions/18325799/could-not-load-file-or-assembly-hunspellx64-dll-while-using-nhunspell-nuget-pack

Can you try uninstalling the current version of WebActivator and install the latest one(looks like upgrade is not supported)?

Please keep me updated on this issue.

@wonix
Copy link

wonix commented Mar 27, 2017

I got the same exception as the title of this issue indicates.

2017-03-27_1611

Here's my setup

  • Visual Studio 2017
  • .NET standard 4.6.1
  • Web API 2.0 using Service Fabric
  • The API application references library that internally references Microsoft.Azure.DcoumentDB v1.13.0

I have another Service Fabric application which does not use Web API with a similar setup that does not throw the exception. The issue does seem to specific to mixing with Web API.

Directly referencing the same library within the Web API project resolved that issue for me. 👍

@rnagpal
Copy link
Contributor

rnagpal commented Mar 28, 2017

@wonix If your target application is project.json based and you are referring a library project that internally references DocumentDB nuget package, those native dependencies will not be copied as they are based on .targets file which is not supported in project.json projects, so yes you would need to explicitly reference DocumentDB nuget package in your target application which is project.json.

In your case, is Web API 2.0 a project.json based project?

@wonix
Copy link

wonix commented Mar 28, 2017

@rnagpal, my application is not project.json based. it was created using the Service Fabric Stateless Service template in Visual Studio 2015. The library project is also not project.json based.

@lehn0058
Copy link

I'm having the same issue as well. I have the latest NuGet update (1.13.1). DocumentDB queries work fine an a console application, but when trying to query from a WebAPI controller with the exact same code I get the exception:

System.DllNotFoundException: Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Note this is only on my local machine - deploying the web service to Azure web apps works fine.

@lehn0058
Copy link

lehn0058 commented Apr 3, 2017

I was able to fix this error when running locally by adding the following to my web.config under the system.web section:

<hostingEnvironment shadowCopyBinAssemblies="false" />

@dmunozgaete
Copy link

thanks @lehn0058 for the fix, i got the same problem and hours wasted trying to fix!.
your solution work for me!.

@YoungjaeKim
Copy link

I almost fight for a day and still unsolved.
WebApi .Net 4.6.1 project throws BadImageFormatException DocumentDB.Spatial.Sql while WPF 4.6.1 works fine.
How can I precisely diagnose and fix this issue?

I already have two dll files (Interop and Spatial) in bin folder correctly. Setup almost all combinations of x64/AnyCPU whatever configurations.

@YoungjaeKim
Copy link

YoungjaeKim commented May 30, 2017

Today, I tested with fresh ASP.NET WebApi project.

  • Visual Studio 2017 Enterprise
  • Azure Api App template (Azure SDK 3.0.1)
  • .Net framework 4.6
  • DocDB library 1.14.1
  • WebActivatorEx 2.2.0
  • Newtonsoft.Json 9.0.1

In below table;
StartUp stands for a project that created from Azure Api App template.
Library stands for depended .NET class library (same .Net Framework 4.6) of the Startup project.

Both have same DocDB library and code to read one DocDB record.

StartUp Library Result
AnyCPU - OK
x64 - Error
AnyCPU AnyCPU OK
AnyCPU x64 Error
x64 AnyCPU Error
x64 x64 Error

In summary, DocumentDB library should be used with AnyCPU configuration.
Build AnyCPU library but running on the x64 does not work.

In addition, I noticed that ALL configurations raise BadImageFormatException from DocumentDB.Spatial.Sql but AnyCPU runs fine if we ignore it.

For me, (1) adding <hostingEnvironment shadowCopyBinAssemblies="false" /> at system.web does NOT help, (2) adding PATH in Global.asax does NOT help either. I didn't try to copy system32 as this is too hacky.

@rnagpal
Copy link
Contributor

rnagpal commented May 30, 2017

@YoungjaeKim This is most likely an issue with your application trying to load the Spatial and Interop dlls that are native. If your process is trying to load these assemblies using Assembly.LoadFrom API it will fail with BadImageFormatException as expected: https://msdn.microsoft.com/en-us/library/system.badimageformatexception. Can you search for Assembly.LoadFrom in your application and ensure that this doesn't gets called for native assemblies?

Please refer to https://stackoverflow.com/questions/18325799/could-not-load-file-or-assembly-hunspellx64-dll-while-using-nhunspell-nuget-pack in case you are running into this issue.

Also, I see that you are using WebActivator package which might be the culprit. The above link says that the latest version of WebActivator seems to have fixed this issue. Can you double check that as well?

Note that you will run into this issue even if you don't have a single line of DocDB code in your application. This issue happens at startup of your application which tries to load all the assemblies using Assembly.LoadFrom which is the culprit.

Please let me know if that helps and if not we can investigate further.

@jstawski
Copy link

jstawski commented Jun 5, 2017

I just want to add to the discussion. We received

Unable to find an entry point named 'CreateServiceProvider' in DLL 'Microsoft.Azure.Documents.ServiceInterop.dll'.

Our website is hosted in Azure App Service as Azure Website. When I went to the bin folder I noticed that both of the DLLs mentioned here: Microsoft.Azure.Documents.ServiceInterop.dll and DocumentDB.Spatial.Sql.dll were there twice, but the second one has the temp extension .dll.MSDEPLOYTMP.

I am pretty sure that extension is there because of another problem we were having with deployment and locked files. This thread suggested to add the key MSDEPLOY_RENAME_LOCKED_FILES with a value of 1. Supposedly it tells te deployment to try to rename locked files.

Furthermore, we had redeployed the application and it started working again only to stop working after about an hour or so.

Do you think these renamed files could be causing the problem?

@ausfeldt ausfeldt reopened this Mar 10, 2018
@creyke
Copy link

creyke commented Mar 12, 2018

I get this same error message in the latest sdk (tools: 2.0.1-beta.23, runtime: 2.0.11587.0)...

[12/03/2018 17:20:59] A ScriptHost error has occurred
[12/03/2018 17:20:59] The listener for function 'Function1' was unable to start. System.Private.CoreLib: One or more errors occurred. (Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)). Microsoft.Azure.DocumentDB.Core: Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E).

...with only the following references...

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDb" Version="3.0.0-beta6" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.9" />
  </ItemGroup>

...as well as a CosmosDBTrigger reference...

        [FunctionName(nameof(Function1))]
        public static void Run([CosmosDBTrigger(
            databaseName: "databaseName",
            collectionName: "collectionName",
            ConnectionStringSetting = "connectionString",
            LeaseCollectionName = "leases")]IReadOnlyList<Document> input, TraceWriter log)
        {
            if (input != null && input.Count > 0)
            {
                log.Verbose("Documents modified " + input.Count);
                log.Verbose("First document Id " + input[0].Id);
            }
        }

@ausfeldt
Copy link
Contributor

@creyke The fix was in the single nuget which was rolled back. Here is a .net standard package which has the fix. Microsoft.Azure.DocumentDB.Core.1.9.1-dev-private.nupkg.

@creyke
Copy link

creyke commented Mar 12, 2018

@ausfeldt Thanks for uploading the dev package, however when I try and install it I get the following error, and it also does not seem to be a valid zip file...

[Local] 'C:\Working\NuGet\Microsoft.Azure.DocumentDB.Core.1.9.1-dev-private.nupkg' is not a valid nupkg file. Error: 'End of Central Directory record could not be found.'.

image

@ausfeldt
Copy link
Contributor

@creyke I just tried and was not able to reproduce. https://github.com/Azure/azure-documentdb-dotnet/raw/DevTestPackages/DevTestPackages/Microsoft.Azure.DocumentDB.Core.1.9.1-dev-private.nupkg

Install-Package

I would try one more time. Maybe the download was corrupt.

@creyke
Copy link

creyke commented Mar 12, 2018

@ausfeldt Thanks again, I'm sorry for the confusion, it was corrupt. After referencing it...

    <PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="1.9.1-dev-private" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDb" Version="3.0.0-beta6" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.9" />
    <PackageReference Include="System.Data.SqlClient" Version="4.4.0" />

...I still unfortunately get the same error...

[12/03/2018 21:46:57] A ScriptHost error has occurred
[12/03/2018 21:46:57] The listener for function 'Function1' was unable to start. System.Private.CoreLib: One or more errors occurred. (Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)). Microsoft.Azure.DocumentDB.Core: Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E).

@creyke
Copy link

creyke commented Mar 14, 2018

Sorry to chase, any options here? I am unable to deploy my code to Azure Functions so am completely blocked.

@ausfeldt
Copy link
Contributor

@creyke Is the azure function 64-bit running on windows?

@isaacabraham
Copy link

I'm seeing the same issue in with F# scripts against 32-bit FSI. I can't load Microsoft.Azure.Documents.ServiceInterop.dll since it is 64-bit only. Is the DocDb client nuget package 64-bit only?

Interestingly I only see this issue if I try to do an IQueryable query. Simple getting all rows works:

let myQuery = client.CreateDocumentQuery<Row>(pricesCollection.SelfLink)

myQuery |> Seq.toArray // works

query {
    for doc in myQuery do
    take 10
    select (doc.Price, doc.Town) }
|> Seq.toArray // pop

Any ideas?

@ausfeldt
Copy link
Contributor

@isaacabraham The nuget package is not 64-bit only. Microsoft.Azure.Documents.ServiceInterop.dll is 64-bit and native. The SDK will only use it if it's running on a Windows 64-bit OS.

@jackbond
Copy link

@ausfeldt I'm curious, given the agony that ServiceInterop.dll has created, is the performance boost on 64 bit windows worth it? Also, if you can run without it, can you guys simply catch the Exception and fall back gracefully?

@ausfeldt
Copy link
Contributor

@jackbond This option is an option that is being considered. Long term we are planing to get rid of this dependency all together. Thanks.

@creyke
Copy link

creyke commented Mar 24, 2018

@ausfeldt Apologies for no response to previous question. I ran out of time and had to remove my dependencies on cosmos unfortunately. Will monitor the thread, but I no longer have a reproduceable case as I had to ship something (have gone back to http for now instead of cosmos db triggers).

@isaacabraham
Copy link

@ausfeldt I'm running on 64 bit Windows 10. I can't reference those two interop libraries - it says Bad CLI Header. Is there no way to "disable" this 64 bit optimisation?

@ausfeldt
Copy link
Contributor

@isaacabraham There is currently no way to disable. The Nuget Package adds build props to make sure that the native DLL is copied to your bin directory. Native assemblies can't be referenced using the #r and #I. The working directory of the F# Interactive process is the path where it is installed, which is somewhere in C:\Program Files, where it will not find the DLL. You options are:

Set the working directory where the DLL is:

System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__

Windows also considers the Path environment variables to look for the DLL:

Environment.SetEnvironmentVariable("Path", Environment.GetEnvironmentVariable("Path") + ";" + __SOURCE_DIRECTORY__)

@isaacabraham
Copy link

@ausfeldt thanks for this. I'd say there are a few issues here:

  1. Build props obviously are only of use with a console application, rather than a script.
  2. Whilst the NuGet client may add build props, I don't believe Paket does.

The actual working directory for FSI isn't in C:\Program Files etc. - it varies depending on which IDE you use - but in VS Code, it's already set to the __SOURCE_DIRECTORY__ macro, and it isn't able to find the dll. However, I was able to get it working by adding the dll folder to the path e.g.

let x64 = IO.Path.GetFullPath @"packages\cosmos\Microsoft.Azure.DocumentDB\runtimes\win7-x64\native\"
Environment.SetEnvironmentVariable("Path", Environment.GetEnvironmentVariable "Path" + ";" + x64)

Thanks for your help here.

@spelltwister
Copy link

@ausfeldt I'm a little confused by these two comments:

Microsoft.Azure.DocumentDB.Core has been deprecated. Can you try 1.21.0 https://www.nuget.org/packages/Microsoft.Azure.DocumentDB/

@creyke 1.21.1 does not target .net standard anymore. Those changes were rolled back. https://www.nuget.org/packages/Microsoft.Azure.DocumentDB.Core is the package that supports .net standard.

Is the .Core package deprecated? Seems like they were both updated few weeks ago. Just want to make sure I'm not using a package that is now dead.

I have read thru the entire chain and tried all the suggestions I could apply to an asp.net core 2.0 site. Every package version fails with the same error "Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)". I tried to also include the fx version which seems to have helped someone else, but no luck on my end. I also tried updating permissions on the directory and gave Everyone Full Control, but no luck. Works fine when run via unit test, just doesn't work at all behind a webapi.

My only option at this point is to change my .netstandard lib to fx and reference the other DocumentDB package; adjusting any differences in code. Not sure if I can host this in an asp.net core 2.0 site or if needs to be asp.net fx. Hopefully that will at least let me ship this project. Kinda sucks that all the development was done against the unit test project until tests were passing only to find out the host has problems with this package T__T

@ausfeldt
Copy link
Contributor

@spelltwister
Copy link

Unfortunately, no luck with the new package. I tried updating just the asp.net core 2.0 references assuming it would use the newer version, but same error. I also tried updating both the .netstandard 1.6 service library and the asp.net core 2.0 site references, but same error. :( Will be trying to convert back to full framework lib and full framework asp.net site.

@spelltwister
Copy link

I was able to convert to using the fx package with no code changes required; just copied all the files into new full framework project so that's cool. The unit tests all still passed after making that change. I was able to do a basic test to see that the service does work when hosted in a asp.net full framework webapi project. However, I'm having other problems now due to System.Runtime not being found, but that's a different issue.

@ausfeldt
Copy link
Contributor

ausfeldt commented Apr 3, 2018

@jackbond Here are the packages that will gracefully fallback if the native assemblies are not found.
Microsoft.Azure.DocumentDB.1.21.1.nupkg and Microsoft.Azure.DocumentDB.Core.1.9.1.nupkg

@jackbond
Copy link

jackbond commented Apr 4, 2018

@ausfeldt Thanks Garrett. I managed to resolve this issue a while back by simply adding the DocDb nuget to all my executable projects, but still receive notifications on this issue. I'll be happy when the native stuff is gone entirely, as they seem to make little sense for this sort of project.

@r3plica
Copy link

r3plica commented Sep 13, 2018

I have started getting this issue now.. I had a working project on my master branch (which was published about a month ago) and today I needed to update my web.config (unrelated) and I started getting this error:

"Unable to find an entry point named 'CreateServiceProvider' in DLL 'Microsoft.Azure.Documents.ServiceInterop.dll'."

I have tried everything in this post and everything in the referenced post and I am still getting this error.
For reference, I have 3 projects with the Microsoft.Azure.DocumentDB package installed (v2.0.0): Project.DocumentDb (which has the service and repository), Project.Web which references Project.DocumentDb, so I added the package as suggested above and finally Project.Api which is the API with controllers, etc. This references Project.Web, so again I added the package as stated above.

I have checked the bin folder and both DLLs are present. This is annoying, because it just started happening .....

@ealsur
Copy link
Member

ealsur commented Sep 13, 2018

@r3plica Did you restart the App? That error can also appear if the ServiceInterop.dll is locked by some process.

@mcupito
Copy link

mcupito commented Sep 14, 2018

@ealsur What could be locking that .dll? Can you provide some examples or things to look for in code?

@ealsur
Copy link
Member

ealsur commented Sep 14, 2018

@mcupito Lock in DLLs in Azure App Service sometimes occur if there is an ongoing process using it, sometimes due to an underlying runtime upgrade (sometimes App Service sends updates to the framework) and I've seen it also happen on deployments. I'm not 100% sure of the reason though, just that an App Service restart unlocks it.

@Adam4224
Copy link

I was able to fix this error when running locally by adding the following to my web.config under the system.web section:

<hostingEnvironment shadowCopyBinAssemblies="false" />

Absolutely FANTASTIC. That was 2 days of absolute hell trying to get the new DocumentDB libraries working on the Azure app service. Fixed just like that!!! Fantastic!

@kirankumarkolli
Copy link
Member

Thanks @Adam4224 .

@j82w lets please capture it in troubleshooting FAQs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests