Saturday, September 24, 2016

Remote Debugging: "The breakpoint will not currently be hit. No symbols have been loaded for this document."

Problem:


When you are remotely debugging your breakpoints show a message that, "The breakpoint will not currently be hit. No symbols have been loaded for this document."

This post is written for a .NET project where you are debugging on one computer (the local machine) and the code is running in IIS on another server (DevServer in this example).

Solution:

Solution 1 - Ensure that PDB files are being generated for your build

On the remote server, navigate in file explorer to the bin folder for your project. This is usually located on the C: drive in inetpub\wwwroot\TheProject\bin. Ensure that there are .PDB files in this folder. If there are not, go back to Visual Studio and change your project's property's so that generated debug symbols are included.

To do this in Visual Studio 2015: Right click on your project and select Properties. Click "Package/Publish Web". Ensure "Exclude Generated debug symbols" is unchecked. You will most likely want to do this for the Debug and Release Configurations.


If you have done Option 1 and are still getting this error, go to Option 2.

Solution 2 - Ensure that you have the same version of code locally and on the remote server.

Before attaching to a process, look at the size of the DLL and PDB files if they are different sizes you may have slightly different versions. This can happen if the code was built using two different methods. For example if the code locally was built using Visual Studio but the code on the remote server was built with release management.

Go to Tools > Options > debugging > Symbols. Add a new Symbol file location for your build server.
Make it point to the bin folder of the project you want to debug. If you have multiple projects you want to debug, add each of their bin folders. It should look something like the screen shot below.

Now attach your debugger to the remote server and confirm that symbols have loaded for your project.

Solution 3 - Ensure you've attached to the right process

Using your browser, navigate to the website hosted on the remote server. This ensures that the process will be running in IIS. In Visual Studio, in the "Attach to Process..." window, find the w3wp.exe process. If there are multiple, try connecting to all of them, but you should be able to tell which one is the website you want. If your symbols are loaded correctly after trying this, you had the correct DLL and PDB files locally and on the remote server, the problem was that Visual Studio was connecting to the wrong process.

It should look similar to the below screen shot.


Solution 4 - Ensure local and Release Management / MSBuilds are generating identical files.

The other solution would be to modify your release management build to make your local build and release management build have the exact same settings so they generate identical DLL and PDB files. This will not be covered in this post because it is outside the scope of this post. However, see the "Other things to try" section below for instructions on manually copying DLL and PDB files from your local machine to the remote server, which would ensure the local and remote versions are the same. However, that is not a proper solution and should only be used for troubleshooting.

Explanation:

The DLLs and PDBs that are generated from a local build need to match the files on the remote server where the code is running. If they do not, or if the symbol files cannot be located by Visual Studio, this error will show up on your breakpoints.

Sometimes you'll build your project in Visual Studio and build that same project with release management or MSBuild, but the DLL and PDB files are different. This can be due to a difference in the build configurations for Visual Studio and release management.

Other things to try:

Manually copy DLL and PDB files from local machine to remote server.

Build the project on your local machine, make sure symbol files (.pdb) are included. To get these symbol files try building in Debug mode. If you still don't have symbol files, right click your project file, and under Package/Publish Web uncheck "Exclude generated debug symbols".
Now find you're project's bin folder in explorer. You should have a fresh set of DLL and PDB files in this folder. The "Date Modified" for any DLL and its matching PDB file should be about the same.


Now find the bin folder for this project on the remote server. It should be the same as what you added to the symbol file locations, so in our example it's \\DevServer\c$\inetpub\wwwroot\TheProject\bin.
Create a backup of this folder just for safe keeping.

Now overwrite the bin folder on the server by copying the bin folder from your local computer to the remote server. The purpose of this is to ensure you have the exact same files locally and on the remote server. This is not a good permanent solution, but if your breakpoints get hit now it proves that your remote debugging is configured mostly correctly and that the problem is a difference with the DLL and PDB files between your local machine and the remote server.

Symbol Status

While debugging, in Visual Studio go to Debug > Windows > Modules. This window can provide you with lots of information for troubleshooting. Look for the DLL of your project (TheProject.dll in our examples). If the Symbol Status shows "Symbols loaded." then things are working correctly.

You can right click on an entry here and click "Load Symbols" to point to a symbol file to use. If Visual Studio likes the symbol files you selected, the symbols will load and the error will go away.

Further Reading

https://blogs.msdn.microsoft.com/visualstudioalm/2015/01/05/understanding-symbol-files-and-visual-studios-symbol-settings/

Monday, August 22, 2016

IIS 7.0 Web Site STIG V-2267 - Request Filtering

Problem

After enabling Request Filtering in IIS for your .NET website, some areas of the application no longer work as expected. This is often done to put your website in compliance with STIG Rule ID SV-32335r3_rule also known as IIS 7.0 Web Site STIG V-2267.

Solution

Add .NET file extensions to the Request Filtering list. These extensions are found here: ASP.NET Web Project File Types.

Begin by adding the extensions under "File Types Managed by ASP.NET", then test the areas of your website that had previously not been functional. If these areas still do not work, try adding the file extensions under "Static File Types" and then "File Types Managed by IIS". Make sure to not add any extensions that are not allowed by the STIG.

It may be regarded as bad practice to add file types to the whitelist that you do not need, so try to only add what you need.

If you have third-party tools that are breaking, those extensions will have to be investigated and added to the whitelist, if appropriate and allowed. Fiddler with help with this, as I explain below in "Other Things to Try."

Explanation

You may think that to find all the file extensions needed for the whitelist, you should explore the files and folders of your website and add all file extensions you find to Request Filtering in IIS. This does not work because there are files that are generated by your app at runtime that have extensions that aren't found in the website's folders. For example, .axd files are a commonly forgotten file extension that will break some areas of your site if they are not included in the request filtering whitelist.

You also need to keep in mind that if you simply allow all file extensions from ASP.NET Web Project File Types, you may be in violation of the STIG. Remember, the point of STIG V-2267 is to limit script execution.

Other Things to Try

Use Fiddler. With Fiddler running, navigate to a page in your application that isn't working and look for 404 errors. These indicate requests for resources that were not found. One reason these files can't be found may be that IIS's request filtering is denying access to them. Look at any 404 errors you are getting and try adding those file types to the whitelist in IIS. For example here is what we would see if .axd files were being denied.

Fiddler results when .axd files are not in the whitelist. Click to Enlarge

Wednesday, August 10, 2016

Unable to start debugging on the web server. Unable to connect to the web server. Verify that the web server is running and that incoming HTTP requests are not blocked by a firewall.

Problem

When trying to debug  your solution or project, hosted on IIS Express, the following error occurs:
Unable to start debugging on the web server. Unable to connect to the
web server. Verify that the web server is running and that incoming
HTTP requests are not blocked by a firewall.

Solution

Change the project's Web settings to "IIS Express" instead of "External Host". This is under "Web" under the "Servers" section. Visual Studio should automatically fill in the Project Url.

Click the picture to enlarge.





Explanation

When a project's Web settings are set to an external server, Visual Studio will try to connect to that server to debug the project. This would be used if you were running IIS (not IIS Express) on your local machine and wanted Visual Studio to debug the app running on IIS.

Other things to try

  • Restart Visual Studio, which also restarts IIS Express
  • Run Visual Studio as Administrator.

Friday, January 22, 2016

"Cannot obtain value of the local variable or argument because it is not available at this instruction pointer, possibly because it has been optimized away."

Problem

While debugging in Visual Studio 2015, many variables are not available. When you try to see a variable's value, Visual Studio tells you, "Cannot obtain value of the local variable or argument because it is not available at this instruction pointer, possibly because it has been optimized away."

Solution

Change the "Solution Configurations" drop down to "Debug".








Explanation

According to Microsoft: "In Release, some locals will be optimized away and will not be available during debugging."




This is done for performance reasons. You can read Microsoft's excellent post on the issue here.

Other things to try

  • Uncheck Project Properties > Build > Optimize Code. (For Visual Studio versions prior to 2015)
  • "In Visual Studio 2015 this option is off by default but if you must debug an application compiled Release, you can turn it back on under Debug -> Options and check “Suppress JIT optimizations on module load (Managed only)”." - MSDN Blog

Wednesday, August 19, 2015

"Variable is accessed within inner class. Needs to be declared final."

Problem:

"Variable is accessed within inner class. Needs to be declared final." or a similar error occurs.

Solution:

Declare the variable final, or make it an instance variable.

Explanation:

Java doesn't want developers to change local variables from within an inner class or an anonymous inner class.

Inner Classes and Local Variables

Any variable defined in a method and accessed by an anonymous inner class must be final. Or, as Oracle says:
"An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final"
Note: "effectively final" is something new introduced in Java SE 8. It is defined as a variable or parameter that is not declared as final, whose value is never changed after it is initialized.

But why make it so inner classes can't modify variables belonging to their outer scope?
The reason is that the inner class "captures" the variable. To understand why this matters, we need to understand the implications of how captured variables work. If you are familiar with closures, that is exactly what's going on here.
The inner class is a closure. It copies the variable from it's enclosing scope to a new variable, and brings just that copy inside the inner class. Anything it does to that copy is independent from the variable in the enclosing scope. So if the variable changes in the inner class, and then it is used later in the enclosing scope, the changes made in the inner class did not persist in the enclosing scope.

Basically, what happens in the inner class stays in the inner class.
Below is a proper scenario.

 
public class RadiusStuff { //This is the outer class
    public void start(Stage stage) { //This method is the enclosing context
    Button submit = new Button("Submit");
    final string radius = "10";
    submit.setOnAction(new EventHandler<ActionEvent>() {
        //This is the inner class, specifically an anonymous class.
        @Override
        public void handle(ActionEvent e) {
            submit.setText(radius);
        }
    });
  }
}

This functionality is by design.

Java wants the developer to use the final keyword on any variables that are going to be modified in the inner class. This prevents us from thinking the things we change in the inner class will persist in the enclosing scope.
In a way, adding the final keyword does not change the behavior of the code. Think about it. With or without the final keyword, any changes to the variable in the inner class won't persist, so why make any changes at all? Java forces developers to use final in this scenario just to emphasize that we shouldn't be modifying a local variable in an inner class.

But what if, in the closure, we are only reading from the variable and not writing to it? Does it still need to be declared final?
If you are using Java 7 or below, the answer is "yes".
In Java 8, if we are only accessing but not changing the variable, the variable is "effectively final" and does not need to be declared final.

The problem with this is that if we ever want to change the variable we won't be able to. In the below code I show an example of this case by assigning a new value to our "radius" variable, but it will not work.
 
public class RadiusStuff { //This is the outer class
    public void start(Stage stage) { //This method is the enclosing context
    Button submit = new Button("Submit");
    final string radius = "10";
    submit.setOnAction(new EventHandler<ActionEvent>() {
        //This is the inner class, specifically an anonymous class.
        @Override
        public void handle(ActionEvent e) {
            submit.setText(radius);
        }
    });
    radius = "15"; //This will throw compile-time error.
  }
}

Instance Variables

I mentioned under the "Solution" section that we could also just make the variable an instance variable. Does this really work? Why?
Referring back to Oracle's documentation we see that our inner class "cannot access local variables in its enclosing scope that are not declared as final..."
So why would an instance variable be an exception? Note the words "local variable". This only applies to variables declared within the method that the inner class is in, also known as the "enclosing scope".
So does this mean an anonymous inner class can change an instance variable, and have those changes persist outside of the inner class?
Yes.

 
public class RadiusStuff { //This is the outer class
    string radius = "10"; //Instance variable, no need to be final.

    public void start(Stage stage) { //This method is the enclosing context
    Button submit = new Button("Submit");
    
    submit.setOnAction(new EventHandler<ActionEvent>() {
        //This is the inner class, specifically an anonymous class.
        @Override
        public void handle(ActionEvent e) {
            submit.setText(radius);
        }
    });
    string radius = "15"; //This is allowed now.
  }
}

Other notes:

  • Variables defined in interfaces are implicitly final, even if they don't have the final keyword.
  • For variables that reference objects, the properties of the object can be modified, even if the variable is final. However, you cannot change which object the variable refers to.
  • You can find excellent information on closures here: C# in depth
  • The behavior mentioned in my post is similar between Java and C#, but there are some differences, so be careful. Perhaps I'll discuss those in a future post.

Tuesday, July 28, 2015

"System.ComponentModel.Win32Exception: Access is denied"

Problem:

The following error is encountered after publishing a website:
System.ComponentModel.Win32Exception: Access is denied[Win32Exception (0x80004005): Access is denied]
[ExternalException (0x80004005): Cannot execute a program. The command being executed was "E:\someDirectory\myWebsite.com\wwwroot\bin\roslyn\csc.exe"

Solution:

Change the .NET application's framework version to 4.5.
Also erase  the <compiler> section from the web.config if it's throwing an error when you build.

Explanation:

This happened to me when running Visual Studio 2015, and publishing my website with a .NET version of 4.5.2, on my Personal Class ASP server on Arvixe. I noticed the problem did not happen when I was using a different computer with an older version of Visual Studio 2015 CTP. I noticed that the non-working version of VS was setting the target framework to 4.5.2, but the version of VS that was working for me had it set to 4.5.

The version that did not work is:

Microsoft Visual Studio Enterprise 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.00081

The version that did work is:

Microsoft Visual Studio Ultimate 2015 CTP
Version 14.0.22609.0 D14REL
Microsoft .NET Framework
Version 4.6.00030


To fix this, open up Visual Studio. Open the Solution Explorer.

















Right click on "WebApplication1" (or whatever you named your app) and then click on "Properties".
You'll be presented with the following window.
Ensure that "Target Framework" is set to 4.5. Hit Ctrl+S to save.














If you previously built with a target framework of 4.5.2, you may have a <compiler> section in your web.config, and this may throw an error when you build. Just remove this section to fix the issue. Removing it should not cause any problems.

That solved it for me. It may not solve it for you depending on the configuration of IIS on the server you're deploying to. If it didn't work, check below for other things I tried that didn't work for me but may work for you!

Other things I tried that didn't work:


  • In IIS, Enabled Basic Authentication, disabled Integrated Windows Authentication.
  • In VS when publishing using Web Deploy, I selected to exclude app_data files.
  • Tried running VS as admin
  • In IIS, checked "Enable write permissions" and "enable directory browsing",
  • Deleted everything from wwwroot and published again
  • Changed platform target from "any cpu" to x86 in web application build settings.
  • Changed platform target from "any cpu" to x64 in web application build settings.

Wednesday, June 3, 2015

Adding and removing GeoFences to a Google Map

Problem:

There is no native way to add, keep track of, or erase GeoFences that are added to a Google Map.

Solution:

After a GeoFence is successfully added to the GeoFencing API, use its radius, latitude, and longitude to construct a Circle object. Add this Circle to a Stack of Circles, and also add it to the Google Map object. Pop the circle off the stack and remove it from the Map when removing the GeoFence.

Explanation:

This assumes you have a GeoFencing API already set up and a Google Map set up.
If you followed the Android GeoFence tutorial you should end up with something like this:

LocationServices.GeofencingApi.addGeofences(
    mGoogleApiClient,
    getGeofencingRequest(),
    getGeofencePendingIntent()
).setResultCallback(this);

In my code I call this every time a GeoFence is added. If that completes successfully, you can now add your GeoFence to the map. You'll need to know the fence's ID, latitude, longitude, and radius, so make sure to store those somewhere. Then, you're going to want to call your method that will do the work for making the circle with the map. I called mine createCircle() and I call it like this. Latlng is a LatLng type, and rad is a float.

createCircle(new CircleOptions()
    .center(latlng)
    .radius(rad)
    .strokeColor(Color.BLACK));

Here is the method that does the work:

private void createCircle(CircleOptions circleOptions){
    if(mMap != null) {
        Circle newCircle = mMap.addCircle(circleOptions);
        if(newCircle != null) {
            circleStack.push(newCircle);
        } else {
            Log.d(TAG, "newCircle is null");
        }
    } else {
        Log.d(TAG, "mMap is null");
    }
}

mMap is an instance variable of the Google Map that was created earlier in our code. Make sure you only call this after the Map is ready. Remember that Google Maps has the onMapReady() callback method, and this should be in your code already if you implemented OnMapReadyCallback.

circleStack is a stack that I instantiated like this:

private Stack<Circle> circleStack = new Stack<Circle>();

Ta-da! Now we have a circle on our map exactly where the GeoFence is!
And the Stack keeps track of the Circles for when we want to remove them.

Removing Circles and GeoFences:

The Android GeoFence tutorial only shows you how to remove all the geoFences for a pending intent. But there is another removeGeofences() method that takes a different argument and allows you to remove a list of fences based on their IDs. But how do you remove just a single GeoFence? I solved this by adding a button that would remove the most recently added GeoFence from the Geofencing Api and from the Google Map.

To remove the GeoFence from the API, just get the most recently added GeoFence from your ArrayList of GeoFences, which if you followed the Android GeoFence tutorial is called mGeofenceList. The most recently added fence will be at the highest index of your mGeofenceList. So get the fence from the highest index, and then get its ID and save it in a variable. You'll also want to remove from the mGeofenceList.
Then, add that ID to a List<String>. Yes, it seems a bit ridiculous to add a single entry to a List, but we need to use a List with the removeGeofences method. Now we are ready to call GeofencingApi.removeGeofences(), passing it your mGoogleApiClient (the same one from above) and your List of a single GeoFence ID to remove.

Finally, remove the Circle from the map. This is where the stack comes in. Pop the circle from the stack and store it in a Circle variable, and then call remove() on that Circle. Note that pop returns the object that it popped off the top of the stack.

Now your GeoFences are removed from the API, so they won't do anything if you enter, exit, or dwell within them. They are also removed from the map so no one will think a GeoFence exists where it doesn't.

public void removeGeofencesClick(View view) {
    if(mGeofenceList.size() > 0 && mGoogleApiClient.isConnected()) {
        int mGeofenceListLastIndex = mGeofenceList.size() - 1;
        Geofence fenceToRemove = mGeofenceList.get(mGeofenceListLastIndex);
        String geoFenceIdToRemove = fenceToRemove.getRequestId();
        mGeofenceList.remove(mGeofenceListLastIndex);

        //This will only ever hold 1 fence at a time, but removeGeofences()
        //takes a List so that's why we make a List.
        List<String> geoFenceToRemove = new ArrayList<>();
        geoFenceToRemove.add(geoFenceIdToRemove);
        LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient,
            geoFenceToRemove).setResultCallback(this);
        //This removes the last added GeoFence from the Map,
        // but not the GeoFence collection.
        if(!circleStack.empty()){
            Circle circleToRemove = (Circle)circleStack.pop();
            circleToRemove.remove(); //remove it from the Map
        }
    }
}