c# - 使用 HTTPS 的 WCF 的最小示例

标签 c# wcf

我正在尝试让一个最小的 WCF/https 示例正常工作,但我无法访问该服务(令人惊讶的是代码运行没有错误)。

  • 我已经创建了一个根证书:

    makecert -n "CN=Test" -r -sv Test.pvk TestCert.cer

  • 我已将它添加到 Windows 根证书中。
  • 我已经创建了一个服务器证书:

    makecert -sk TestCom -iv Test.pvk -n "CN=TestCom" -ic TestCert.cer -sr LocalMachine -ss my -sky exchange -pe

  • 我已经为 SSL url 保留了一个端口:

    netsh http add urlacl url=https://+:15001/ user=Everyone

  • 我已将证书哈希设置为端口:

    netsh http add sslcert ipport=0.0.0.0:15001 certhash=XXX appid={76d71921-b40d-4bc8-8fcc-4315b595878e}

  • 我已将 TestCom 添加到 etc/hosts 文件

现在我已经创建了一个简单的 C# 项目:

namespace ConsoleApplication7
{
    [ServiceContract]
    public interface ISimpleService
    {
        [OperationContract]
        string SimpleMethod(string msg);
    }

    class SimpleService : ISimpleService
    {
        public string SimpleMethod(string msg)
        {
            return "Hello " + msg;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(SimpleService));
            try
            {
                host.Open();
                Console.ReadLine();
                host.Close();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                Console.Read();
            }
        }
    }
}

使用以下 app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.web>
      <compilation debug="true"/>
    </system.web>
    <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviors_HTTPSServiceHost" >
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="BasicHttpBinding_HTTPSServiceHost">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

    <services>
      <service name="ConsoleApplication7.SimpleService" behaviorConfiguration="ServiceBehaviors_HTTPSServiceHost">
        <endpoint address=""  binding="wsHttpBinding" bindingConfiguration="BasicHttpBinding_HTTPSServiceHost"
                  contract="ConsoleApplication7.ISimpleService">
          <identity>
            <dns value="TestCom"/>
          </identity>
        </endpoint>
        <endpoint name="mex" address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="https://TestCom:51001/SimpleService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

</configuration>

最佳答案

我无法对此帐户发表评论,所以如果这不是答案,请在下面打开一个对话,以帮助缩小范围。将您的设置与我写的交叉引用 here你可能会在你的设置中发现一些我没有发现的东西。

我最初能看到的唯一问题是

I have added it to the Windows-Root certificates.

A specified logon session does not exist. It may already have been terminated.

如果这是错误,只需检查您将证书导入到正确的位置即可。

导入现有证书

  • 运行 mmc.exe。
  • 转到文件 -> 添加/删除管理单元
  • 选择证书管理单元。
  • 选择计算机帐户
  • 导航至:证书(本地计算机)\个人\证书
  • 右键单击“证书”文件夹并选择“所有任务”->“导入”。
  • 按照向导说明选择证书。

我已经解决了 wsHttpBinding 可能出现的所有问题,并且有一个个人 wiki,如果有任何日志,我可以更好地帮助您,它目前在哪里崩溃?

关于c# - 使用 HTTPS 的 WCF 的最小示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30326877/

相关文章:

c# - c# 编译器是否优化 Count 属性?

wcf - 如何在 WCF 中为非自定义绑定(bind)定义 maxReceivedMessageSize 时使用 ServiceRoutes

c# - IN () 语句在 NHibernate 中如何工作? (使用标准)

c# - 任何 .NET '#region directive' 约定想法?

c# - 连接到本地主机上的 MySQL 失败

wcf - 严格版本化的 WCF 服务命名空间是否应该是契约(Contract)唯一的?

WCF 与 Ninject 抛出 ArgumentNullException

c# - 在 WCF 中设置最大下载速度

silverlight - Windows Phone 7 - Cookie 未发送到 WCF 服务

c# - MySQL "file-per-table",未选择数据库错误