Browse Source

Register projects with IIS Express as separate sites and use the configured port.

pull/18/head
Matt Ward 14 years ago
parent
commit
5e34cf768e
  1. 6
      src/AddIns/BackendBindings/AspNet.Mvc/Project/AspNet.Mvc.csproj
  2. 36
      src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/IIS6Administrator.cs
  3. 44
      src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/IIS7Administrator.cs
  4. 75
      src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/IISAdministrator.cs
  5. 36
      src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/IISExpressAdministrator.cs
  6. 21
      src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/IISExpressProcessStartInfo.cs
  7. 23
      src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/NullIISAdministrator.cs
  8. 3
      src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/WebBehavior.cs
  9. 8
      src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/WebProject.cs
  10. 41
      src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/WebProjectOptions/WebProjectOptionsPanel.xaml.cs
  11. 90
      src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/WebProjectService.cs
  12. 24
      src/AddIns/BackendBindings/AspNet.Mvc/Test/Src/WebProjectTests.cs

6
src/AddIns/BackendBindings/AspNet.Mvc/Project/AspNet.Mvc.csproj

@ -160,6 +160,11 @@
<Compile Include="Src\Folding\WebFormsMarkupCharacterReader.cs" /> <Compile Include="Src\Folding\WebFormsMarkupCharacterReader.cs" />
<Compile Include="Src\IAddMvcItemToProjectView.cs" /> <Compile Include="Src\IAddMvcItemToProjectView.cs" />
<Compile Include="Src\IFileSystem.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\IMvcModelClassLocator.cs" />
<Compile Include="Src\IMvcClass.cs" /> <Compile Include="Src\IMvcClass.cs" />
<Compile Include="Src\IMvcControllerFileGenerator.cs" /> <Compile Include="Src\IMvcControllerFileGenerator.cs" />
@ -219,6 +224,7 @@
<Compile Include="Src\MvcViewTextTemplateFileName.cs" /> <Compile Include="Src\MvcViewTextTemplateFileName.cs" />
<Compile Include="Src\MvcViewTypeViewModel.cs" /> <Compile Include="Src\MvcViewTypeViewModel.cs" />
<Compile Include="Src\MvcVirtualPath.cs" /> <Compile Include="Src\MvcVirtualPath.cs" />
<Compile Include="Src\NullIISAdministrator.cs" />
<Compile Include="Src\ProcessMonitor.cs" /> <Compile Include="Src\ProcessMonitor.cs" />
<Compile Include="Src\SelectedFolderNodeInProjectsView.cs" /> <Compile Include="Src\SelectedFolderNodeInProjectsView.cs" />
<Compile Include="Src\SelectedMvcControllerFolder.cs" /> <Compile Include="Src\SelectedMvcControllerFolder.cs" />

36
src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/IIS6Administrator.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);
}
}
}
}

44
src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/IIS7Administrator.cs

@ -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();
}
}
}

75
src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/IISAdministrator.cs

@ -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"));
}
}
}

36
src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/IISExpressAdministrator.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 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);
}
}
}

21
src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/IISExpressProcessStartInfo.cs

@ -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);
}
}
}

23
src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/NullIISAdministrator.cs

@ -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)
{
}
}
}

3
src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/WebBehavior.cs

@ -85,7 +85,8 @@ namespace ICSharpCode.AspNet.Mvc
if (properties.UseIISExpress) { if (properties.UseIISExpress) {
// start IIS express and attach to it // start IIS express and attach to it
if (WebProjectService.IsIISExpressInstalled) { if (WebProjectService.IsIISExpressInstalled) {
DebuggerService.CurrentDebugger.Start(new ProcessStartInfo(WebProjectService.IISExpressProcessLocation)); ProcessStartInfo processInfo = IISExpressProcessStartInfo.Create(WebProject);
DebuggerService.CurrentDebugger.Start(processInfo);
} else { } else {
DisposeProcessMonitor(); DisposeProcessMonitor();
MessageService.ShowError("${res:ICSharpCode.WepProjectOptionsPanel.NoProjectUrlOrProgramAction}"); MessageService.ShowError("${res:ICSharpCode.WepProjectOptionsPanel.NoProjectUrlOrProgramAction}");

8
src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/WebProject.cs

@ -21,6 +21,14 @@ namespace ICSharpCode.AspNet.Mvc
this.msbuildProject = msbuildProject; this.msbuildProject = msbuildProject;
} }
public string Name {
get { return msbuildProject.Name; }
}
public string Directory {
get { return msbuildProject.Directory; }
}
public bool HasWebProjectProperties() public bool HasWebProjectProperties()
{ {
if (VisualStudioProjectExtension.ProjectContainsExtension(msbuildProject)) { if (VisualStudioProjectExtension.ProjectContainsExtension(msbuildProject)) {

41
src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/WebProjectOptions/WebProjectOptionsPanel.xaml.cs

@ -36,6 +36,7 @@ namespace ICSharpCode.AspNet.Mvc
if (WebProjectService.IsIISExpressInstalled) { if (WebProjectService.IsIISExpressInstalled) {
UseIISExpress.IsChecked = true; UseIISExpress.IsChecked = true;
PortTextBox.Text = properties.DevelopmentServerPort.ToString(); PortTextBox.Text = properties.DevelopmentServerPort.ToString();
ProjectUrl.Text = String.Empty;
SelectIISExpress(); SelectIISExpress();
} }
} else if (properties.UseIIS) { } else if (properties.UseIIS) {
@ -57,7 +58,6 @@ namespace ICSharpCode.AspNet.Mvc
{ {
if (IsDirty) { if (IsDirty) {
webProject.UpdateWebProjectProperties(properties); webProject.UpdateWebProjectProperties(properties);
//project.Save();
} }
IsDirty = false; IsDirty = false;
return true; return true;
@ -65,28 +65,29 @@ namespace ICSharpCode.AspNet.Mvc
void CreateVirtualDirectory_Click(object sender, RoutedEventArgs e) void CreateVirtualDirectory_Click(object sender, RoutedEventArgs e)
{ {
string error = WebProjectService.CreateVirtualDirectory( try {
GetWebServer(), IISAdministrator administrator = IISAdministrator.CreateAdministrator(properties);
ProjectService.CurrentProject.Name, if (!administrator.IsIISInstalled()) {
Path.GetDirectoryName(ProjectService.CurrentProject.FileName)); ShowIISNotFoundMessage();
return;
if (!string.IsNullOrEmpty(error)) { }
MessageService.ShowError(error);
} else { administrator.CreateVirtualDirectory(webProject);
MessageService.ShowMessage(ResourceService.GetString("ICSharpCode.WebProjectOptionsPanel.VirtualDirCreated")); MessageService.ShowMessage(ResourceService.GetString("ICSharpCode.WebProjectOptionsPanel.VirtualDirCreated"));
} catch (Exception ex) {
MessageService.ShowError(ex.Message);
} }
} }
WebServer GetWebServer() void ShowIISNotFoundMessage()
{ {
if (properties.UseIISExpress) { MessageService.ShowWarning(ResourceService.GetString("ICSharpCode.WebProjectOptionsPanel.IISNotFound"));
return WebServer.IISExpress;
}
return WebServer.IIS;
} }
void UseIISExpress_Click(object sender, RoutedEventArgs e) void UseIISExpress_Click(object sender, RoutedEventArgs e)
{ {
ProjectUrl.Text = String.Empty;
properties.IISUrl = String.Format(@"http://localhost:{0}/", PortTextBox.Text);
SelectIISExpress(); SelectIISExpress();
OnWebProjectPropertiesChanged(); OnWebProjectPropertiesChanged();
} }
@ -96,10 +97,6 @@ namespace ICSharpCode.AspNet.Mvc
properties.UseIISExpress = true; properties.UseIISExpress = true;
properties.UseIIS = false; properties.UseIIS = false;
properties.DevelopmentServerPort = Int32.Parse(PortTextBox.Text); 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; bool isIISExpressInstalled = WebProjectService.IsIISExpressInstalled;
if (!isIISExpressInstalled) { if (!isIISExpressInstalled) {
@ -109,13 +106,13 @@ namespace ICSharpCode.AspNet.Mvc
} else { } else {
StatusLabel.Text = String.Empty; StatusLabel.Text = String.Empty;
} }
IISExpressGroup.IsEnabled = CreateVirtualDirectoryButton.IsEnabled = isIISExpressInstalled; IISExpressGroup.IsEnabled = CreateVirtualDirectoryButton.IsEnabled = isIISExpressInstalled;
LocalIISGroup.IsEnabled = false; LocalIISGroup.IsEnabled = false;
} }
void UseLocalIIS_Click(object sender, RoutedEventArgs e) void UseLocalIIS_Click(object sender, RoutedEventArgs e)
{ {
properties.IISUrl = String.Format("{0}/{1}", WebBehavior.LocalHost, webProject.Name);
SelectLocalIIS(); SelectLocalIIS();
OnWebProjectPropertiesChanged(); OnWebProjectPropertiesChanged();
} }
@ -132,10 +129,6 @@ namespace ICSharpCode.AspNet.Mvc
UseLocalIIS.IsChecked = false; UseLocalIIS.IsChecked = false;
} else { } else {
StatusLabel.Text = String.Empty; 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; ProjectUrl.Text = properties.IISUrl;
} }
LocalIISGroup.IsEnabled = CreateVirtualDirectoryButton.IsEnabled = isIISInstalled; LocalIISGroup.IsEnabled = CreateVirtualDirectoryButton.IsEnabled = isIISInstalled;
@ -188,7 +181,7 @@ namespace ICSharpCode.AspNet.Mvc
void PortTextBox_KeyUp(object sender, KeyEventArgs e) void PortTextBox_KeyUp(object sender, KeyEventArgs e)
{ {
properties.DevelopmentServerPort = Int32.Parse(PortTextBox.Text); 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);
} }
} }
} }

90
src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/WebProjectService.cs

@ -37,8 +37,6 @@ namespace ICSharpCode.AspNet.Mvc
const string IIS_LOCATION = "Software\\Microsoft\\InetStp"; const string IIS_LOCATION = "Software\\Microsoft\\InetStp";
const string IIS_MAJOR_VERSION = "MajorVersion"; const string IIS_MAJOR_VERSION = "MajorVersion";
const string IIS_INSTALL_PATH = "InstallPath"; const string IIS_INSTALL_PATH = "InstallPath";
const string DEFAULT_WEB_SITE = "Default Web Site";
const string IIS_WEB_LOCATION = "IIS://localhost/W3SVC/1/Root";
const string ASPNET_REG_PATH = @"SOFTWARE\MICROSOFT\ASP.NET"; const string ASPNET_REG_PATH = @"SOFTWARE\MICROSOFT\ASP.NET";
const string ASPNET_ROOT_VER = @"RootVer"; const string ASPNET_ROOT_VER = @"RootVer";
@ -215,93 +213,5 @@ namespace ICSharpCode.AspNet.Mvc
return IISVersion.None; return IISVersion.None;
} }
} }
/// <summary>
/// Creates a virtual directory in local IIS or IIS Express.
/// </summary>
/// <param name="virtualDirectoryName">Virtual directory name.</param>
/// <param name="virtualDirectoryPath">Physical path.</param>
/// <returns>Error string or string null = no errors.</returns>
public static string CreateVirtualDirectory(WebServer webServer, string virtualDirectoryName, string physicalDirectoryPath)
{
try {
string iisNotFoundError = ResourceService.GetString("ICSharpCode.WebProjectOptionsPanel.IISNotFound");
if (!IsIISOrIISExpressInstalled)
return iisNotFoundError;
string error;
switch(IISVersion)
{
case IISVersion.IIS5:
case IISVersion.IIS6:
var vr = new IISVirtualRoot();
vr.Create(IIS_WEB_LOCATION,
physicalDirectoryPath,
virtualDirectoryName,
out error);
break;
default:
if (!IsIISExpressInstalled && (IISVersion == IISVersion.None))
return iisNotFoundError;
// TODO: find a better way to create IIS applications without Microsoft.Web.Administration.ServerManager
string name = "/" + virtualDirectoryName;
// load from GAC
Assembly webAdministrationAssembly = null;
try {
// iis installed
foreach(DomAssemblyName assembly in GacInterop.GetAssemblyList()) {
if (assembly.FullName.Contains("Microsoft.Web.Administration")) {
if (IsAssemblyForWebServer(webServer, assembly)) {
webAdministrationAssembly = Assembly.Load(assembly.FullName);
break;
}
}
}
} catch {
return iisNotFoundError;
}
if (webAdministrationAssembly == null)
return iisNotFoundError;
// use dynamic because classic reflection is way TOO ugly
dynamic manager = webAdministrationAssembly.CreateInstance("Microsoft.Web.Administration.ServerManager");
if (manager.Sites[DEFAULT_WEB_SITE] != null) {
if (manager.Sites[DEFAULT_WEB_SITE].Applications[name] == null) {
manager.Sites[DEFAULT_WEB_SITE].Applications.Add(name, physicalDirectoryPath);
manager.CommitChanges();
error = string.Empty;
} else {
error = ResourceService.GetString("ICSharpCode.WebProjectOptionsPanel.ApplicationExists");
}
} else {
if (manager.Sites[0].Applications[name] == null) {
manager.Sites[0].Applications.Add(name, physicalDirectoryPath);
manager.CommitChanges();
error = string.Empty;
} else {
error = ResourceService.GetString("ICSharpCode.WebProjectOptionsPanel.ApplicationExists");
}
}
manager.Dispose();
break;
}
return error;
}
catch (Exception ex) {
return ex.Message;
}
}
static bool IsAssemblyForWebServer(WebServer webServer, DomAssemblyName assembly)
{
if (webServer == WebServer.IISExpress) {
return (assembly.Version.Major == 7) && (assembly.Version.Minor == 9);
}
return true;
}
} }
} }

24
src/AddIns/BackendBindings/AspNet.Mvc/Test/Src/WebProjectTests.cs

@ -272,5 +272,29 @@ namespace AspNet.Mvc.Tests
Assert.IsFalse(contains); Assert.IsFalse(contains);
} }
[Test]
public void Name_MSBuildProjectNameIsTest_ReturnsTest()
{
CreateMSBuildProject();
msbuildProject.Name = "Test";
CreateWebProject(msbuildProject);
string name = webProject.Name;
Assert.AreEqual("Test", name);
}
[Test]
public void Directory_MSBuildProjectDirectoryIsSet_ReturnsMSBuildProjectDirectory()
{
CreateMSBuildProject();
msbuildProject.FileName = @"c:\projects\Test\test.csproj";
CreateWebProject(msbuildProject);
string directory = webProject.Directory;
Assert.AreEqual(@"c:\projects\Test", directory);
}
} }
} }

Loading…
Cancel
Save