c# - 如何使用 HTTPS 访问 WCF WebServiceHost 端点?

标签 c# visual-studio-2010 wcf visual-studio wcf-binding

我已成功创建 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/

相关文章:

.net - 如果Unity不在GAC中,VS如何知道在添加引用中显示它?

c++ - 我可以将可执行文件构建为 dll 吗?

c# - 是否可以使用 IErrorHandler 返回与契约(Contract)中预期内容相匹配的响应 DTO

c# - 即使在构造函数中初始化后,SelectList 也会抛出 "Object reference not set"错误

c# - 为什么我的方法只返回 IEnumerable 的第一个元素?

c# - WPF TileBrush 和 DrawingContext.DrawRectangle ——相对于矩形的左上角?

visual-studio-2010 - 通过 MSBuild 从 None ItemGroup 复制项目时保留目录结构

c# - WCF:通用接口(interface)的序列化是否可能?

.net - 托管实现通用接口(interface)的 C# 通用类的 WCF 服务

c# - 在 WCF 中初始化对象一次