簡體   English   中英

WCF服務:WSHttpBinding

[英]WCF Service : WSHttpBinding

我創建了測試自托管的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");

因為“你好”是不正確的?

謝謝。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM