Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Contents tagged with Visual Studio

  • Could not load file or assembly 'System.Web.Http.WebHost'

     

    We ran into this error when deploying a solution from the build server:

    Could not load file or assembly 'System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    Since this was a new web server and this solution used Nuget (and Nuget Package Restore) for Web Api, we assumed the Nuget packages needed to be updated but whatever type of updating we tried, we were unable to resolve the issue on the deployed web server.  After all sorts of checking and confirming of installed packages and assemblies, we just deleted the packages folder on the build server (the one created by Nuget Package Restore):

    image

    And then a fresh build solved the problem!

    We are not exactly sure what happened here but most likely Nuget Package Restore had pulled down a newer version of the System.Web.Http.WebHost assembly that was not compatible with our project.  But we are not exactly sure why the Visual Studio project (MSBuild) kept trying to use the newer version instead of the proper version for the project reference.  This project had been upgraded to .NET Framework 4.5 several weeks ago and then rolled back to .NET Framework 4.0 because of a different issue.  This could explain where the incorrect version of System.Web.Http.WebHost came from but why Nuget Package Restore kept the wrong one is unknown and why the project kept building with the incorrect assembly is unknown too. 

    In any case, for anyone that runs into the same issue, delete your packages folder and let Nuget Package Restore pull a fresh set of assemblies.

     

    Full Exception information:
        Exception type: HttpException
        Exception message: Could not load file or assembly 'System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
       at System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app)
       at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers)
       at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context)
       at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context)
       at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)

    Could not load file or assembly 'System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
       at Global.Application_Start(Object sender, EventArgs e)

  • Microsoft Build (MSBuild) 12

    Did you know that you can install MSBuild to your build and deployment server individually without needing to install Visual Studio?

    You can get MSBuild 12 – aka Microsoft Build Tools 2013 – from http://www.microsoft.com/en-us/download/details.aspx?id=40760.

    Downloading and installing will give you MSBuild.exe in C:\Program Files (x86)\MSBuild\12.0\Bin

    image

    The 64-bit version is located in C:\Program Files (x86)\MSBuild\12.0\Bin\amd64.

  • Update Nuget.exe in.nuget package restore folder

    When upgrading to the latest version of MVC with a build server using Nuget package restore, you may get these errors indicating that Nuget is out of date:

    The schema version of 'Microsoft.AspNet.Mvc' is incompatible with version 2.2.31210.9045 of NuGet. Please upgrade NuGet to the latest version from http://go.microsoft.com/fwlink/?LinkId=213942.
    The schema version of 'Microsoft.AspNet.WebPages' is incompatible with version 2.2.31210.9045 of NuGet. Please upgrade NuGet to the latest version from http://go.microsoft.com/fwlink/?LinkId=213942.

    The links in those errors only point you to the Nuget home page which is confusing because it does not really tell you how to update Nuget.

    From this StackOverflow answer, you can use the Nuget.exe executable and tell it to update itself.  To do this open a command window in the folder where you have the old version of Nuget.exe and execute the command:

    nuget.exe update –self

    image

    Nuget.exe will start, check for the latest version, and update itself if there is a newer version:

    image

  • Web Application Project is configured to use IIS. Unable to access the IIS metabase.

    On a new install of Visual Studio 2013 on Windows Server 2012 R2 I ran into the following error when trying to open a Visual Studio Web Application project:

    image

     

    Even though I am an administrator on the machine, Visual Studio is not running as administrator so it does not have permission to the IIS metabase files.  One solution is to run Visual Studio as administrator.  This works but you can also take ownership of the IIS metabase files to fix this too. 

    Browse to C:\Windows\System32\inetsrv and then double-click the “config” directory to get this warning dialog:

    image

    Do the same for the C:\Windows\System32\inetsrv\config\export directory and also the other directories in C:\Windows\System32\inetsrv.

    Now you will be able to open the Visual Studio Web Application Project without a problem.

    ---------------------------
    Microsoft Visual Studio
    ---------------------------

    The Web Application Project is configured to use IIS. Unable to access the IIS metabase. You do not have sufficient privilege to access IIS web sites on your machine.

    ---------------------------
    OK
    ---------------------------

  • Reference assemblies for .NET Framework v4.5 were not found

    Setting up a build server on a new install of Windows Server 2012 R2 and ran into the following warning when building the projects:

    Warning MSB3644 - The reference assemblies for framework ".NETFramework,Version=v4.5" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.

    The build server was a fresh machine and the issue was Visual Studio 2013 needing to be installed on the server.  Installing Visual Studio Professional 2013 without any options resolved the issue.

    • Under optional Features uncheck all the options to speed up the install.
      • image
  • VB.NET IF() Coalesce and “Expression Expected” Error

    I am trying to use the equivalent of the C# “??” operator in some VB.NET code that I am working in.

    This StackOverflow article for “Is there a VB.NET equivalent for C#'s ?? operator?” explains the VB.NET IF() statement syntax which is exactly what I am looking for... and I thought I was going to be done pretty quickly and could move on.

    But after implementing the IF() statement in my code I started to receive this error:

    Compiler Error Message: BC30201: Expression expected.

    image

    And no matter how I tried using the “IF()” statement, whenever I tried to visit the aspx page that I was working on I received the same error.

    This other StackOverflow article Using VB.NET If vs. IIf in binding/rendering expression indicated that the VB.NET IF() operator was not available until VS2008 or .NET Framework 3.5.  So I checked the Web Application project properties but it was targeting the .NET Framework 3.5:

    image

    So I was still not understanding what was going on, but then I noticed the version information in the detailed compiler output of the error page:

    image

    This happened to be a C# project, but with an ASPX page with inline VB.NET code (yes, it is strange to have that but that is the project I am working on).  So even though the project file was targeting the .NET Framework 3.5, the ASPX page was being compiled using the .NET Framework 2.0.  But why?  Where does this get set?  How does ASP.NET know which version of the compiler to use for the inline code?

    For this I turned to the web.config.  Here is the system.codedom/compilers section that was in the web.config for this project:

    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5" />
                <providerOption name="WarnAsError" value="false" />
            </compiler>
        </compilers>
    </system.codedom>

    Keep in mind that this is a C# web application project file but my aspx file has inline VB.NET code.  The web.config does not have any information for how to compile for VB.NET so it defaults to .NET 2.0 (instead of 3.5 which is what I need).

    So the web.config needed to include the VB.NET compiler option.  Here it is with both the C# and VB.NET options (I copied the VB.NET config from a new VB.NET Web Application project file).

        <system.codedom>
            <compilers>
                <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                    <providerOption name="CompilerVersion" value="v3.5" />
                    <providerOption name="WarnAsError" value="false" />
                </compiler>
          <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <providerOption name="CompilerVersion" value="v3.5"/>
            <providerOption name="OptionInfer" value="true"/>
            <providerOption name="WarnAsError" value="false"/>
          </compiler>
        </compilers>
        </system.codedom>

     

    So the inline VB.NET code on my aspx page was being compiled using the .NET Framework 2.0 when it really needed to be compiled with the .NET Framework 3.5 compiler in order to take advantage of the VB.NET IF() coalesce statement.  Without the VB.NET web.config compiler option, the default is to compile using the .NET Framework 2.0 and the VB.NET IF() coalesce statement does not exist (at least in the form that I want it in).  FYI, there is an older IF statement in VB.NET 2.0 compiler which is why it is giving me the unusual “Expression Expected” error message – see this article for when VB.NET got the new updated version.

    EDIT (2011-06-20): I had made a wrong assumption in the first version of this blog post.  After a little more research and investigation I was able to figure out that the issue was in the web.config and not with the IIS App Pool.  Thanks to the comment from James which forced me to look into this again.

  • Unable to close Visual Studio 2008

    I ran into an issue with not being able to close Visual Studio 2008 today.  My Visual Studio 2008 solution opened and work fine but then when I tried to close it, Visual Studio would just freeze up.  Something was going on since the processor was saying about 25% usage or so and the mouse was blinking back and forth between the wait cursor and the default arrow but no matter how long I waited, Visual Studio 2008 would not close (it definitely looked like it was stuck in a loop doing something while trying to close down). 

    I was able to open and close another Visual Studio 2008 solution without any problems so this confirmed it was something specific to the this solution. 

    After trying a couple random things (such as closing all files, removing a reference, etc.) I tried deleting the Visual Studio Solution User Options .suo file.

    image

    Deleting the file solved the problem!

    So if you are ever in the same situation consider a corrupt .suo file to be the problem. 

  • Visual Studio Shortcut: Surround With

    I learned a new Visual Studio keyboard shortcut today that is really awesome; the “Surround With” shortcut. 

    You can trigger the Surround With context menu by pressing the Ctrl-K, Ctrl-S key combination when on a line of code.

    Ctrl-K, Ctrl-S means to hold down the Control key and then press K and then while still holding down the Control key press S.

    Here is where this comes in handy:

    You type a line of code and then realize you need to put it within an if statement block. So you type “if” and hit tab twice to insert the if statement code snippet.  Then you highlight the previous line of code that you typed, and then either drag and drop it into the if-then block or cut and paste it.  That is not too bad but it is a lot of extra key clicks and mouse moves.

    Now try the same with the Surround With keyboard shortcut.  Just highlight that line of code that you just typed and press Ctrl-K, Ctrl-S and choose the if statement code snippet, hit tab, and POW!... you are done!  No more code moving/indenting required.

    Here is what the Surround With context menu looks like:

    image

    Just up or down arrow inside the drop down list to the code snippet that you want to surround your currently selected text with.  Did I mention this is AWESOME!

    Now it is so simple to surround lines of code with an if-then block or a try-catch-finally block... things that usually took several key clicks and maybe one or two mouse moves.

    And this works in both Visual Studio 2008 and Visual Studio 2010 which means it has been around for a long time and I never knew about it.

     

  • Visual Studio confused when there are multiple system.web sections in your web.config

    I am trying to start debugging in Visual Studio for the website I am currently working on but Visual Studio is telling me that I have to enable debugging in the web.config to continue:

    image

    But I clearly have debugging enabled:

    image

    At first I chose the option to Modify the Web.config file to enable debugging but then I started receiving the following exception on my site:

    HTTP Error 500.19 - Internal Server Error

    The requested page cannot be accessed because the related configuration data for the page is invalid.

    Config section 'system.web/compilation' already defined. Sections must only appear once per config file. See the help topic <location> for exceptions

     

    So what is going on here?  I already have debug=”true”, Visual Studio tells me I do not, and then when I give Visual Studio permission to fix the problem, I get a configuration error.

    Eventually I tracked it down to having two <system.web> sections.

    I had defined customErrors higher in the web.config:

    image

    And then had a second system.web section with compilation debug=”true” further down in the web.config.  This is valid in the web.config and my site was not complaining but I guess Visual Studio does not know how to handle it and sees the first system.web, does not see the debug=”true” and thinks your site is not set up for debugging.

    To fix this so that Visual Studio was not going to complain, I removed the duplicate system.web declaration and moved the customErrors statement down.

  • How to change the Target Framework Version for a Visual Basic Project

    Today I received the following warning message from Visual Studio while adding a reference in a Visual Basic project:

    image

    The Target Framework version for the project ‘XXXX’ is higher than the current Target Framework version. 
    Would you like to add this reference to your project anyway?

    This warning message makes complete sense since the Visual Basic project I was working on is targeting the .NET Framework 2.0 and the project that was being referenced targeted the .NET Framework 3.5.  The only issue was that I was not able to figure out how to change the Target Framework Version in a Visual Basic project.

    In a C# project it is pretty easy; right-click on the project and choose properties and in the Application tab (vertical tab) you will see the Target Framework dropdown box:

    image

    But in Visual Basic it is hidden.  Here is where I found how to change the Target Framework version after searching for a bit:

    Right-click on the project and choose properties and then select the Compile tab (vertical tab).
    image

    At the bottom of the Project > Compile tab you will find an Advanced Compile Options… button.
    image

    In this dialog called the Advanced Compiler Settings is where you will find how to change the Target Framework version for Visual Basic.

    image