我已成功创建 WCF 自承载 HTTP
Web 服务。我使用 webserviceHost
创建此服务。我没有对 web.config
文件进行任何更改。这是我的代码:
实例.cs:
[OperationContract, WebInvoke(Method = "GET", UriTemplate = "/getstatus/", ResponseFormat = WebMessageFormat.Json)]
bool getstatus();
服务.cs:
public bool getstatus()
{
return true;
}
绑定(bind)WS.cs
void bindservice()
{
try
{
m_running = true;
// Start the host
m_serviceHost = new WebServiceHost(typeof(Swiper.cs), new Uri(http://localhost:8083"));
ServiceEndpoint ep = m_serviceHost.AddServiceEndpoint(typeof(Instace.cs), new WebHttpBinding(), "");
m_serviceHost.Open();
while (m_running)
{
// Wait until thread is stopped
Thread.Sleep(SleepTime);
}
// Stop the host
m_serviceHost.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (m_serviceHost != null)
{
m_serviceHost.Close();
}
}
}
以上代码可以很好地在 Htpp
上运行 WCF
。但我如何将其转换为 HTTPS
。我关注了很多博客,但什么也没得到。有可能吗?
最佳答案
基本上,您需要根据您在主机中使用的端口号手动注册证书。以下是有关如何实现该目标的一些详细信息
http://msdn.microsoft.com/en-us/library/ms733791.aspx
21/2/13 更新:
如果您已经按照上述方式为域注册了证书,您应该能够通过对上述代码进行一些调整来使其正常工作。下面是一些使用控制台应用程序作为主机的示例代码。 HTH.
using (var serviceHost = new WebServiceHost(typeof(Swiper), new Uri("https://localhost:8083")))
{
var secureWebHttpBinding = new WebHttpBinding(WebHttpSecurityMode.Transport) { Name = "secureHttpWeb" };
serviceHost.AddServiceEndpoint(typeof(ISwiper), secureWebHttpBinding, "");
serviceHost.Open();
Console.WriteLine("Service running...");
Console.WriteLine("Press any key to stop service.");
Console.ReadLine();
// Stop the host
serviceHost.Close();
}
关于c# - 如何使用 HTTPS 访问 WCF WebServiceHost 端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14933696/