Visual Studio Add-In To Automatically Attach to Development Server - asp.net

Is anyone aware of a Visual Studio 2010 Add-In that will automatically allow you to attach to a running instance of the ASP.Net Development Server? And if there is more than one currently running, display a quick dialog that lets you choose from a list of just the ASP.Net Development Servers that are running?
Why do I want this? <-- feel free to skip this part.
The way I usually develop / debug web applications is to launch a browser and navigate through the application until I get to the page I want (could be many pages deep.) I don't want to have the debugger attached through these steps for various reasons (it is slower than not having it attached, extraneous break-points may be hit, I may have break when "thrown" turned on and not want to break earlier in the app when handled errors are thrown, etc...)
I navigate to the page I want, then use the Visual Studio menus to Debug > Attach to Process, and then from within the Attach to Process dialog, I have to scroll all the way down (pages and pages and pages of processes) until I find the WebDev.WebServer40.EXE process I want and choose that.
Doing this makes me take my hands off the keyboard and use a mouse (something I generally try to avoid.)
And doing this seems needlessly repetitive since, if I am debugging an ASP.Net Web Application, I always want to attach to an instance of the WebDev.WebServer40.exe.

I prefer to do the exact same thing and it IS possible to bind it all to a keystroke with a macro.
Goto Tools > Macros > Macro IDE
Add a new module and use this code (the funky comments are for syntax highlighting)
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.Collections.Generic
Public Module AttachingModule
Sub AttachToAspNET()
Try
Dim process As EnvDTE.Process
Dim listProcess As New List(Of String)
'' // uncomment the processes that you'd like to attach to. I only attach to cassini
'' // listProcess.Add("aspnet_wp.exe")
'' // listProcess.Add("w3wp.exe")
listProcess.Add("webdev.webserver")
For Each process In DTE.Debugger.LocalProcesses
For Each procname As String In listProcess
If process.Name.ToLower.IndexOf(procname) <> -1 Then
process.Attach()
End If
Next
Next
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
End Module
Click on File > Close and return
Click on Tools > Options
Click on Environment > Keyboard
I put the macro in MyMacros, so I look for "Macros.MyMacros.AttachingModule.AttachToAspNET" in the "Show Commands Containing" textbox".
I prefer to use Ctrl+Alt+D but put whatever you want in the "Press Shortcut Keys" textbox and click Assign, then OK
Now all you have to do is hit Ctrl+Alt+D to attach to all cassini instances.
I've seen various versions of this around the internets and this was the most recent I found. I had to modify that slightly to remove the extra web processes and to drop the .exe from WebDev.WebServer.exe, so that it would debug .net 4.0 instances of cassini.

I don't know of any such add-in but you can more easily attach to the process using shortcut keys and pressing 'W' to scroll to the WebDev process.
Ctrl+Alt+P - Attach to Process
(process window now has focus)
Press W, which jumps to processes starting with W
Press Enter to attach
Not an addin but you can do it without touching the mouse.

Check this answer out: Attach To Process in 2012
This is a simple plugin that gives shortcuts to attaching to nunit agent, IIS and IIS Express. Its pure convenience as compared to Ctrl-Alt-P, but it is convenient.
Direct link to the plugin here

Related

Development and debugging in Dynamics 365

I’ve upgraded a custom model to Dynamics 365, from Ax2012. I’ve created deploy-able package and imported into an on-prem environment.
The machine on which I developed is different from the on-prem environment (TEST-env).
There is some functionality that is not working as it should, which I need to debug, and this is where I get stuck.
I’ve been developing in Ax from version 3 to 2012. I am struggling to get comfortable and find my way in Visual Studio.
In a broad sense I guess my question is: how do I debug something as simple as a button click event on a form? Can I run the form from Visual Studio and debug it there, without having to open the Dynamics 365 website?
Since my development environment and the on-prem D365 website (TEST-env) are on two different machines, is there an easy way to make changes to the code and have the TEST-env updated, or do I have to create a deployment package and import it into TEST-env for each change?
To debug, you just create a project with your objects, right click on a form and click "Set as Startup Object" then go put a breakpoint (F9) on whatever line of code, and press F5 to run.
You could connect your Test version to VSTS and do code moves that way via branch/merging.
Your development environment is an entirely isolated version meaning typically your database and the application all live on the same box and all the D365 services run from that one box. IIS is configured to run your instance so when you browser to D365 you are viewing your development environment.
Typical debugging involves placing break points in the code, normally if you know there is code behind a button click you want to break into you would find the form where the button is right-click and get the form name. Then open visual studio 2015 which is the exclusive IDE for development. Find the AOT explorer and paste in the form name. Then open the form once it is in the explorer, find the button and look at the methods, events of that and view the code. From there you can put break points and you just have to attach the debugger to w3wp.exe. Make sure you go into the options under the Dynamics 365 menu and find debugging and make sure to uncheck "Load symbols only for items in the solution" or your break point won't get unless it is something in your project.
The other option you have with debugging is to create what is called a runnable class in D365. You create a new class and add a void main entry and then right-click on the class from the solution explorer and select "set as startup object". Then you can just press the start button from visual studio and that class will fire. This allows you to easily debug scenarios where you are checking what a select in X++ returns or items like that. You can't run the D365 site from pressing start, only simple runnable classes.
Example of a runnable class in D365
class TestClass
{
public static void main(Args _args)
{
Info("hello world");
}
}

ASP.NET unit testing Windows7/IIS7

Spent several hours today trying to write some unit tests against an ASP.NET project. It's Visual Studio 2010.
Using Windows 7 Enterprise with IIS7.
Steps I took were:
Added a new test project to the solution
Opened a class file as part of the web site (Member.vb)
Right clicked within the class file and "Generate unit tests"
Select the methods I wish to generate stubs for, choose to add to my test project, click OK
Open up the generated MemberTest.vb file in the test project, click within one of the generated tests, click "Run tests in curent context"
When following these precise steps on my Windows XP Professional with IIS6 machine it works fine.
However on the Windows 7 Enterprise machine on IIS7 I get:
The URL specified ('http://localhost/MyProject') does not
correspond to a valid directory. Tests configured to run in ASP.NET in
IIS require a valid directory to exist for the URL. The URL may be
invalid or may not point to a valid Web application.
So what's going on, I can confirm I can browse to http://localhost/MyProject and it displays perfectly.
I feel sure I'm missing some sort of config in Windows/IIS but I'm really at a loss.
Generated test method:
<TestMethod(), _
HostType("ASP.NET"), _
UrlToTest("http://localhost/MyProject")> _
Public Sub MyMethodTest()
Dim target As Member_Accessor = New Member_Accessor() ' TODO: Initialize to an appropriate value
Dim CurrentVal As Short = 0 ' TODO: Initialize to an appropriate value
Dim expected As Short = 0 ' TODO: Initialize to an appropriate value
Dim actual As Short
actual = target.MyMethod(CurrentVal)
Assert.AreEqual(expected, actual)
Assert.Inconclusive("Verify the correctness of this test method.")
End Sub
(Cross-posted at ASP.NET Forums)
This could be a permissions issue.
If you're using the default directory (C:\users\\Documents\Visual Studio 2010\Projects), the app identity pool does not have permissions there. You'd have to create a project in something like C:\webs and make sure app pool identity has permission to the folder.
Refer to Rick Anderson's blog post at http://blogs.msdn.com/b/rickandy/archive/2011/04/22/test-you-asp-net-mvc-or-webforms-application-on-iis-7-in-30-seconds.aspx and see if that helps.
If you have not done unit testing before, I would really recommend that you start by just testing the functionality of your classes as cleanly as possible. Try to break you you functionality into small pieces that can be tested individually without and dependencies to the web context.
Have a look at this question for an idea about What is unit testing
Here is an MSDN Magazine article about testing
You can also have a look at this Blog. The examples are using NUnit but the principal is the same if you are using MSTest.
I can also recommend Roy Osheroves Book Art of unit testing
In you case if the Member class does not have dependencies to web context you don't need the IIS and could instead just do something like this:
<TestMethod()> _
Public Sub MyMethodTest()
Dim target = New Member()
Dim CurrentVal As Short = 0 ' TODO: Initialize to an appropriate value
Dim expected As Short = 0 ' TODO: Initialize to an appropriate value
Dim actual As Short
actual = member.MyMethod(CurrentVal)
Assert.AreEqual(expected, actual)
End Sub
I ran into the same problem today. After some research, I found this thread which suggested I check my event log. Upon doing that, I discovered numerous errors similar to the following:
(QTAgent32.exe, PID 12348, Thread 61) WebSites.GetWebServer: failed to
create AspNetHelper:
Microsoft.VisualStudio.Enterprise.Common.AspNetHelperException: The
website metabase contains unexpected information or you do not have
permission to access the metabase. You must be a member of the
Administrators group on the local computer to access the IIS metabase.
Therefore, you cannot create or open a local IIS Web site. If you
have Read, Write, and Modify Permissions for the folder where the
files are located, you can create a file system web site that points
to the folder in order to proceed. --->
System.Runtime.InteropServices.COMException: Unknown error
(0x80005000)
That lead me to this blog post which seems to have resolved the issue.
I just needed to go to "Turn Windows features on or off" and add IIS 6 Management Compatibility and all four subcomponents. I'm running Windows 7 Home Premium which doesn't have the Windows Authentication option, but that didn't seem to be an issue. Give it a shot and see if that resolves the issue for you.
You may need to enable "Use IIS" in the project properties, then click "Create Virtual Directory". Do you have IIS Express installed?

How to automatically attach VS to another process for debugging?

I'm debugging an ASP.NET application deployed on IIS 7.5 (VS 2010/Windows 7 x64) . After I make changes, I have to deploy it to the IIS folder and then do the following things (anyone of you should already know, I just list to demonstrate how boring and time-consuming they are):
Click on the Debug menu, choose Attach to Process
From the list, choose Show processes in all sessions
Choose the right w3wp.exe process
Click attach
Click attach again
well, it's load of unnecessary works. Due to our system architect, this is the only way, we can debug straightforward by F5 button, but I wonder that if there's a workaround about this, so I can do all these things in on-click or short-cut key.
Thank you very much.
http://blog.lavablast.com/post/2008/01/11/Attach-to-Process-with-one-shortcut.aspx
Create a macro in visual studio with the following:
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Security.Principal
Public Module RecordingModule
Sub AttachToAspNET()
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim compName As String = WindowsIdentity.GetCurrent().Name
compName = compName.Substring(0, compName.IndexOf("\"))
Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, compName).Item("w3wp.exe")
proc2.Attach2(dbgeng)
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
End Module
change w3wp.exe to aspnet if thats what you want. Then go into the key shortcuts and just bind a shortcut to run that macro.
Why don't you just go to project properties and select Use Local IIS Web Server.
If this is remote server you can do this too: Using Visual Studio 2008 with IIS 7
Although the article is about VS2008, the concept is the same in 2010.

How can I debug faster in Visual Studio?

Every time I have to go to attach to process, then scroll down and find w3wp.exe
Is there a faster way to do this?
I have a macro for this very purpose. In the tools menu, open up Macros -> Macros IDE. In the lefthand pane, double-click MyModule (or create a new module) and paste in this code:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Public Module MyModule
Sub AttachToIIS()
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(2) As EnvDTE80.Engine
dbgeng(0) = trans.Engines.Item("T-SQL")
dbgeng(1) = trans.Engines.Item("Managed")
Dim proc2 As EnvDTE80.Process2 = _
dbg2.GetProcesses(trans, Environment.MachineName).Item("w3wp.exe")
proc2.Attach2(dbgeng)
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
End Module
Then, you can edit your keyboard shortcuts and set this to a new combination; I use Ctrl+Shift+A. The command to invoke will be Macros.MyMacros.MyModule.AttachToIIS.
EDITED: changed "COMPUTERNAME" to Environment.MachineName.
You should be able to debug IIS just as if you are using the Visual Studio web server (Cassini):
Show Properties for you ASP.NET project.
Select the Web tab.
In the Servers section select Use Local IIS Web server. Fill in the Project Url.
Run your project in the debugger by hitting F5 (Debug => Start Debugging).
If you are running on Vista or newer with UAC enabled you will have to run Visual Studio as administrator for this to work. Right click on the Visual Studio shortcut and select Run as Administrator.... Accept the prompt to elevate priviledges.
Debug->Attach to Process
Start typing the name of the process "w3wp" and it will immediately find it in the list.
You could write a macro and assign it to a toolbar button.

Error BC30002 - Type XXX is not defined

OK, this begins to drive me crazy. I have an asp.net webapp. Pretty straightforward, most of the code in the .aspx.vb, and a few classes in App_Code.
The problem, which has begun to occur only today (even though most of the code was already written), is that once in a while, I have this error message :
Error BC30002: Type ‘XXX’ is not defined
The error occurs about every time I modify the files in the App_Code folder. EDIT : OK, this happens also if I don't touch anything for a while then refresh the page. I'm still trying to figure out exactly how to trigger this error.
I just have to wait a little bit without touching anything, then refresh the page and it works, but it's very annoying.
So I searched a little bit, but nothing came up except imports missing. Any idea ?
I think I found the problem.
My code was like that :
Imports CMS
Sub Whatever()
Dim a as new Arbo.MyObject() ' Arbo is a namespace inside CMS
Dim b as new Util.MyOtherObject() ' Util is a namespace inside Util
End Sub
I'm not sure why I wrote it like that, but it turns out the fact I was calling classes without either calling their whole namespace or importing their whole namespace was triggering the error.
I rewrote it like this :
Imports CMS.Arbo
Imports CMS.Util
Sub Whatever()
Dim a as new MyObject()
Dim b as new MyOtherObject()
End Sub
And now it works...
This happened to me after I added a new project to an old solution. I lowered the Target framework to match that of the other 'older' projects and the error went away.
Sounds like a pre compile issue, particularly because you mention that you get the error and then wait and it disappears. ASP.NET may be still in the process of dynamically compiling your application or it has compiled the types into different assemblies.
With dynamic compilation, you are not guaranteed to have different codebehind files compiled into the same assembly. So the type you are referencing may not be able to be resolved within its precompiled assembly.
Try using the "#Reference" directive to indicate to the runtime that your page and the file that contains your type should be compiled into the same assembly.
# Reference - MSDN
Check for a compiler warning (Output window of Visual Studio) "warning : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure". This happens when one of your dlls is compiled with a newer version of dotnet. If your current project is set to use a lower version of dotnet, the dependency chain prevents the dll (with the higher dotnet ver) from loading. It gives a compile error in Visual Studio, but can still run in IIS.
Sounds like it happens every time the website spins up (the app gets recycled every time you touch app_code and probably you have IIS configured to shut down the website after X minutes of inactivity).
I bet it has something to do with the asp.net worker process not having the correct access rights on the server. So its trying to load an assembly and is being denied.
Check this link and Table 19.3 for a list of all the folders the worker process account must have access to in order to function. And don't forget to give it rights to all files and folders in your virtual directory!
Replace your vbproj and vbproj.user file from your backup before if the references are equal

Resources