我使用 Visual Studio 2012 (ASP.Net 4.5 - C#) 和 Web 服务开发了一个 Web 应用程序。两者都在一个单一的解决方案中。我需要将我的解决方案转换为 EXE 文件(为我的 Web 应用程序创建 EXE)。我真正需要的是,如果运行我的设置文件,它应该在 IIS 中托管我的 Web 应用程序和 Web 服务。请提供解决我问题的步骤。
2 回答
安静的迟到的答案:我不能在这里发布整个项目,但我会在这里发布流程图,您可以尝试一下。
此处流程图中的所有步骤都应以编程方式控制。
1. 启动应用程序的步骤。
注意:如果您使用的是本地数据库(.mdf),请忽略第三层(是否添加了网站)
用于本地数据库的连接字符串:
public string ConnectionString =
"Data Source=(local);Initial Catalog=YOUR_DATABASE_NAME;Integrated Security=True";
但请记住,您需要安装 dotnet 框架才能运行您的应用程序。不用担心,因为您可以在应用程序设置项目中设置先决条件。
以下流程图过程的所有代码。
是否安装了 IIS:
注意:我发布了 IIS 7 及更高版本的代码。
public bool IsIISInstalled()
{
return Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp",
"VersionString", null) != null;
}
安装 IIS
public int InstallIIS()
{
string DISM_CMD_CODE = "START /WAIT DISM /Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-ASP /FeatureName:IIS-ASPNET /FeatureName:IIS-BasicAuthentication /FeatureName:IIS-CGI /FeatureName:IIS-ClientCertificateMappingAuthentication /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-CustomLogging /FeatureName:IIS-DefaultDocument /FeatureName:IIS-DigestAuthentication /FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-FTPExtensibility /FeatureName:IIS-FTPServer /FeatureName:IIS-FTPSvc /FeatureName:IIS-HealthAndDiagnostics /FeatureName:IIS-HostableWebCore /FeatureName:IIS-HttpCompressionDynamic /FeatureName:IIS-HttpCompressionStatic /FeatureName:IIS-HttpErrors /FeatureName:IIS-HttpLogging /FeatureName:IIS-HttpRedirect /FeatureName:IIS-HttpTracing /FeatureName:IIS-IIS6ManagementCompatibility /FeatureName:IIS-IISCertificateMappingAuthentication /FeatureName:IIS-IPSecurity /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter /FeatureName:IIS-LegacyScripts /FeatureName:IIS-LegacySnapIn /FeatureName:IIS-LoggingLibraries /FeatureName:IIS-ManagementConsole /FeatureName:IIS-ManagementScriptingTools /FeatureName:IIS-ManagementService /FeatureName:IIS-Metabase /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ODBCLogging /FeatureName:IIS-Performance /FeatureName:IIS-RequestFiltering /FeatureName:IIS-RequestMonitor /FeatureName:IIS-Security /FeatureName:IIS-ServerSideIncludes /FeatureName:IIS-StaticContent /FeatureName:IIS-URLAuthorization /FeatureName:IIS-WebDAV /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerManagementTools /FeatureName:IIS-WebServerRole /FeatureName:IIS-WindowsAuthentication /FeatureName:IIS-WMICompatibility /FeatureName:WAS-ConfigurationAPI /FeatureName:WAS-NetFxEnvironment /FeatureName:WAS-ProcessModel /FeatureName:WAS-WindowsActivationService";
string command = DISM_CMD_CODE;
ProcessStartInfo pStartInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
Process p = new Process();
p.StartInfo = pStartInfo;
//p.WaitForExit();
p.Start();
return 1;
}
是否添加了网站
public bool IsWebsiteAddedInIIS(string WebsiteName)
{
ServerManager serverManager = new ServerManager();
var site = serverManager.Sites.FirstOrDefault(s => s.Name == WebsiteName);
if (site == null)
{
//No site added
return false;
}
else
{
//site added
return true;
}
}
public int CreateNewWebsite(string SiteName, string PublishedFilesPath)
{
ServerManager serverManager = new ServerManager();
var site = serverManager.Sites.FirstOrDefault(s => s.Name == SiteName);
if (site == null)
{
serverManager.Sites.Add(SiteName, "http", "*:8080:", PublishedFilesPath);
serverManager.CommitChanges();
return 1;
}
else
{
return 2;
}
}
public void StartWebsite(string SiteName)
{
ServerManager serverManager = new ServerManager();
var site = serverManager.Sites.FirstOrDefault(s => s.Name == SiteName);
if (site != null)
{
site.Stop();
site.Start();
}
}
public void StopWebsite(string SiteName)
{
ServerManager serverManager = new ServerManager();
var site = serverManager.Sites.FirstOrDefault(s => s.Name == SiteName);
if (site != null)
{
site.Stop();
}
}
获取网站网址
public string GetWebsiteURL(string SiteName)
{
//string SiteUrl = "";
//ServerManager serverManager = new ServerManager();
//var site = serverManager.Sites.FirstOrDefault(s => s.Name == SiteName);
//var siteBindings = site.GetCollection("bindings");
//string protocol = (string)siteBindings["protocol"];
//string bindingInfo = (string)siteBindings["bindingInformation"];
//if (protocol.StartsWith("http", StringComparison.OrdinalIgnoreCase))
//{
// string[] parts = bindingInfo.Split(':');
// if (parts.Length == 3)
// {
// //Get the port in use HERE !!!
// string port = parts[1];
// SiteUrl = "localhost:" + port;
// }
//}
//return SiteUrl;
int port = 0;
string SiteUrl = "";
ServerManager serverManager = new ServerManager();
var site = serverManager.Sites.FirstOrDefault(s => s.Name == SiteName);
foreach (Binding binding in site.Bindings)
{
port = binding.EndPoint.Port;
SiteUrl = "localhost:" + port + "/index.aspx";
break;
}
return SiteUrl;
}
初始化浏览网站
您必须将 Cefsharp 铬浏览器安装到您的 Windows 窗体中
安装包 CefSharp.WinForms -版本 75.1.143
public void InitBrowser(string Address)
{
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser(Address);
Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
谢谢..
也许您需要创建一个 Web 设置项目。“Web 设置项目为部署网站提供了最高级别的灵活性。尽管 Web 设置项目对开发人员来说更加复杂,但它们允许您生成 MSI 包、预编译网站并执行应用程序可能执行的几乎任何设置任务要求。
许多网站不需要自定义配置。在这些情况下,您可以简单地构建您的 MSI 文件并准备好进行部署。但是,更复杂的场景包括依赖项(例如特定的操作系统版本或服务包)、自定义注册表项或管理员配置。Web 设置项目允许您部署满足这些要求的网站。”
查看第 8 章“调试和部署”中的“MCTS Self-Paced Training Kit (Exam 70-515) - Web Applications Development with Microsoft .NET Framework 4”一书。
希望这可以帮助。