'Catel.IoC.IServiceLocator' does not contain a definition for 'Instance' - c#

I'm quite new to .NET development and Catel but is working on a project to learn it. At the moment the original developer is not present so I'll try my luck here instead.
To implement Undo/Redo the Memento Pattern support of Catel seems great so giving it a try but I have problems getting an instance of the MementoService. When I do in my ViewModel:
var mementoService = ServiceLocator.Instance.ResolveType<IMementoService>();
I get:
'Catel.IoC.IServiceLocator' does not contain a definition for 'Instance' and no extension method 'Instance' accepting a first argument of type 'Catel.IoC.IServiceLocator' could be found (are you missing a using directive or an assembly reference?)
I have noticed that when I do the same in my Model though I do not get this problem. Seems strange that I would need different ways of getting an instance of the service depending on where I try to get it.
I do not understand what I'm doing wrong. The NuGet Package Manager Console tells me I have Catel.Extensions.Memento 3.7 installed as well as Catel.Core, Catel.MVVM and Catel.Extensions.Controls 3.7. I'm using Visual Studio 2013 with .NET 4.0.

The interface does not have an Instance property, and it is obsolete.
You should use this:
1) In a static context:
Catel.IoC.ServiceLocator.Default
2) In a non-static context (takes care of scopes automatically):
var serviceLocator = this.GetServiceLocator();

Related

HostingEnvironment.QueueBackgroundWorkItem method is missing

I am working in .NET framework 4.8 (correction: my Visual Studio installation was 4.8, the application was targeting 4.5 - this was the source of the problem, which is now solved) and trying to use the QueueBackgroundWorkItem method of the HostingEnvironment class, but when I write it into my code, I get this message:
BC30456: 'QueueBackgroundWorkItem' is not a member of 'HostingEnvironment'.
I have used Imports System.Web.Hosting at the top of my script. System.Web.Hosting imports OK and the HostingEnvironment class shows up highlighted in my code, as it should. The class HostingEnvironment is recognised and has methods, but QueueBackgroundWorkItem isn't one of them.
I get a similar error when I try to use this method in C#.
CS0426: The type name 'QueueBackgroundWorkItem' does not exist in the type 'HostingEnvironment'.
I've searched for the System.Web.Hosting library in NuGet to see if there were any updates, but nothing came up. Google searches for these errors haven't got me anywhere, other than to links explaining how to use the HostingEnvironment method.

SetBasePath not present in ConfigurationBuilder under .NET Core 5

According to the docs for .NET Core 5, there's a method SetBasePath and it's widely used in a bunch of blogs (example 1, example 2, example 3 etc.). There's no notion of it being a weird gotcha or such. However, when I try the syntax below, it's marked red and claimed not to be there.
using System;
using Microsoft.Extensions.Configuration;
static void Main(string[] args)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
IConfigurationBuilder builder = new ConfigurationBuilder();
builder.SetBasePath(path);
}
I'm not sure why this happens not what to do about it.
Steps to reproduce:
Create a vanilla console application in .NET Core 5 in C#.
Paste in the code in Program.cs replacing the file's contents.
Try to compile or execute.
The error received is like this.
Error CS1061
'ConfigurationBuilder' does not contain a definition for 'SetBasePath' and no accessible extension method 'SetBasePath' accepting a first argument of type 'ConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?)
As far i can understand, I have all the prerequisites in place.
Remember that multiple types can contribute methods to a single type through Extension Methods. And so SetBasePath was never a method on the IConfigurationBuilder interface.
And multiple Assemblies can contribute types to the same namespace, and the docs say the type that defines the SetBasePath extension method is in:
FileConfigurationExtensions Class Definition Namespace:
Microsoft.Extensions.Configuration Assembly:
Microsoft.Extensions.Configuration.FileExtensions.dll
FileConfigurationExtensions Class
Which you can easilly verify is not present in your dependencies. So you're missing a NuGet package. Turns out this one is easy to find: Microsoft.Extensions.Configuration.FileExtensions

How can I use TimeZoneInfo.GetAdjustmentRules() in .net core?

I'm writing some codes to deal with time zones, so naturally, I'm already having a bad day. In particular, I've really be struggling to get the DST adjustment rules for time zones. According the source code for TimeZoneInfo, there should be an instance method called .GetAdjustmentRules(). It was written early in 2015.
However, this console app doesn't compile targeting .net standard 1.6.
using System;
public class Program {
public static void Main() {
var tz = TimeZoneInfo.Local;
Console.WriteLine(tz.GetAdjustmentRules());
}
}
CS1061 'TimeZoneInfo' does not contain a definition for 'GetAdjustmentRules' and no extension method 'GetAdjustmentRules' accepting a first argument of type 'TimeZoneInfo' could be found (are you missing a using directive or an assembly reference?) timezonecore..NETCoreApp,Version=v1.0
In fact, when I retrieve a list of methods by reflection, I find that are none called .GetAdjustmentRules(). Interestingly, there is a private member called m_adjustmentRules, that I might be able to use instead.
But where is .GetAdjustmentRules()? If there's no way to use it, is there another way to get this information?
The "trick" is you don't directly reference mscorlib, you reference System.Runtime that is pulled in by dependencies of Microsoft.NETCore.App. The purpose of System.Runtime is to abstract away what actually implements all the System.* objects. The CoreCLR has implementations in mscorlib and the calls are forwarded to it. the CoreRT runtime for example has implementations in System.Private.CoreLib (some of which are pure C# implementations that are C++/native method calls in CoreCLR).
So to determine what you can actually call on TimeZoneInfo, this is the place to look for 1.0:
https://github.com/dotnet/corefx/blob/release/1.0.0/src/System.Runtime/ref/System.Runtime.cs
Both the branches release/1.0.0 and release/1.1.0 don't contain a definition of GetAdjustmentRules() but the master branch does so you can expect that method to be available in .NET Core 1.2.
The method will also be available when just targeting netstandard2.0 for libraries using NETStandard.Library. The reference for what will be available in netstandard2.0 is currently here:
https://github.com/dotnet/standard/tree/master/netstandard/ref

Unknown namespace during build (but intellisense knows it)

I am currently working on a project with SignalR. On my production server, it is missing an automatically generated js-file in ~/signalr/hubs.
I wanted to try this solution:
SignalR not working on production server
But when I try to add this line to my application_start method in global.asax, my project will not build anymore. Although intellisense knows and suggests the extension method!
I referenced the namespace with using SignalR;. I tried calling it as an extension method and calling it as a static method with the object instance as the first parameter.
The exact error messages:
When calling as extension method:
Error 1 'System.Web.Routing.RouteCollection' does not contain a definition for 'MapHubs' and no extension method 'MapHubs' accepting a first argument of type 'System.Web.Routing.RouteCollection' could be found (are you missing a using directive or an assembly reference?)
When calling as static method:
Error 1 The type or namespace name 'RouteExtensions' does not exist in the namespace 'SignalR' (are you missing an assembly reference?)
Anybody knows why this happens and how to solve the problem? Many thanks in advance!
I'm pretty sure that it could be one of these reasons:
Your project requires a reference to an assembly that's not added to the project.
Is your project using the .NET Framework version that's compatible with the referenced assembly?
With your given info, it would be hard to determine the exact reason, but I believe that I provided you some helpful hints.
The fact that intellisense suggests you anything like an extension method won't mean that the project has the required references to work properly.

Resolving extension methods/LINQ ambiguity

I'm writing an add-in for ReSharper 4. For this, I needed to reference several of ReSharper's assemblies. One of the assemblies (JetBrains.Platform.ReSharper.Util.dll) contains a System.Linq namespace, with a subset of extension methods already provided by System.Core.
When I edit the code, it creates an ambiguity between those extensions, so that I cannot use OrderBy, for instance. How could I solve this? I would like to use the core LINQ extensions, and not the ones from ReSharper.
I get the following error when trying to compile:
The call is ambiguous between the
following methods or properties:
'System.Linq.Enumerable.OrderBy<string,int>(System.Collections.Generic.IEnumerable<string>,
System.Func<string,int>)' and
'System.Linq.Enumerable.OrderBy<string,int>(System.Collections.Generic.IEnumerable<string>,
System.Func<string,int>)'
EDIT: I tried the suggestion below, unfortunately without luck. In the meanwhile, I "solved" the problem by removing references to System.Core. This way I could use the extensions provided by ReSharper DLL files.
I uploaded a sample program where I just imported the ReSharper DLL files I needed. I changed the alias of System.Core to SystemCore, added the extern alias directive, but it still didn't work. If I missed something, please let me know.
P.S. The references are to ReSharper v4.1 DLL files installed in the default directroy in "C:\Program Files\JetBrains\ReSharper\v4.1\...".
This is probably one of those rare cases where it makes sense to use an extern alias.
In the properties page for the reference to System.Core (i.e. under References, select System.Core, right-click and select "Properties"), change the "Aliases" value to "global,SystemCore" (or just "SystemCore" if it's blank to start with).
Then in your code, write:
extern alias SystemCore;
using SystemCore::System.Linq;
That will make all the relevant types etc in System.Core.dll's System.Linq namespace available. The name "SystemCore" here is arbitrary - you could call it "DotNet" or something else if that would make it clearer for you.
This isn't really an answer, but may provide an easier way for others to reproduce the issue (from the command-line - you could do it with two projects in Visual Studio if you want).
1) Create BadLinq.cs and build it as BadLinq.dll:
using System.Collections.Generic;
namespace System.Linq
{
public static class Enumerable
{
public static IEnumerable<T> Where<T>(this IEnumerable<T> source,
Func<T,bool> predicate)
{
return null;
}
}
}
2) Create Test.cs:
extern alias SystemCore;
using System;
using SystemCore::System.Linq;
static class Test
{
static void Main()
{
var names = new[] { "Larry", "Curly", "Moe" };
var result = names.Where(x => x.Length > 1);
}
}
3) Compile Test.cs specifying the extern alias:
csc Test.cs /r:BadLinq.dll /r:SystemCore=System.Core.dll
This fails with:
Test.cs(11,28): error CS1061:
'System.Array' does not contain a
definition for 'Where' and no
extension method 'Where' accepting a first argument of type
'System.Array' could be found
(are you missing a using directive or an assembly reference?)
If you change it to not try to use an extension method (i.e. Enumerable.Where) it works fine with the extern alias.
I think this may be a compiler bug. I've emailed a private mailing list which the C# team reads - I'll update this answer or add a new one when I hear back.
This is no longer an issue, since I am able to use the LINQ extensions, as provided by ReSharper DLL files, even while targeting .NET 3.0.
Mr. Skeet was right again! I am able to use full LINQ syntax, while targeting .NET 3.0 in the project's properties and not referencing System.Core!
In order for ReSharper to be as compatible as possible with the variety of solutions it is used with, it is built against .NET 2.0. LINQ, etc. came in in C# 3.0, so they are not available in that version of the Framework. So, JetBrains added in their own version.
The solution is to build your addin against .NET 2.0 as well.
I had the ambiguous reference problem using System.ComponentModel. Visual Studio was complaining that a DLL file exists in both v2 and v4. I was able to resolve it by removing the reference to the System DLL file and readding it.
One solution would be to move all your code out to a partial class that uses the ReSharper code. In there, you'd import only the ReSharper namespace and not System.Core.
In the rest of the partial class, you'd import all the other namespaces you need, including System.Core, but not the ReSharper namespace.
I had the same problem, even with extern alias, and I raised it as a compiler bug on Connect. The workaround for the time being is to forgo the extension method syntax.
The bug is fixed for Visual Studio 2010.
It is really a compiler error.
I had the same problem, and I solved it just by cleaning and rebuilding the project. After that the problem disappeared.
I had a similar situation. After two hours struggling I realized I had duplicate namespace names in my libraries. If you are using file Dynamic.cs published by Microsoft, the only thing you need to do is rename the current namespace to something else, and it will be fixed.
//Copyright (C) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
namespace System.Linq.Dynamic <- for example to Linq.Dynamic
{
I found this same sort of ambiguity when use PagedList in MVC (.Net 4.5, MVC 5). I found that if I took the object for the argument that was ambiguous and cast it explicitly first, the problem was resolved. To if the ambiguity was between a method that takes System.Linq.Enumerable and one that takes System.Collections.Generic.IEnumerable as the parameter in question, and the source is of type System.Collections.Generic.IEnumerable, I don't use the extension method on it. I cast it.In this example, my repository method returns a List:
searchRequest.CaseSearchResults = csr.SelectMatchingCases(searchRequest);
var results = searchRequest.CaseSearchResults.AsEnumerable<CaseSearchResult>();
int pageNum = (int)(ViewBag.PageNum ?? 1);
var pageResults =results.ToPagedList<CaseSearchResult>(pageNum, 5);
Calling the extension method on searchRequest.CaseSearchResults caused the ambiguity error; explicitly casting to results and then calling the extension on that worked.

Categories