【问题标题】:WCF Service : WSHttpBindingWCF 服务:WSHttpBinding
【发布时间】:2010-06-17 12:30:15
【问题描述】:

我已经创建了测试自托管 wcf 应用程序并尝试添加支持 https。 服务器应用程序代码为:

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");

因为 "hello" 不正确?

谢谢。

【问题讨论】:

  • “不正确”是什么意思?你得到一个错误 - 如果是这样,它是什么?您是否没有收到错误消息,但服务未按预期工作?
  • 是的,我收到文本错误:找不到与绑定 WSHttpBinding 的端点的方案 https 匹配的基地址。注册的基地址方案是 [http]。

标签: c# wcf wshttpbinding


【解决方案1】:
 sh.AddServiceEndpoint(c, b, "https://xxxx:xx/service");

【讨论】:

  • 点赞字符串地址Http = String.Format("http://{0}:8002/hello", System.Net.Dns.GetHostEntry("").HostName);唯一的变化是 https ?
【解决方案2】:

基本上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")

【讨论】:

  • 谢谢,目前服务器启动时没有错误,但如果我尝试将客户端绑定到服务器,我会收到下一个错误:下载“server:8002/hello”时出错。底层连接已关闭:发送时发生意外错误。身份验证失败,因为远程方已关闭传输流。元数据包含无法解析的引用:“server:8002/hello”。在下一条评论中继续错误消息...
  • server:8002/hello 发出 HTTP 请求时出错。这可能是由于在 HTTPS 情况下未使用 HTTP.SYS 正确配置服务器证书。这也可能是由于客户端和服务器之间的安全绑定不匹配造成的。底层连接已关闭:发送时发生意外错误。身份验证失败,因为远程方已关闭传输流。如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。如何解决?
  • 如果我做 httpcfg query ssl 我可以看到可配置的 HTTP.SYS ---------------------------- -------------------------------------------------- - IP:0.0.0.0:8002哈希:11111111111111111111111111111111指南:{00000000-0000-0000-0000-0000-0000-000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000Y0000000000来证券0
猜你喜欢
  • 2017-04-03
  • 2018-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 2011-02-15
  • 1970-01-01
相关资源
最近更新 更多