获取控制台应用程序的Windows防火墙提示,但不提供服

我有一个ac #.net应用程序,它从网络上的其他设备接收TCP和UDP流。

当我将其作为控制台应用程序运行时,Windows防火墙会提示我:“Windows防火墙已阻止此程序的某些function”,它要求我允许vshost32.exe在网络上进行通信。

我同意,该应用程序工作正常。

然而,当我将应用程序作为服务运行时(我有一个单独的控制台和服务包装器)我没有得到这样的提示,如果关闭防火墙我只能让它工作。

这是服务的预期吗? ()

另外,我已经阅读了一些代码片段,建议您可以手动将例外添加到Windows防火墙列表中。 这仅适用于控制台应用程序还是适用于服务?

我的一些代码可以监听端口,以防有用…

// // Setup UDP listening // if (protocol == "UDP") { m_udp = new UdpConn("RedwallReceiver UDP", m_local, new NetAddress()); m_udp.Receive(new VDataHandler(ReceiveData)); } // // Setup TCP listening // if (protocol == "TCP") { m_listener = new TcpListener(m_local); m_listener.Start(); m_listener.BeginAcceptSocket(AcceptSocket, null); } 

服务在受限制的环境下执行,并且允许与UI进行很少或没有交互。 他的答案涵盖了所有的推理,这里是如何实现相同的。

我建议在解决方案中添加一个额外的项目(我们称之为Configurator),它可以作为安装过程的一部分启动。 据我所知,向防火墙添加规则需要管理权限。 以下是步骤:

  • Configurator项目创建为Console或WinForms应用程序。 这里不需要UI。
  • 将应用程序清单文件添加到Configurator项目。 右键单击项目,添加>新建项>应用程序清单文件。 将标记更改为
  • Configurator项目的输出添加到您的设置/部署项目中。
  • 选择部署项目并导航到“ Custom Actions选项卡。 在Commit节点下添加新的自定义操作,并使其指向Configurator项目的输出。
  • Configurator项目中,从COM引用添加对NetFwTypeLib引用。
  • 将以下代码添加到Configurator项目中。

修改Configurator项目的Main方法以返回int(0表示成功,非零表示失败)并使用以下代码。 请注意,我已经直接从我的项目粘贴了这个,所以你可能需要修复一些decleration错误等。

 private static int Main (string [] args) { var application = new NetFwAuthorizedApplication() { Name = "MyService", Enabled = true, RemoteAddresses = "*", Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL, IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY, ProcessImageFileName = "ServiceAssemblyName.dll", }; return (FirewallUtilities.AddApplication(application, out exception) ? 0 : -1); } namespace MySolution.Configurator.Firewall { using System; using System.Linq; using NetFwTypeLib; public sealed class NetFwAuthorizedApplication: INetFwAuthorizedApplication { public string Name { get; set; } public bool Enabled { get; set; } public NET_FW_SCOPE_ Scope { get; set; } public string RemoteAddresses { get; set; } public string ProcessImageFileName { get; set; } public NET_FW_IP_VERSION_ IpVersion { get; set; } public NetFwAuthorizedApplication () { this.Name = ""; this.Enabled = false; this.RemoteAddresses = ""; this.ProcessImageFileName = ""; this.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; this.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY; } public NetFwAuthorizedApplication (string name, bool enabled, string remoteAddresses, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion, string processImageFileName) { this.Name = name; this.Scope = scope; this.Enabled = enabled; this.IpVersion = ipVersion; this.RemoteAddresses = remoteAddresses; this.ProcessImageFileName = processImageFileName; } public static NetFwAuthorizedApplication FromINetFwAuthorizedApplication (INetFwAuthorizedApplication application) { return (new NetFwAuthorizedApplication(application.Name, application.Enabled, application.RemoteAddresses, application.Scope, application.IpVersion, application.ProcessImageFileName)); } } } namespace MySolution.Configurator.Firewall { using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using NetFwTypeLib; public static class FirewallUtilities { public static bool GetApplication (string processImageFileName, out INetFwAuthorizedApplication application, out Exception exception) { var result = false; var comObjects = new Stack(); exception = null; application = null; if (processImageFileName == null) { throw (new ArgumentNullException("processImageFileName")); } if (processImageFileName.Trim().Length == 0) { throw (new ArgumentException("The argument [processImageFileName] cannot be empty.", "processImageFileName")); } try { var type = Type.GetTypeFromProgID("HNetCfg.FwMgr", true); try { var manager = (INetFwMgr) Activator.CreateInstance(type); comObjects.Push(manager); try { var policy = manager.LocalPolicy; comObjects.Push(policy); var profile = policy.CurrentProfile; comObjects.Push(profile); var applications = profile.AuthorizedApplications; comObjects.Push(applications); foreach (INetFwAuthorizedApplication app in applications) { comObjects.Push(app); if (string.Compare(app.ProcessImageFileName, processImageFileName, true, CultureInfo.InvariantCulture) == 0) { result = true; application = NetFwAuthorizedApplication.FromINetFwAuthorizedApplication(app); break; } } if (!result) { throw (new Exception("The requested application was not found.")); } } catch (Exception e) { exception = e; } } catch (Exception e) { exception = e; } finally { while (comObjects.Count > 0) { ComUtilities.ReleaseComObject(comObjects.Pop()); } } } catch (Exception e) { exception = e; } finally { } return (result); } public static bool AddApplication (INetFwAuthorizedApplication application, out Exception exception) { var result = false; var comObjects = new Stack(); exception = null; if (application == null) { throw (new ArgumentNullException("application")); } try { var type = Type.GetTypeFromProgID("HNetCfg.FwMgr", true); try { var manager = (INetFwMgr) Activator.CreateInstance(type); comObjects.Push(manager); try { var policy = manager.LocalPolicy; comObjects.Push(policy); var profile = policy.CurrentProfile; comObjects.Push(profile); var applications = profile.AuthorizedApplications; comObjects.Push(applications); applications.Add(application); result = true; } catch (Exception e) { exception = e; } } catch (Exception e) { exception = e; } finally { while (comObjects.Count > 0) { ComUtilities.ReleaseComObject(comObjects.Pop()); } } } catch (Exception e) { exception = e; } finally { } return (result); } public static bool RemoveApplication (string processImageFileName, out Exception exception) { var result = false; var comObjects = new Stack(); exception = null; if (processImageFileName == null) { throw (new ArgumentNullException("processImageFileName")); } if (processImageFileName.Trim().Length == 0) { throw (new ArgumentException("The argument [processImageFileName] cannot be empty.", "processImageFileName")); } try { var type = Type.GetTypeFromProgID("HNetCfg.FwMgr", true); try { var manager = (INetFwMgr) Activator.CreateInstance(type); comObjects.Push(manager); try { var policy = manager.LocalPolicy; comObjects.Push(policy); var profile = policy.CurrentProfile; comObjects.Push(profile); var applications = profile.AuthorizedApplications; comObjects.Push(applications); applications.Remove(processImageFileName); result = true; } catch (Exception e) { exception = e; } } catch (Exception e) { exception = e; } finally { while (comObjects.Count > 0) { ComUtilities.ReleaseComObject(comObjects.Pop()); } } } catch (Exception e) { exception = e; } finally { } return (result); } } }