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

基本的に、の3番目のパラメータ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 に答える