Resolve CS0121 in System.Web.Http - c#

This code:
Request.CreateResponse(HttpStatusCode.PartialContent);
Yields following compilation error:
Error CS0121 The call is ambiguous between the following methods or
properties:
'System.Net.Http.HttpRequestMessageExtensions.CreateResponse(System.Net.Http.HttpRequestMessage,
System.Net.HttpStatusCode)' and
'System.Web.Http.HttpRequestMessageCommonExtensions.CreateResponse(System.Net.Http.HttpRequestMessage,
System.Net.HttpStatusCode)'
Both are in System.Net.Http namespace, one in System.Web.Http assembly the other in System.Net.Http.Formatting but I need them both as a reference. Even worse System.Net.Http.HttpRequestMessageExtensions class is present in both assemblies.
As a workaround, I could use this overload, which seems to be in only one of them:
Request.CreateResponse<object>(HttpStatusCode.PartialContent, null);
But is there any other way to resolve the conflict?
[Steps to reproduce]
Create a console app
Add the reference to System.Net.Http
Add the reference to System.Net.Http.Formatting from NuGet
Add using System.Net.Http
Add var req = new HttpRequestMessage();
Try adding req.CreateResponse(HttpStatusCode.OK);
You should see 12 extension method overloads. Some are duplicated, those are the problem.

In general, you can use extension methods directly to resolve such ambiguities:
Instead of
myRequest.CreateResponse(HttpStatusCode.PartialContent);
use
HttpRequestMessageExtensions.CreateResponse(myRequest, HttpStatusCode.PartialContent);
In this specific case, I could not reproduce the issue following your instructions. However, I have downloaded the Microsoft.AspNet.WebApi.Client Nuget instead of the System.Net.Http.Formatting Nuget since the later one is marked as deprecated. Maybe try to update to the newer Nuget?
Update: I can reproduce the issue when also installing the System.Web.Http.Common Nuget and adding using System.Web.Http; to the file. Using the explicit call to the method as shown above resolves the issue.

Related

Polly.Caching.Memory.MemoryCacheProvider throws System.TypeLoadException : method not implemented for one application

I'm using these pacakges:
PackageReference Include="Polly" Version="7.2.2"
PackageReference Include="Polly.Caching.Memory" Version="3.0.2"
I have a testproject that uses these packages and the code works.
When I try to integrate my code in an existing application with lots of other nuget packages, suddenly I get a TypeLoadException.
The method TryGet in the type Polly.Caching.Memory.MemoryCacheProvider from the assembly Polly.Caching.Memory, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc doesn't have an implementation.
I have been checking everything, but I can't find the cause of this.
The versions of Polly and Polly.Caching.Memory are aligned across all projects involved.
In the bin folder the correct version appears.
I looked at the code of the assembly and the code has an implementation for TryGet
Any suggestion on what I can do to further investigate this?
The packages were all correctly configured.
I got on the right track when I saw that the method was returning a tuple.
I figured that perhaps this second application was not using the correct versions of the same packages.
I changed :
<HintPath>..\Solutions\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
To
<HintPath>..\Solutions\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
Bingo!

System.ObservableExtensions.Subscribe: ambiguous reference

CrossBleAdapter.Current.WhenStatusChanged().Subscribe(obj => { });
I'm getting the following error for the subscribe method
/Users/sarath/Developer/Workspace/GCH Mobile
App/Gogch.iOS/TMAirController.cs(57,57): Error CS0121: The call is
ambiguous between the following methods or properties:
'System.ObservableExtensions.Subscribe(System.IObservable,
System.Action)' and
'System.ObservableExtensions.Subscribe(System.IObservable,
System.Action)' (CS0121) (_SmartFHR.iOS)
How can I resolve this?
Edit
Upon further investigation I found that ObservableExtensions exists both System.Reactive.Core and System.Reactive. I can't remove either because other packages depend on it.
Update
Thank you for pointing me in the right direction.
I found the problem. I was using System.Reactive version 4.0.0 and System.Core.Reactive version 3.0.0. Once I changed core version to 4.0.0, problem solved.

Azure Functions: Adding Neo4j Driver dependency to C# HTTP Trigger throws Error

Im creating a Azure function which should connect to a neo4j database after receiving a https request. I used the Visual Studio 2015 function preview to set up my project and to create a C# HTTP trigger function.
I then added a Neo4j dependency to my project.json
"dependencies": {
"Neo4j.Driver": "1.2.0"
}
After a NuGet rebuild i get multiple error like the following:
run.csx(8,19): error CS1929: 'HttpRequestMessage' does not contain a definition for 'GetQueryNameValuePairs' and the best extension method overload 'HttpRequest MessageExtensions.GetQueryNameValuePairs(HttpRequestMessage)' requires a receiver of type 'HttpRequestMessage'
I didn't change anything else of the default function code and after adding the dependent the Framework Versions are still the same.
Do i have to add some default dependencies so that i can still use the HTTP trigger?
Edit: i uploaded the Content of the project.lock.json files maybe this helps
Without dependency https://api.myjson.com/bins/1f5hrv
With dependency (3000 lines) https://api.myjson.com/bins/14fovv
The reference to Neo4j.Driver causes a reference to System.Net.Http to be added, using a version that leads to a type mismatch between what is referenced and the extension methods from the assemblies provided by the runtime.
Please add your own references to System.Net.Http, see this stackoverflow question: Azure Functions - System.Net.Http

The type 'Microsoft.SqlServer.Types.SqlGeography' exists in both 'Microsoft.SqlServer.Types.dll' and 'Microsoft.SqlServer.Types.dll'

In my windows class library (consumed by a MVC website) I have installed the NugetPackage Microsoft.SqlServer.Types (Spatial).
Now, using ado.net I am trying to read the value by doing:
protected SqlGeography MapSqlGeography(DbDataReader reader, string key)
{
return reader[key] is DBNull ? null : (SqlGeography)reader[key];
}
If I add a brake point in this line and in the visual studio watch window I type: "reader[key]", I can see the correct Point(XXXX,XXX) of type: "object {Microsoft.SqlServer.Types.SqlGeography}"
But, as soon as I try to make the cast I have the following error:
(SqlGeography)reader[key] The type 'Microsoft.SqlServer.Types.SqlGeography' exists in both
'Microsoft.SqlServer.Types.dll' and
'Microsoft.SqlServer.Types.dll'
Main strange fact is that the dlls are exactly the same...
As far as I know I only have one "source" for this namespace/class name, it should not be duplicated....
My "usings" are:
using Microsoft.SqlServer.Types;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Threading.Tasks;
Any ideas on how to solve this? Thanks.
Update #1
I uninstalled the NugetPackage `Microsoft.SqlServer.Types (Spatial)' and instead tried the one called: 'Microsoft.SqlServer.Types (Unofficial)' and even after cleaning all the previous folders/files and also cleaning up the "bin/obj", I continue to have the exact same exception....
I simply do now see the cause of this now.... any ideas would be really appreciated.
Update #2
Just tried to use extern alias destination;
return reader[key] is DBNull
? null
: (destination.Microsoft.SqlServer.Types.SqlGeography)reader[key];
And have the exception:
Cannot cast 'reader[key]' (which has an actual type of 'Microsoft.SqlServer.Types.SqlGeography')
to
'Microsoft.SqlServer.Types.SqlGeography'
Microsoft.SqlServer.Types.SqlGeography
I encountered this error today because a referenced library included a different version of Microsoft.SqlServer.Types from Nuget than the locally installed one.
You can install a matching version using Nuget to resolve the issue, or you may be able to use binding redirects if that is not an option.
For example:
Install-Package Microsoft.SqlServer.Types -Version 10.50.1600.1
Check your specific versions by looking at package.json for your dependencies, or perhaps you can check the DLL properties directly.
The runtime uses the following steps to resolve an assembly reference:
Determines the correct assembly version by examining applicable configuration files, including the application configuration file, publisher policy file, and machine configuration file. If the configuration file is located on a remote machine, the runtime must locate and download the application configuration file first.
Checks whether the assembly name has been bound to before and, if so, uses the previously loaded assembly.
Checks the global assembly cache. If the assembly is found there, the runtime uses this assembly.
Probes for the assembly using the following steps:
If configuration and publisher policy do not affect the original reference and if the bind request was created using the Assembly.LoadFrom method, the runtime checks for location hints.
If a codebase is found in the configuration files, the runtime checks only this location. If this probe fails, the runtime determines that the binding request failed and no other probing occurs.
Probes for the assembly using the heuristics described in the probing section. If the assembly is not found after probing, the runtime requests the Windows Installer to provide the assembly. This acts as an install-on-demand feature.
Note There is no version checking for assemblies without strong names, nor does the runtime check in the global assembly cache for assemblies without strong names.
Now, check to see if you have multiple assemblies referenced (as Damien_The_Unbeliever said in the comment) or you do not have specific version set for that assembly.
Also you can try the Assembly Binding Log Viewer (Fuslogvw) to see exactly what gets loaded and what are the search paths.
Try using this in your web.config file:
<dependentAssembly>
<assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="10.0.0.0-11.0.0.0" newVersion="14.0.0.0" />
</dependentAssembly>

Adding MetadataReference in Roslyn Vs Microsoft.CodeAnalysis

Roslyn version 1.2.* has a function called MetadataReference.CreateAssemblyReference() which takes the display name of the assembly and returns the appropriate MetadataReference object. For example I was able to add reference to various assemblies as follows:
Compilation compilation = Compilation.Create("HelloWorld")
.AddReferences(MetadataReference.CreateAssemblyReference("mscorlib"),
MetadataReference.CreateAssemblyReference("System.Linq"),
MetadataReference.CreateAssemblyReference("System.Data.Linq"),
MetadataReference.CreateAssemblyReference("System.Data"),
MetadataReference.CreateAssemblyReference("System.Data.DataSetExtensions"),
MetadataReference.CreateAssemblyReference("System.Xml"),
MetadataReference.CreateAssemblyReference("System.Xml.Linq"),
MetadataReference.CreateAssemblyReference("System"),
MetadataReference.CreateAssemblyReference("System.Core")
//MetadataReference.CreateAssemblyReference("System.Core"),
/*MetadataReference.CreateAssemblyReference("System")*/)
.AddSyntaxTrees(tree);
This however does not seem possible with the Microsoft.CodeAnalysis package (this is the latest package that one can install from Nuget). This package has a few functions inside MetadataReference - but they either require an Assembly or a file path.
Does that above mentioned simpler function exist in newer compiler packages?
You can load the assembly with the CLR loader and find out where it was loaded from:
typeof(DataSetExtensions).Assembly.Location

Categories