2013-08-05 11 views
7

Ok, quindi sto ospitando un servizio WCF all'interno di un'applicazione console.Il servizio WCF auto-ospitato funziona con HTTP non con HTTPS

tutti i collegamenti vengono creati in modo programmatico, quindi nessuna impostazione di configurazione.

Ho un servizio di lavoro finché io uso HttpTransportBindingElement però, non appena io uso HttpsTransportBindingElement poi niente funziona, il servizio non viene visualizzato all'interno del browser e l'applicazione client restituisce un 405 (Method Not Allowed) CommunicationException

Ho provato a fissare un SecurityBindingElement al mio CustomBinding ma non sono sicuro di quale opzione dovrei utilizzare.

SecurityBindingElement.CreateCertificateOverTransportBindingElement()

SecurityBindingElement.CreateAnonymousForCertificateBindingElement()

ecc

Il codice per la creazione del host è sotto

baseAddress = new Uri(string.Format("{0}://{1}", strConnectionType, ConfigurationManager.AppSettings["baseAddress"])); 

      ServiceHost host = new ServiceHost(typeof(IMyService), baseAddress); 

      host.AddServiceEndpoint(typeof(MyService), binding, String.Empty); 

      ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
      smb.HttpsGetEnabled = certificate != null; 
      smb.HttpGetEnabled = certificate == null; 

      host.Description.Behaviors.Add(smb); 

      ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>(); 

      if (sdb == null) 
      { 
       host.Description.Behaviors.Add(new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true }); 
      } 
      else 
      { 
       if (!sdb.IncludeExceptionDetailInFaults) 
       { 
        sdb.IncludeExceptionDetailInFaults = true; 
       } 
      } 


      if (certificate != null) 
      { 
       host.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, certificate.Thumbprint); 
      } 
+1

Che cos'è il 'binding'? Anche la configurazione programmatica dei servizi WCF è dannatamente dura, perché non usare il file di configurazione? –

+0

@ ta.speot.is potresti approfondire la tua domanda? –

+1

@ shankar-damodaran grazie per il montaggio :) –

risposta

6

Ho seguito questo blog http://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/10/selfhosting-a-wcf-service-over-https.aspx che ha evidenziato che, per HTTPS lavoro è necessario associare la porta al certificato che si sta utilizzando.

Process bindPortToCertificate = new Process(); bindPortToCertificate.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "netsh.exe");

bindPortToCertificate.StartInfo.Arguments = string.Format("http add sslcert ipport=0.0.0.0:{0} certhash={1} appid={{{2}}}", port, certificate.Thumbprint, Guid.NewGuid());

bindPortToCertificate.Start();

bindPortToCertificate.WaitForExit();

una volta che questo è stato fatto tutto ha funzionato.

Contattatemi se c'è bisogno del mio codice di esempio di configurazione e configurazione di un server WCF auto-ospitato con associazioni programmate in modo programmato. :)

Problemi correlati