AutoMapper non-static conversion from static - MapperConfiguration, CreateMapper - c#

I'm moving from a mixture of v2 & v4 AutoMapper static coding on .NET Framework 4, to version 6.0.1 using .NET Framework 4.6.2. I am running into problems in my conversion development under Visual Studio 2015.
var dummy = new MapperConfiguration(cfg => cfg.CreateMap<DateTime,string>());
Visual Studio tells me that MapperConfiguration.MapperConfiguration(Action<IMapperConfigurationExpression> configure) (+1 overload)
Constructor 'MapperConfiguration' has 0 parameter(s) but is invoked with 1 argument(s).
When I code the next line:
var dmy1 = dummy.CreateMapper();
Visual Studio tells me that IMapper MapperConfiguration.CreateMapper() (+1 overload) Cannot resolve symbol CreateMapper.
This is all basic non-static stuff, and I could use another pair of eyes giving me a heads up on what I'm missing. I'm thinking once I solve the problem with the first line of code, the other problem will go away. Ideas?

The problem goes away when I restart Visual Studio. There must have been something residual in the VS instance causing problems.

I has the same problem after Edmx update. The problem goes away after Visual Studio restart.

Related

public static class Contract && .net core 3.1 && macOS

how do I make Contract work on macOS?
the following code
private static int GetInt(string s)
{
Contract.Requires<ArgumentNullException>(s != null);
return 10;
}
static void Main()
{
Console.WriteLine(GetInt("s"));
}
leads to the exception:
An assembly (probably "TestConsoleApp") must be rewritten using the code contracts binary rewriter (CCRewrite) because it is calling Contract.Requires and the CONTRACTS_FULL symbol is defined. Remove any explicit definitions of the CONTRACTS_FULL symbol from your project and rebuild. CCRewrite can be downloaded from http://go.microsoft.com/fwlink/?LinkID=169180. \r\nAfter the rewriter is installed, it can be enabled in Visual Studio from the project's Properties page on the Code Contracts pane. Ensure that "Perform Runtime Contract Checking" is enabled, which will define CONTRACTS_FULL.
it's 2020 today, I work with dot.net core on my mac, but
the link from above is for downloading windows installer that was implemented 5 years ago in 2015 and works with Visual Studio 2010, 2012, 2013, 2015.
And I just wonder if public static class Contract is something that Microsoft uses internally or obsolete or something...
I hope someone can shed some light on the matter, can't you? ))
you were asking "I just wonder if public static class Contract is something that Microsoft uses internally or obsolete or something".
As per my understanding, it's unfortunately obsolete (it used to work with VS 2015 as you pointed out, provided you installed an extension).
See:
Does Visual Studio 2017 work with Code Contracts?
You could perhaps try PostSharp's version of Code Contracts?
I hope that MS will continue support of code contracts sometime in the future.

Visual Studio 2017 publish ASP.NET Core app with C# 7.2

I have a Asp.Net MVC Core website that's using public static async Task Main(). For that to work I've set the language version to C# 7.2 (in the properties -> build -> advanced dialog, double checked in the csproj) for both Debug and Release build configurations.
App builds and starts fine in both Debug and Release mode.
Now, I'm trying to publish it to an Azure Website directly from Visual Studio 2017 15.5.2 (with WebDeploy) and I get this:
Program.cs(17,29): Error CS8107: Feature 'async main' is not available
in C# 7.0. Please use language version 7.1 or greater. CSC(0,0): Error
CS5001: Program does not contain a static 'Main' method suitable for
an entry point
In the output window I see it's running C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Roslyn\csc.exe with some flags, probably one of them is wrong?
Anyone know if this is a known issue or I'm doing something wrong?
This appears to be a bug in Visual Studio.
Adding this line to main property group in the .csproj file resolved the issue for me:
<LangVersion>latest</LangVersion>
The issue was also reported here in the ASP.NET Home repository.
Not an answer, per se, but for what it's worth async Main is just syntactic sugar. Behind the scenes Roslyn just adds the standard void Main wrapper construction:
static void Main(object[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}
static async Task MainAsync(object[] args)
{
// your code
}
It's probably not worth your time trying to get the server on the same page C# version-wise, just to save literally three lines of code.

Visual Studio Online CI daily builds fail since switching to VS 2015 and using C# 6 Roslyn features

Since switching from VS 2013 to VS 2015 and using some new C# 6 features, our daily builds in Visual Studio Online have begun failing.
The errors on the build are all pointing to the new auto property feature but I assume all new features will cause this.
An example piece of code that causes a failure is using:
public int MyFavouriteNumber { get; set; } = 7;
instead of
private int _myFavouriteNumber = 7;
public int MyFavouriteNumber
{
get { return _myFavouriteNumber; }
set { _myFavouriteNumber = value; }
}
I've had a look around my build configuration, but I can't see anything that relates to C# 6 or Roslyn.
What do I have to change to make my daily builds work again?
Edit
Here's an example error (they're all the same, for auto properties).
Models\Core\Bookings\BookingProduct.cs (29, 0)
Invalid token '=' in class, struct, or interface member declaration
Models\Core\Bookings\BookingProduct.cs (29, 0)
Invalid token '(' in class, struct, or interface member declaration
And here is the offending line:
public virtual IList<BookingPricingAddon> AddonsPricing { get; set; } = new List<BookingPricingAddon>();
TheLethalCoder's comments pointed me in the right direction.
The problem was that all of my projects were using Default as the target Language Version, which is fine if you're using VS 2015, however, my .sln file had the following opening 3 lines:
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
Apparently, Visual Studio Online uses this to work out which version of MSBuild to use. (It was using 12).
Upgrading my solution to the following:
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
allowed Visual Studio Online to see that my IDE was using Roslyn, and therefore used MSBuild 14.
The easiest way I found to upgrade is to click on the solution in Solution Explorer and then going to File > Save As > Solution File and overwrite your existing solution file, it just upgrades the first 3 lines.
My builds are now successful.
I think the same could be achieved by setting the Language Version on each of your projects. This can be done by going to your .csproj files and navigating to:
Properties > Build > Advanced > Language Version > C# 6.0
A good reference about the Default settings in each IDE can be found here.

The var keyword not showing up in visual studio

I've installed a new instance of visual studio 2010 premium and everything seems to work fine when I load old projects. When I start to write new classes though, the var keyword is not working or showing up in intellisense. This is a new solution and no web project. (so no web.config) Target framework for the project is set to .net 4.0. When I try compiling it by writing
var x = "this";
I get "A get or set accessor expected" error.
Do I need to reinstall? Any ideas what could be wrong here?
You don't have parentheses after your method name so the compiler thinks you're defining a property.
public void Server_Test()
{
var ...
}

Web Api Content.ReadAsAsync method not found, but code compiles

I am using System.Net.Http.HttpClient to access a rest service. This code compiles and runs just fine:
var client = new HttpClient();
var result = client.GetAsync(ServiceUrl + "/whatever").Result;
var content = result.Content.ReadAsAsync<StatusReport>().Result;
However the ReadAsAsync method is colored red in my IDE, and intellisense cannot find it. I have made sure to update all of my nuget packages. Referenced and added using statement for System.Net.Http.Formatting, but error persists (and resharper tells me the using statement is unused).
I am not sure if this is a problem with visual studio 2012, or with resharper 7. Sometimes restarting visual studio helps, and sometimes it doesn't. I suspect I may have some older version of some assembly referenced, or something like that, but I have updated everything I know how and the problem is still intermittent.
What else can I try?
You must add reference from Nuget "ASP.NET Web API 2 Client". Microsoft.AspNet.WebApi.Client
Try clearing your resharper cache. (ReSharper > Options > Environment > General > Clear Caches)
HttpClient is new in 4.5 AFAIK and maybe your assemblies are cached from 4.0 or something?

Categories