I run a self-hosted WCF service which I want to protect with self-signed SSL certificate. For that to work I need to bind this SSL certificate with certain port. There are couple ways of doing it:
-
According to this http://msdn.microsoft.com/en-us/library/ms733791.aspx I can use
netshand it works but I would like not to have dependencies on the netsh and do everything from the .NET code. -
So I found the following snippet:
using (ServerManager manager = new ServerManager()) { Site site = manager.Sites.FirstOrDefault(i => i.Applications.Cast<Application>().Any()); Application app = site.Applications.Cast<Application>().First(); site.Bindings.Add(":*:PORTN", cert.GetCertHash(), store.Name); manager.CommitChanges(); }
It works, however this required IIS to be installed. Otherwise I’m getting the following exception:
Retrieving the COM class factory for component with CLSID
{2B72133B-3F5B-4602-8952-803546CE3344} failed due to the following
error: 80040154 Class not registered (Exception from HRESULT:
0x80040154 (REGDB_E_CLASSNOTREG))..
So, question is – is there any way to accomplish this inside managed code, but without having to install IIS?
Found the correct answer to my question: Binding an SSL certificate to a port programmatically
Instead of using managed classes like
ServerManagerI would need to rely on P/Invoking native API such asHttpSetServiceConfiguration.