使用HTTPS的WCF最小例子

问题描述 投票:8回答:2

我试图让一个最小的WCFhttps例子工作,但我无法访问服务(令人惊讶的是,代码运行没有错误)。

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

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

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

    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}。

  • 我在etchosts文件中加入了TestCom。

现在我已经创建了一个简单的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>
c# wcf
2个回答
1
投票

我不能对这个账户进行评论,所以如果这不是答案,请在下面打开对话,并帮助缩小它。将你的设置与我写的内容进行交叉参考 此处 你可能会发现一些我没有发现的东西在你的设置。

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

我已经将它添加到Windows-Root证书中。

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

如果这是错误,只需检查你导入证书的位置是否正确。

导入现有的证书

  • 运行mmc.exe。
  • 转到 "文件-> 添加删除快进"。
  • 选择证书快照。
  • 选择计算机账户
  • 导航到: 证书(本地电脑)/个人/证书
  • 右击证书文件夹,选择所有任务->导入。
  • 按照向导指示选择证书。

wsHttpBinding的所有可能的问题我都打过了,而且有个人的wiki,如果有日志的话,我可以更好的帮助你,目前在哪里崩溃?


0
投票

估计你需要加入policyVersion,才能使它运行成下面的样子。

<serviceMetadata httpGetEnabled="True"  policyVersion="Policy12"></serviceMetadata>
© www.soinside.com 2019 - 2024. All rights reserved.