Showing posts with label IIS. Show all posts
Showing posts with label IIS. Show all posts

Apr 17, 2011

Unable to start debugging on the server.

Lot's of such errors, lots of resolutions.
I had another one, the full message is:

Unable to start debugging on the server. An authentication error occured while communicating with the web server. Please see Help for assistance.

It looks like it is an issue with permissions but the problem is that I disabled the "HTTP Keep-Alive" signals for the web site.

Hope this saves few minutes of research ;).

Jul 19, 2010

Site is recycled + ThreadPool.QueueUserWorkItem


Just to let you know that you may experience this issue.

If you have an unhandled exception it may be because of a job queued using ThreadPool.QueueUserWorkItem method.

Actually it appears that although the exception is not affecting the main thread (it was in another one) - the .NET will kill the running process (w3wp in our case).

As far as I read this behavior is implemented since .NET Framework 2.0.
It is because you may miss unhandled exceptions in a child thread (if it's in the main thread the process will be terminated in a windows application).

I imagine each request to a web server as a separate thread so I am kind of worried why unhandled exceptions aren't killing the W3WP process as well.
It only seems to be killed if you create another thread in the request thread and this another child thread exits with unhandled exception.

Will need to investigate a bit more about that.

Aug 25, 2009

Determine if your site is running under IIS or Casini

For the people who don't know - Casini is the built in Visual Studio server which is by default running when you debug ASP.NET project.
You can use the following to check if the site is running under IIS:

bool isUnderIIS = this.Request.ServerVariables["SERVER_SOFTWARE"].IndexOf("IIS") > -1;

Not the most clever thing I've ever done but seems to be the only way at the moment.
If you know a better way - let us know...

To clarify - IIS will return something like : "Microsoft-IIS/5.1", while Casini will let you bump your head with
string.Empty.
Anyway, this covers my scenarios 100% so I can use it.

Jan 21, 2008

5 minutes C# code to open Web.Config from Windows Forms application.

In my previous post about IIS, Virtual Directories and Directory Services you saw how can you obtain the sites per IIS instance as well sa all the root virtual directories. Now it is time to show you how can you obtain the Web.Config file for a root directory.

At first glance I thought it will be easy, I thought I will simply need declare the following:

using System.Web.Configuration;

And / or :

using System.Configuration;

Well it's not quite this way. In order to load web.config file from a windows forms application you need to have some kind of context to be used or, in other words, file mapping.

The following method takes two parameters : path to the root directory and DirectoryEntry, representing the root directory
object (you can see my previous post ( Get IIS sites and virtual directories with .NET / C# / Directory Services ) learn how to obtain DirectoryEntry for a web root directory.
And here is the method:

      
List < string []> GetWebConfigAppSettings( string Path, DirectoryEntry entry)
       {
            
List < string []> items = new List < string []>();
            
if (entry.Properties[ "Path" ].Value != null )
            {
               
FileInfo fiWebConfig = new FileInfo (entry.Properties[ "Path" ].Value + @"\web.config" );
               
if (fiWebConfig.Exists)
                {
                  
WebConfigurationFileMap fm = new WebConfigurationFileMap ();
                   System.Collections.
Hashtable props = new System.Collections. Hashtable ();
                  
foreach (System.DirectoryServices. PropertyValueCollection property in entry.Properties)
                   {
                        
string propertyName = property.PropertyName;
                        props.Add(propertyName, property.Value);
                   }
                  
VirtualDirectoryMapping mapping = new VirtualDirectoryMapping (fiWebConfig.Directory.FullName, true );
                   fm.VirtualDirectories.Add(entry.Properties[
"AppRoot" ].Value.ToString(), mapping);
                   System.Configuration.
Configuration cfg = System.Web.Configuration. WebConfigurationManager .OpenMappedWebConfiguration(fm, entry.Properties[ "AppRoot" ].Value.ToString());
                  
foreach ( KeyValueConfigurationElement elmt in cfg.AppSettings.Settings)
                   {
                        items.Add(
new string [] { elmt.Key, elmt.Value });
                   }
                }
            }
            
return items;
       }



The method above will return a List of all in the web.config file (the appSettings section), you can easilly adjust it to fit your needs.

As you can see the magic begins in :

> System.Configuration. Configuration cfg = System.Web.Configuration. WebConfigurationManager .OpenMappedWebConfiguration(fm, entry.Properties[ "AppRoot" ].Value.ToString());


method. You simply need to make some preparations befor ask it to open the web.config.

Note : this code again is written in few minutes and does not pretend to be optimized, you can again use it at your own will.

Dec 28, 2007

Get IIS sites and virtual directories with .NET / C# / Directory Services

       public List GetSites(string serverName)
       {
            List Nodes = new List();
            CustomTreeNode tnResult = new CustomTreeNode();
            try
            {
                DirectoryEntry w3svc = new DirectoryEntry("IIS://" + serverName + "/w3svc");
                foreach (DirectoryEntry entry in w3svc.Children)
                {
                   if (entry.SchemaClassName == "IIsWebServer")
                   {
                        CustomTreeNode Site = new CustomTreeNode(entry.Properties["ServerComment"].Value.ToString());
                        this.ServerLoadStart(entry.Properties["ServerComment"].Value.ToString());
/>                        Site.CurrentEntry = entry;
                        Site.ImageIndex = 1;
                        Site.SelectedImageIndex = 1;
                        Site = GetVirtualDirectories(entry, Site);
                        Site.ImageIndex = 0;
                        Site.SelectedImageIndex = 0;
                        Nodes.Add(Site);
                   }
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
            return Nodes;
       }
       private CustomTreeNode GetVirtualDirectories(DirectoryEntry site, CustomTreeNode Parent)
       {
            string VirDirSchemaName = "IIsWebVirtualDir";
            DirectoryEntry folderRoot = site.Children.Find("Root", VirDirSchemaName);
            foreach (DirectoryEntry deChild in folderRoot.Children)
            {
                CustomTreeNode tnFolder = new CustomTreeNode(deChild.Name);
                this.VirtualDirectoryStart(deChild.Name);
                tnFolder.ImageIndex = 0;
                tnFolder.SelectedImageIndex = 0;
                tnFolder.CurrentEntry = deChild;
                tnFolder.ImageIndex = 1;
                tnFolder.SelectedImageIndex = 1;
                Parent.Nodes.Add(tnFolder);
            }
            return Parent;
       }

Here is the result I get after few more hours of work: