When I try to use Azure Search on .NET Framework > 4.5.2, it blows up, throwing the above exception:
public SearchServiceClient Create()
{
return new SearchServiceClient(options.Value.ServiceName, new SearchCredentials(options.Value.SecurityKey));
}
System.TypeLoadException occurred
HResult=0x80131522
Message=Inheritance security rules violated by type: 'System.Net.Http.WebRequestHandler'. Derived types must either match the security accessibility of the base type or be less accessible.
Source=<Cannot evaluate the exception source>
StackTrace:
at Microsoft.Rest.ServiceClient`1.CreateRootHandler()
at Microsoft.Azure.Search.SearchServiceClient..ctor(String searchServiceName, SearchCredentials credentials)
at Search.Index.Search.SearchClientFactory.Create() in C:\code\Search\solutions\service\Search.Index\Search\SearchClientFactory.cs:line 18
This code is in a class library compiled with .NET Framework 4.5.2. When the executable which includes the class library is also 4.5.2, the above code works. But if I upgrade the executable to 4.6 (or 4.6.1, 4.6.2, 4.7.1) then the code starts throwing the exception.
I've spent the last couple of hours on this before raising the issue here - and I just figured out a workaround. I've added the NuGet package for System.Net.Http 4.3.3 to my class library which contains the above code - and now it works.
@mattfrear Thanks for reporting this and sharing your workaround. This looks like a broader compatibility issue that might affect other Azure .NET libraries too. Have you had this problem with any other libraries, or just Azure Search? Also, which version of the Microsoft.Azure.Search package did you use?
We are using the latest Microsoft.Azure.Search package - 3.0.4.
We haven't had this problem with other Azure libraries on .NET 4.6. We are using Microsoft.Azure.DocumentDB, WindowsAzure.ServiceBus, and WindowsAzure.Storage. And in fact, we're using Microsoft.Azure.Search in another project on 4.6 fine. The one that works uses ISearchIndexClient, the one that fails uses SearchServiceClient.
Something not standard about our project is we are using .NET Core style .csprojs but targeting .NET 4.6 via <TargetFramework>net46</TargetFramework>.
Thanks for the info. We'll investigate.
Possibly related: https://github.com/dotnet/corefx/issues/11100
Yes, I came across that issue too, I think I may have read the workaround on that page.
@mattfrear Thanks for confirming; I just found that issue yesterday. I'm hoping that fixing this will be a simple matter of ensuring that Microsoft.Azure.Search refers to the correct version of whatever dependency refers to System.Net.Http. First I have to repro the issue though. I'm working on that today.
@mattfrear So far I'm unable to repro this issue. I suspect it's because I can't even compile my test projects without adding an explicit reference to System.Net.Http, and this is presumably picking up the correct version of that assembly.
I'm trying to create a .NET 4.6 executable that references a .NET 4.52 class library which attempts to create a SearchServiceClient. I'm using the new .csproj format and the TargetFramework element as you did.
Would you be able to provide a minimal repro of this issue?
@brjohnstmsft OK, I've tried, but I can't repro the issue now in a standalone solution now. Happy for you to close it.
@mattfrear Thanks for checking. I'll close it.
Thanks @brjohnstmsft, your suggestion worked for me.
"I've installed the NuGet package for System.Net.Http 4.3.3 - and now it works."
@mattfrear Thank you
Most helpful comment
I've spent the last couple of hours on this before raising the issue here - and I just figured out a workaround. I've added the NuGet package for System.Net.Http 4.3.3 to my class library which contains the above code - and now it works.