2016-06-20 7 views
0

Hoffe, dass Sie mir helfen können!WCF Self-hosted Commandline Rückgabe 503 Fehler

Ich habe eine Self-gehostete WCF-Commandline über HTTPS mit X509-Zertifikat und ich bekomme immer einen Http 503 Fehler. O ist derzeit Windows 8.1 mit VS Gemeinschaft 2015. Am Ende wird diese Kommandozeile in einem Windows-Dienst integriert werden ...

Meine C# -Code für WCF-Dienst ist diese:

using System; 
using System.ServiceModel; 
using WCF_Service_Library; //Contract and operations are defined in this library 
using System.Threading; 
using System.Globalization; 
using System.Security.Cryptography.X509Certificates; 
using System.ServiceModel.Description; 
using System.Net; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Reflection; 

namespace Service_Commandline 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     {    
      ServiceHost serviceHost; 

      //Get base Url for Web Service WCF 
      string addressHttps; 
      string CurrentDnsHostName = Dns.GetHostEntry("").HostName; 

      addressHttps = String.Format("https://" +CurrentDnsHostName + ":443"); 

      BasicHttpBinding wsHttpBinding =new BasicHttpBinding(); 
      wsHttpBinding.Security.Mode = BasicHttpSecurityMode.Transport; 

      serviceHost = new ServiceHost(typeof(PCsService), new Uri(addressHttps)); 

      //Add service endpoint 
      Type endpoint = typeof(IPCsService); 

      serviceHost.AddServiceEndpoint(endpoint, wsHttpBinding, "WCF"); 

      //Search for X509 certificate 
      X509Certificate2 certFound = FindX509Certificate("server"); 

      if (certFound!=null) 
       serviceHost.Credentials.ServiceCertificate.Certificate=certFound; 

      serviceHost.Credentials.ClientCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck; 

      Uri uri = new Uri(serviceHost.Description.Endpoints[0].ListenUri.AbsoluteUri + "/mex"); 

      //Add a behavior linked to Mex endpoint 
      ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
      smb.HttpsGetEnabled = true; 
      smb.HttpsGetUrl = uri; 
      serviceHost.Description.Behaviors.Add(smb); 

      Console.Out.WriteLine("Mex address " + smb.HttpsGetUrl); 
      try 
      { 
       serviceHost.Open(); 

       string address = serviceHost.Description.Endpoints[0].ListenUri.AbsoluteUri; 
       Console.WriteLine("Listening @ {0}", address); 
       //Certificate is: 
       Assembly assembly = typeof(Program).Assembly; 
       GuidAttribute attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]; 
       string appIdInString = attribute.Value; 
       Console.WriteLine("AppId is: " + appIdInString); 
       if (certFound != null) 
       { 
        Console.WriteLine("Certificate is: " + certFound.FriendlyName); 
        Console.WriteLine("Certificate thumbprint is: " + certFound.Thumbprint); 
       } 
       else 
       { 
        Console.WriteLine("No certificate used..."); 
       } 
       Console.WriteLine("Press enter to close the service"); 
       Console.ReadLine(); 

      } 
      catch (CommunicationException ce) 
      { 
       Console.WriteLine("A commmunication error occurred: {0}", ce.Message); 
       Console.WriteLine(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("An unforseen error occurred: {0}", ex.Message); 
       Console.ReadLine(); 
      } 
      finally 
      { 
       //Close WCF service whatever error found 
       if (serviceHost != null && serviceHost.State!=CommunicationState.Closed) 
        serviceHost.Close(); 
      } 
     } 

     private static X509Certificate2 FindX509Certificate(string wCFServerName, StoreName storeName=StoreName.My, StoreLocation storeLocation=StoreLocation.LocalMachine) 
     { 
      //Find valid certificate 
      X509Certificate2Collection searchForCurrentServerCertificate = new X509Certificate2Collection(); 
      X509Certificate2 certFound = new X509Certificate2(); 
      X509Store store=null; 

      try 
      { 
       store = new X509Store(storeName, storeLocation); 

       store.Open(OpenFlags.ReadOnly); 
       searchForCurrentServerCertificate = store.Certificates.Find(X509FindType.FindBySubjectName, wCFServerName, true); 

       certFound = null; 
       if (searchForCurrentServerCertificate != null && searchForCurrentServerCertificate.Count > 0) 
       { 
        certFound = searchForCurrentServerCertificate[0]; 
       } 
       store.Close(); 
      } 
      catch (Exception) 
      { 

       if (store != null) 
        store.Close(); 
      } 

      return certFound; 
     } 
    } 
} 

Ausgabe dieses C# -Code ist:

Mex address https:// dev01/WCF/mex 
Listening @ https:// dev01/WCF 
AppId is: f8939c9f-46e7-4d85-ba57-045e68e7fe44 
Certificate is: Certificate for this app on port 443 
Certificate thumbprint is: 5A26F1C44DE99C0FEB3FB89C80664B9619211696 
Press enter to close the service 

ich auch Namensraum mit cmd reserviert haben:

netsh http add urlacl url= https:// +:443/WCF/ user="\Everyone" 

Und tach X509-Zertifikat auf Port 443 mit cmd:

netsh http add sslcert ipport=0.0.0.0:443 certhash=5a26f1c44de99c0feb3fb89c80664b9619211696 appid={f8939c9f-46e7-4d85-ba57-045e68e7fe44} certstorename=MY 

Schließlich diese 2 Kommandozeilen scheinen ok nach diesen Ausgängen:

C:\Windows\system32>netsh http show urlacl url=https:// +:443/WCF/ 

Réservations d'URL : 
-------------------- 

    URL reserved   : https:// +:443/WCF/ 
     Utilisateur : \Everyone 
      Écouter : Yes 
      Déléguer : No 
      SDDL : D:(A;;GX;;;WD) 

C:\Windows\system32>netsh http show sslcert ipport=0.0.0.0:443 

Liaisons de certificat SSL : 
---------------------------- 

    Adresse IP:port      : 0.0.0.0:443 
    Hachage du certificat    : 5a26f1c44de99c0feb3fb89c80664b9619211696 

    ID de l'application    : {f8939c9f-46e7-4d85-ba57-045e68e7fe44} 
    Nom du magasin de certificats :  : (null) 
    Vérifier la révocation des certificats clients : Enabled 
    Vérifier la révocation au moyen du certificat client mis en cache uniquement 
 : Disabled 
    Vérification de l'utilisation     : Enabled 
    Heure d'actualisation de la révocation : 0 
    Délai d'attente de la récupération d'URL  : 0 
    Identificateur CTL    : (null) 
    Nom du magasin CTL    : (null) 
    Utilisation du mappeur DS    : Disabled 
    Négocier le certificat client : Disabled 

Und ich habe keine andere reservierte URL auf https://...443:/WCF/.

Was auch immer ich versuchte, bekomme ich einen 503 Fehler auf https: // DEV01: 443/WCF /. Aber mex Endpunkt scheint auf https: // DEV01: 443/WCF/Mex in Ordnung zu sein. Ich bekomme ein Xml-Ergebnis. Nur eine Sache scheint mir in Mex Xml seltsam: targetNamespace ist in Mex-Endpunkt gleich "http: // Tempuri.org/".

Bitte helfen Sie mir zu diesem Thema!

Grüße, François

+0

Ich kann auch sagen, dass X509-Zertifikat von einer CA geliefert wird und sein Hostname (CN = XXXXX.XXXXX.fr) entspricht nicht meinem Dev-Hostname ... Kann das Problem hier sein? – Francois

+0

Ich habe ein persönliches Zertifikat mit makecert.exe erstellt, aber ich habe immer noch 503 Fehler ... – Francois

Antwort

0

ich einen Weg fand schließlich diese selbst gehosteten haben Dienst arbeiten. Ich verwende keine Parameter mehr im Code. Ich benutze eine app.config, die 99% meiner serviceHost-Konfiguration definiert und fügen Sie einfach einige Parameter im Code (wie Zertifikatsverwaltung).

Soweit ich weiß, hatte dieses Beispiel 0 Chancen, so zu arbeiten! Viele Jungs erklären das Gegenteil, aber sie haben sich geirrt.

Also, mit app.config, ist mein Code ganz einfach:

static void Main(string[] args) 
    {    
     ServiceHost serviceHost; 

     serviceHost = new ServiceHost(typeof(PCsService)); 

     try 
     { 
      serviceHost.Open(); 
      ... 
      ... 

Hoffe, dass es jemand anderes helfen!

Grüße.