Permalink
Browse files

Register projects with IIS Express as separate sites and use the conf…

…igured port.
  • Loading branch information...
1 parent 3921508 commit 5e34cf768e1aeecc7cd99993e996ab3218bcc572 @mrward mrward committed Feb 28, 2012
@@ -160,6 +160,11 @@
<Compile Include="Src\Folding\WebFormsMarkupCharacterReader.cs" />
<Compile Include="Src\IAddMvcItemToProjectView.cs" />
<Compile Include="Src\IFileSystem.cs" />
+ <Compile Include="Src\IIS6Administrator.cs" />
+ <Compile Include="Src\IIS7Administrator.cs" />
+ <Compile Include="Src\IISAdministrator.cs" />
+ <Compile Include="Src\IISExpressAdministrator.cs" />
+ <Compile Include="Src\IISExpressProcessStartInfo.cs" />
<Compile Include="Src\IMvcModelClassLocator.cs" />
<Compile Include="Src\IMvcClass.cs" />
<Compile Include="Src\IMvcControllerFileGenerator.cs" />
@@ -219,6 +224,7 @@
<Compile Include="Src\MvcViewTextTemplateFileName.cs" />
<Compile Include="Src\MvcViewTypeViewModel.cs" />
<Compile Include="Src\MvcVirtualPath.cs" />
+ <Compile Include="Src\NullIISAdministrator.cs" />
<Compile Include="Src\ProcessMonitor.cs" />
<Compile Include="Src\SelectedFolderNodeInProjectsView.cs" />
<Compile Include="Src\SelectedMvcControllerFolder.cs" />
@@ -0,0 +1,36 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+using System.EnterpriseServices.Internal;
+
+namespace ICSharpCode.AspNet.Mvc
+{
+ public class IIS6Administrator : IISAdministrator
+ {
+ const string IIS_WEB_LOCATION = "IIS://localhost/W3SVC/1/Root";
+
+ public IIS6Administrator(WebProjectProperties properties)
+ : base(properties)
+ {
+ }
+
+ public override bool IsIISInstalled()
+ {
+ return WebProjectService.IsIISInstalled;
+ }
+
+ public override void CreateVirtualDirectory(WebProject project)
+ {
+ string error = null;
+ var virtualRoot = new IISVirtualRoot();
+ virtualRoot.Create(IIS_WEB_LOCATION,
+ project.Directory,
+ project.Name,
+ out error);
+ if (!String.IsNullOrEmpty(error)) {
+ throw new ApplicationException(error);
+ }
+ }
+ }
+}
@@ -0,0 +1,44 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+
+namespace ICSharpCode.AspNet.Mvc
+{
+ public class IIS7Administrator : IISAdministrator
+ {
+ public IIS7Administrator(WebProjectProperties properties)
+ : base(properties)
+ {
+ }
+
+ public override bool IsIISInstalled()
+ {
+ return WebProjectService.IsIISInstalled;
+ }
+
+ public override void CreateVirtualDirectory(WebProject project)
+ {
+ string name = "/" + project.Name;
+
+ dynamic manager = CreateServerManager();
+
+ if (manager.Sites[DEFAULT_WEB_SITE] != null) {
+ if (manager.Sites[DEFAULT_WEB_SITE].Applications[name] == null) {
+ manager.Sites[DEFAULT_WEB_SITE].Applications.Add(name, project.Directory);
+ manager.CommitChanges();
+ } else {
+ ThrowApplicationExistsException();
+ }
+ } else {
+ if (manager.Sites[0].Applications[name] == null) {
+ manager.Sites[0].Applications.Add(name, project.Directory);
+ manager.CommitChanges();
+ } else {
+ ThrowApplicationExistsException();
+ }
+ }
+ manager.Dispose();
+ }
+ }
+}
@@ -0,0 +1,75 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+using System.Reflection;
+using ICSharpCode.Core;
+using ICSharpCode.SharpDevelop.Dom;
+
+namespace ICSharpCode.AspNet.Mvc
+{
+ public abstract class IISAdministrator
+ {
+ public const string DEFAULT_WEB_SITE = "Default Web Site";
+
+ protected IISAdministrator(WebProjectProperties properties)
+ {
+ this.Properties = properties;
+ }
+
+ protected IISAdministrator()
+ {
+ }
+
+ protected WebProjectProperties Properties { get; set; }
+
+ public static IISAdministrator CreateAdministrator(WebProjectProperties properties)
+ {
+ if (properties.UseIISExpress) {
+ return new IISExpressAdministrator(properties);
+ }
+ switch (WebProjectService.IISVersion) {
+ case IISVersion.IIS5:
+ case IISVersion.IIS6:
+ return new IIS6Administrator(properties);
+ case IISVersion.None:
+ return new NullIISAdministrator();
+ default:
+ return new IIS7Administrator(properties);
+ }
+ }
+
+ public abstract bool IsIISInstalled();
+
+ public abstract void CreateVirtualDirectory(WebProject project);
+
+ protected dynamic CreateServerManager()
+ {
+ Assembly webAdministrationAssembly = GetWebAdminstrationAssembly();
+ return webAdministrationAssembly.CreateInstance("Microsoft.Web.Administration.ServerManager");
+ }
+
+ protected Assembly GetWebAdminstrationAssembly()
+ {
+ foreach(DomAssemblyName assembly in GacInterop.GetAssemblyList()) {
+ if (assembly.FullName.Contains("Microsoft.Web.Administration")) {
+ if (IsServerManagementVersionRequired(assembly)) {
+ return Assembly.Load(assembly.FullName);
+ }
+ }
+ }
+ throw new ApplicationException(ResourceService.GetString("ICSharpCode.WebProjectOptionsPanel.IISNotFound"));
+ }
+
+
+ protected virtual bool IsServerManagementVersionRequired(DomAssemblyName assemblyName)
+ {
+ return true;
+ }
+
+ protected void ThrowApplicationExistsException()
+ {
+ throw new ApplicationException(ResourceService.GetString("ICSharpCode.WebProjectOptionsPanel.ApplicationExists"));
+ }
+ }
+}
@@ -0,0 +1,36 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+using ICSharpCode.SharpDevelop.Dom;
+
+namespace ICSharpCode.AspNet.Mvc
+{
+ public class IISExpressAdministrator : IISAdministrator
+ {
+ public IISExpressAdministrator(WebProjectProperties properties)
+ : base(properties)
+ {
+ }
+
+ public override bool IsIISInstalled()
+ {
+ return WebProjectService.IsIISExpressInstalled;
+ }
+
+ public override void CreateVirtualDirectory(WebProject project)
+ {
+ dynamic manager = CreateServerManager();
+ dynamic site = manager.Sites.Add(project.Name, project.Directory, Properties.DevelopmentServerPort);
+ string bindingInformation = String.Format("*:{0}:localhost", Properties.DevelopmentServerPort);
+ site.Bindings[0].BindingInformation = bindingInformation;
+ manager.CommitChanges();
+ manager.Dispose();
+ }
+
+ protected override bool IsServerManagementVersionRequired(DomAssemblyName assemblyName)
+ {
+ return (assemblyName.Version.Major == 7) && (assemblyName.Version.Minor == 9);
+ }
+ }
+}
@@ -0,0 +1,21 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+using System.Diagnostics;
+
+namespace ICSharpCode.AspNet.Mvc
+{
+ public static class IISExpressProcessStartInfo
+ {
+ public static ProcessStartInfo Create(WebProject project)
+ {
+ return new ProcessStartInfo(WebProjectService.IISExpressProcessLocation, GetSiteArgument(project));
+ }
+
+ static string GetSiteArgument(WebProject project)
+ {
+ return String.Format("/site:{0}", project.Name);
+ }
+ }
+}
@@ -0,0 +1,23 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+
+namespace ICSharpCode.AspNet.Mvc
+{
+ public class NullIISAdministrator : IISAdministrator
+ {
+ public NullIISAdministrator()
+ {
+ }
+
+ public override bool IsIISInstalled()
+ {
+ return false;
+ }
+
+ public override void CreateVirtualDirectory(WebProject project)
+ {
+ }
+ }
+}
@@ -85,7 +85,8 @@ public override void Start(bool withDebugging)
if (properties.UseIISExpress) {
// start IIS express and attach to it
if (WebProjectService.IsIISExpressInstalled) {
- DebuggerService.CurrentDebugger.Start(new ProcessStartInfo(WebProjectService.IISExpressProcessLocation));
+ ProcessStartInfo processInfo = IISExpressProcessStartInfo.Create(WebProject);
+ DebuggerService.CurrentDebugger.Start(processInfo);
} else {
DisposeProcessMonitor();
MessageService.ShowError("${res:ICSharpCode.WepProjectOptionsPanel.NoProjectUrlOrProgramAction}");
@@ -21,6 +21,14 @@ public WebProject(MSBuildBasedProject msbuildProject)
this.msbuildProject = msbuildProject;
}
+ public string Name {
+ get { return msbuildProject.Name; }
+ }
+
+ public string Directory {
+ get { return msbuildProject.Directory; }
+ }
+
public bool HasWebProjectProperties()
{
if (VisualStudioProjectExtension.ProjectContainsExtension(msbuildProject)) {
@@ -36,6 +36,7 @@ protected override void Load(MSBuildBasedProject project, string configuration,
if (WebProjectService.IsIISExpressInstalled) {
UseIISExpress.IsChecked = true;
PortTextBox.Text = properties.DevelopmentServerPort.ToString();
+ ProjectUrl.Text = String.Empty;
SelectIISExpress();
}
} else if (properties.UseIIS) {
@@ -57,36 +58,36 @@ protected override bool Save(MSBuildBasedProject project, string configuration,
{
if (IsDirty) {
webProject.UpdateWebProjectProperties(properties);
- //project.Save();
}
IsDirty = false;
return true;
}
void CreateVirtualDirectory_Click(object sender, RoutedEventArgs e)
{
- string error = WebProjectService.CreateVirtualDirectory(
- GetWebServer(),
- ProjectService.CurrentProject.Name,
- Path.GetDirectoryName(ProjectService.CurrentProject.FileName));
-
- if (!string.IsNullOrEmpty(error)) {
- MessageService.ShowError(error);
- } else {
+ try {
+ IISAdministrator administrator = IISAdministrator.CreateAdministrator(properties);
+ if (!administrator.IsIISInstalled()) {
+ ShowIISNotFoundMessage();
+ return;
+ }
+
+ administrator.CreateVirtualDirectory(webProject);
MessageService.ShowMessage(ResourceService.GetString("ICSharpCode.WebProjectOptionsPanel.VirtualDirCreated"));
+ } catch (Exception ex) {
+ MessageService.ShowError(ex.Message);
}
}
- WebServer GetWebServer()
+ void ShowIISNotFoundMessage()
{
- if (properties.UseIISExpress) {
- return WebServer.IISExpress;
- }
- return WebServer.IIS;
+ MessageService.ShowWarning(ResourceService.GetString("ICSharpCode.WebProjectOptionsPanel.IISNotFound"));
}
void UseIISExpress_Click(object sender, RoutedEventArgs e)
{
+ ProjectUrl.Text = String.Empty;
+ properties.IISUrl = String.Format(@"http://localhost:{0}/", PortTextBox.Text);
SelectIISExpress();
OnWebProjectPropertiesChanged();
}
@@ -96,10 +97,6 @@ void SelectIISExpress()
properties.UseIISExpress = true;
properties.UseIIS = false;
properties.DevelopmentServerPort = Int32.Parse(PortTextBox.Text);
- if (String.IsNullOrEmpty(properties.IISUrl)) {
- properties.IISUrl = String.Format(@"http://localhost:{0}/" + ProjectService.CurrentProject.Name, PortTextBox.Text);
- OnWebProjectPropertiesChanged();
- }
bool isIISExpressInstalled = WebProjectService.IsIISExpressInstalled;
if (!isIISExpressInstalled) {
@@ -109,13 +106,13 @@ void SelectIISExpress()
} else {
StatusLabel.Text = String.Empty;
}
-
IISExpressGroup.IsEnabled = CreateVirtualDirectoryButton.IsEnabled = isIISExpressInstalled;
LocalIISGroup.IsEnabled = false;
}
void UseLocalIIS_Click(object sender, RoutedEventArgs e)
{
+ properties.IISUrl = String.Format("{0}/{1}", WebBehavior.LocalHost, webProject.Name);
SelectLocalIIS();
OnWebProjectPropertiesChanged();
}
@@ -132,10 +129,6 @@ void SelectLocalIIS()
UseLocalIIS.IsChecked = false;
} else {
StatusLabel.Text = String.Empty;
- if (String.IsNullOrEmpty(properties.IISUrl)) {
- properties.IISUrl = String.Format("{0}/{1}", WebBehavior.LocalHost, ProjectService.CurrentProject.Name);
- OnWebProjectPropertiesChanged();
- }
ProjectUrl.Text = properties.IISUrl;
}
LocalIISGroup.IsEnabled = CreateVirtualDirectoryButton.IsEnabled = isIISInstalled;
@@ -188,7 +181,7 @@ void PortTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
void PortTextBox_KeyUp(object sender, KeyEventArgs e)
{
properties.DevelopmentServerPort = Int32.Parse(PortTextBox.Text);
- properties.IISUrl = String.Format(@"{0}:{1}/{2}", WebBehavior.LocalHost, PortTextBox.Text, ProjectService.CurrentProject.Name);
+ properties.IISUrl = String.Format(@"{0}:{1}/", WebBehavior.LocalHost, PortTextBox.Text);
}
}
}
Oops, something went wrong.

0 comments on commit 5e34cf7

Please sign in to comment.