2010-06-17 70 views
0

我创建了测试自托管的wcf应用程序,并尝试添加支持https。服务器应用程序的 代码是:WCF服务:WSHttpBinding

using System; 
using System.Security.Cryptography.X509Certificates; 
using System.ServiceModel; 
using System.ServiceModel.Description; 
using System.ServiceModel.Security; 

namespace SelfHost 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string addressHttp = String.Format("http://{0}:8002/hello", System.Net.Dns.GetHostEntry("").HostName); 
      Uri baseAddress = new Uri(addressHttp); 
      WSHttpBinding b = new WSHttpBinding(); 
      b.Security.Mode = SecurityMode.Transport; 
      b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; 
      Uri a = new Uri(addressHttp); 
      Uri[] baseAddresses = new Uri[] { a }; 
      ServiceHost sh = new ServiceHost(typeof(HelloWorldService), baseAddresses); 
      Type c = typeof(IHelloWorldService); 
      sh.AddServiceEndpoint(c, b, "hello"); 
      sh.Credentials.ServiceCertificate.SetCertificate(
       StoreLocation.LocalMachine, 
       StoreName.My, 
       X509FindType.FindBySubjectName,"myCert"); 
      sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode = 
      X509CertificateValidationMode.PeerOrChainTrust; 
      try 
      { 
       sh.Open(); 

       string address = sh.Description.Endpoints[0].ListenUri.AbsoluteUri; 
       Console.WriteLine("Listening @ {0}", address); 
       Console.WriteLine("Press enter to close the service"); 
       Console.ReadLine(); 
       sh.Close(); 
      } 
      catch (CommunicationException ce) 
      { 
       Console.WriteLine("A commmunication error occurred: {0}", ce.Message); 
       Console.WriteLine(); 
      } 
      catch (System.Exception exc) 
      { 
       Console.WriteLine("An unforseen error occurred: {0}", exc.Message); 
       Console.ReadLine(); 
      } 
     } 
    } 
    [ServiceContract] 
    public interface IHelloWorldService 
    { 
     [OperationContract] 
     string SayHello(string name); 
    } 

    public class HelloWorldService : IHelloWorldService 
    { 
     public string SayHello(string name) 
     { 
      return string.Format("Hello, {0}", name); 
     } 
    } 
} 

什么名字(地址),我应该伸到线

sh.AddServiceEndpoint(c, b, "hello"); 

因为“你好”是不正确的?

谢谢。

+0

你说的 “不正确” 是什么意思?你会得到一个错误 - 如果是这样,它是什么?你没有得到一个错误,但服务没有按预期工作? – 2010-06-17 12:37:58

+0

是的,我得到文本错误: 无法找到与绑定WSHttpBinding的端点匹配scheme https的基地址。注册的基地址方案是[http]。 – jitm 2010-06-17 12:39:58

回答

0
sh.AddServiceEndpoint(c, b, "https://xxxx:xx/service"); 
+0

Like string addressHttp = String.Format(“http:// {0}:8002/hello”,System.Net.Dns.GetHostEntry(“”)。HostName);只有变更是https? – jitm 2010-06-17 12:38:53

0

基本上,AddServiceEndpoint中的第三个参数是服务的地址。

如果您已经定义了基地址(因为您有- http://{0}:8002/hello),它是一个相对地址 - 它将被添加到相应协议的基地址。

你的情况

因此,加入此服务端点,你会得到在端点:

http://{0}:8002/hello/hello 

您可以连接到端点,跟我们的服务?

或者你也可以定义一个完全指定的地址 - 如果你没有任何基地址,这个特别有用。如果指定完整地址,则将使用该地址(覆盖定义的基址)。因此,如果您使用:

AddServiceEndpoint(c, b, "http://server:8888/HelloService") 

那么您的服务将可以在该特定的URL访问 - 无论您之前定义的基地址如何。

更新:感谢您的评论。是的,如果您将安全模式定义为“传输”,那么您需要为所有地址使用https://

定义基地址:

string addressHttp = String.Format("https://{0}:8002/hello", System.Net.Dns.GetHostEntry("").HostName); 

或压倒一切的一个完全合格的地址:

AddServiceEndpoint(c, b, "https://server:8888/HelloService") 
+0

谢谢,目前服务器启动没有错误,但如果我尝试绑定客户端到服务器,我收到下一个错误: 下载'https:// server:8002/hello'时出错。 底层连接已关闭:发送时发生意外错误。 验证失败,因为远程方关闭了传输流。 元数据包含无法解析的引用:'https:// server:8002/hello'。 继续在下一条评论中显示错误消息... – jitm 2010-06-17 12:53:33

+0

将HTTP请求发送到https:// server:8002/hello时发生错误。这可能是由于在HTTPS情况下服务器证书未使用HTTP.SYS正确配置。这也可能是由于客户端和服务器之间的安全绑定不匹配造成的。 底层连接已关闭:发送时发生意外错误。 验证失败,因为远程方关闭了传输流。 如果服务在当前解决方案中定义,请尝试构建解决方案并再次添加服务引用。 如何解决它? – jitm 2010-06-17 12:54:08

+0

如果我做httpcfg查询ssl 我可以看到可配置的HTTP.SYS -------------------------------- ---------------------------------------------- IP:0.0 .0.0:8002 哈希:1111111111111111111111111111111111111 GUID:{00000000-0000-0000-0000-000000000000} CertStoreName:(空) CertCheckMode:0 RevocationFreshnessTime:0 UrlRetrievalTimeout:0 SslCtlIdentifier:(空) SslCtlStoreName:( null) 标记:0 – jitm 2010-06-17 13:04:48

相关问题