How to install .NET Framework 4.7 in Visual Studio 2017

Microsoft just released .NET Framework 4.7, but surprisingly it is not the easiest to install.

Here are the steps for Windows 10, as of this writing, it is not available for earlier versions for of Windows.

1. Install Windows 10 Creators Update

Go here https://www.microsoft.com/en-us/software-download/windows10, download and install. This will take a while and will reboot your computer a few times.

2. Install Visual Studio 2017 Updates

In Visual Studio, go to Tools -> Extensions and Updates, install any available updates.

(You might have the option here to install 4.7, see the image below in step 4.)

3. Open Visual Studio 2017 Installer

Use the Start Menu to search for the Visual Studio Installer.

In the installer, click Modify on the version of Visual Studio you have installed.

4. Install .NET framework 4.7

Click .NET desktop development.

On the right select .NET Framework 4.7 and finally install.

Locating and checking an executing DLL on a running web server or other application

Edit – even though the steps described below related to an example with IIS, you can use this technique for any running process, it’s just a matter of identifying the process id. In some cases you will not even need the process id, e.g. if all processes are using the same version of a DLL.

Introduction

Figuring out what dll is actually executing on a running web server is no easy task; it’s not as simple as looking in the bin directory of the deployed application as the dlls are copied from there to a set of temporary directories within the windows systemroot.

I know of two ways of figuring this out, the first requires some code changes and the second does not.

I wrote some time ago about how to find which dll is really executing when running an application, it works just fine if you can alter the source code to include the snippet I proposed.

Clearly that is not a lot of use in a deployed application when you cannot change the code or even redeploy.

No code change needed

Here is the alternative, no code changes needed. The application I am interested in is called MyFancyApp, there are other applications running on the server too. Let’s say that the dll I am interested in is AutoMapper.dll, I need to verify that the running dll is really the version I expect.

1. Open a command prompt and navigate to %systemroot%\System32\inetsrv.
2. Execute appcmd list wp, this will show all the running application pools and the associated process ids. The output will look something like this –
find dll appcmd

You can see that MyFancyApp is running with process id of 1224.

Now switch to Process Explorer.

3. Open Process Explorer and hit CTRL-F, type in AutoMapper.dll. In the results PID 1224 is shown, in fact it will be there a few times. At this point the file location of the dll is visible.
find dll search

4. Double click one of the entries for PID 1224 where the Type is DLL.

5. In the lower pane of Process Explorer the AutoMapper.dll is selected and the version is shown, I had to right click on the lower pane and the version column to make it visible.

find dll results

That’s it. No redeploy, no code editing, no remote debugging. You now know for sure which dll is running and where to find it. If the dll is your own and you are concerned about what might be in the code you can open the file in dotpeek to decompile it.

Cached nuget packages causing you problems?

Short version
If you are having problems with nuget packages and think it might be related to local caching delete packages from these directories –

%LocalAppData%\NuGet\Cache
and
%userprofile%\.nuget\packages\

Long version
I was making nuget package, lets call it Foo, for my own use, not one that I was going to distribute publicly, but I was putting in on MyGet.

I had a few classes in it and after making the nuget package, I uploaded it to MyGet. I then switched to the Business solution and referenced this newly created nuget package.

After doing a little bit of work, I noticed that I had incorrectly made a class internal rather than public in the Foo nuget package.

No problem, back to the Foo solution where I was making the library, change the class to public, rebuild, and remake the nuget package. I didn’t want to increment the version number, so I left it at 1.0.0. On MyGet I deleted the old package and then uploaded the new one.

Here’s what I tried and didn’t work –

  • In the Business solution referencing the Foo nuget package, I uninstalled Foo and then reinstalled it.
  • But Visual Studio still reported that the class was inaccessible!
  • Uninstall and reinstall again, same problem!!
  • Delete the Foo directory from the packages directory beside the solution file, remove the Foo line from the packages.config. Reinstall Foo. Same problem!!!
  • Recomplie Foo, delete Foo from MyGet, reupload Foo to MyGet. Reinstall Foo. Still inaccessible!!!!
  • WTF!!!!!

And this is what works –

  • Download the Foo nuget package directly from MyGet and reference it directly. Success! There must be a local cache that Visual Studio is using!!!!!!

Yes there are, I found two –

%LocalAppData%\NuGet\Cache
and
%userprofile%\.nuget\packages\

Delete the offending nuget package; back in Visual Studio I can now successfully install the nuget package.

In general avoid using the same package version number when uploading a new package to MyGet.

Finding the Location of a Running Assembly in .Net

For many reasons you might want to know where a running assembly is located on your filesystem.

I had to do this once when I was compiling classes at runtime and had a dependency on EntityFramework. Another reason might be verifying which dll is actually being used in an application.

Here’s the simple method to perform the search –

    private string[] GetAssemblyLocation(string[] assemblyNames)
    {
        string [] locations = new string[assemblyNames.Length];
 
        for (int loop = 0; loop <= assemblyNames.Length - 1; loop++)       
		{
             locations[loop] = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && a.ManifestModule.Name == assemblyNames[loop]).Select(a => a.Location).FirstOrDefault();
        }
        return locations;
    }

And this is how you call it –

	string[] assemblyLocations = GetAssemblyLocation(new string[] { "System.Web.dll", "System.dll", "EntityFramework.dll" });

Saving enums as strings with Entity Framework

Full source code here.

I hit a problem where I wanted to use Entity Framework to save the text value of an enum to the database rather than its numeric value. This is not possible with Entity Framework at the moment; there are a few hacky solutions out there. I add my own hacky solution here.

I have a Person class with an ID, name and gender; the gender is an enum with just male and female. I’d like to save “Male” or “Female” to the database instead of 1 or 2.

The enum is simple.

    public enum Gender
    {
        Male = 1,
        Female = 2
    }

The Person class makes use of Data Annotations to perform the correct mappings to the table. The Gender property is not mapped to the database while the GenderString is mapped as the column named Gender.

    public class Person 
    {
        public int PersonID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        [Column("Gender")]
        public string GenderString
        {
            get { return Gender.ToString(); }
            private set { Gender = value.ParseEnum<Gender>(); }
        }

        [NotMapped]
        public Gender Gender { get; set; }
    }

I use an extension method to parse the text value of gender back into the enum.

    public static class StringExtensions
    {
        public static T ParseEnum<T>(this string value)
        {
            return (T)Enum.Parse(typeof(T), value, true);
        }
    }

Rows in the table will now have the string value of the enum rather than that number.

PersonIDFirstNameLastNameGender
1JamesSmithMale
2JaneSmithFemale

And when a person is loaded from the database the Gender enum is correctly populated.

The only drawback is that there is now a public property called Gender and GenderString on the Person class, but I have made the set of GenderString private to prevent accidental updating.

Full source code here.

Dotclear blog editing problem

The dotclear blogging engine has some issues when including pre-formated html; it removes all leading spaces and mangles empty lines.

This is a significant problem when posting nicely coloured code snippets like those produced by hilite.me or the SyntaxHilighter in WordPress.

A small hello world app ends up looking like this –

public class Hello1
{
public static void Main()
{
System.Console.WriteLine("Hello, World!");
}
}

Instead of this –

public class Hello1
{
   public static void Main()
   {
      System.Console.WriteLine("Hello, World!");
   }
}

Ahhh, much more readable. My way of solving this requires sed, so you’ll need a Linux installation, cygwin or Windows version of the command.

My hilite.me CSS looks like this –
border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;font-size:9px

Create a file called SedFixesForDotclear.sed.
Its content is

s/^$/<br/>/g #replace blank lines with <br/>
s/t/    /g #replace tabs with spaces - then the next line replaces the spaces
:l s/^((&nbsp)*) /1&nbsp/;tl #replace all leading spaces with &nbsp

Run the command –
sed -f SedFixesForDotclear.sed input.html > output.html

Paste the output file into your post.