Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
I have a WCF service self-hosted in a console application. (will later moved to a WPF app)

It works all fine when the windows firewall is turned off. But when the firewall is active, service doesn't seems to receive ANY requests.

Service opens fine.
I'm using basicHttpBinding and no sessions. I've tried adding exceptions to firewall. Adding the exe didn't helped at all. (Probably because the connection is initiated by http.sys and not by my exe)

However, allowing the port on both inbound and outbound rules made it work.

The app I'm working on is for normal windows users with little or no technical knowledge. I don't expect them to sneak into firewall settings to get it to work.

My target platforms are Windows Vista and Windows 7.

Is there a way that I can do this in C# ? Adding the exception to the port in the installer would be great, so I can remove it when un-installing.

(I don't care about any third party firewalls or anti-virus programs)

Help needed please...
Posted

1 solution

Found a solution for this.

I've searched through a lot of sites, including codeplex, but couldn't find a good solution in managed code.
Most solutions were calling native code in protected locations, which can be blocked by UAC and some anti-virus programs. Some of them used COM components (hnetcfg.dll) and used kernal mode drivers.


But here's the simple way of doing things. Run netsh command. :laugh:

To open a port

netsh advfirewall firewall add rule name="rule name goes here" dir=in action=allow protocol=TCP localport=7777


Change name= and localport= to what ever you want.

To remove the rule
netsh advfirewall firewall delete rule name="rule name goes here" dir=in protocol=TCP localport=7777


NOTE: there is no action, because it doesn't make any sense. Actually protocol, dir and localport parameters are optional. But I recommend to always use them because this command deletes all results matching the criteria. So make sure you only delete the once you want tot delete.

Best Practices


Always run the delete command before adding a rule. It'll make sure you're not adding a duplicate rule. So you won't end-up with a zillion rules to allow the same port.

Happy Coding
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900