0

我已经创建了测试自托管 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");

因为“你好”不正确?

谢谢。

4

2 回答 2

0
 sh.AddServiceEndpoint(c, b, "https://xxxx:xx/service");
于 2010-06-17T12:35:59.657 回答
0

基本上,in的第三个参数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")
于 2010-06-17T12:39:34.127 回答