使用HTTPS的WCF的最小示例

Har*_*ald 8 c# wcf

我正在尝试使用最小的WCF/https示例,但我无法访问该服务(令人惊讶的是代码运行没有错误).

  • 我创建了一个根证书:

    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网址预留了一个端口:

    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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用以下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>
Run Code Online (Sandbox Code Playgroud)

小智 0

猜测您需要包含策略版本才能使其运行如下

<serviceMetadata httpGetEnabled="True"  policyVersion="Policy12"></serviceMetadata>
Run Code Online (Sandbox Code Playgroud)