我創建了測試自託管的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");
因爲「你好」是不正確的?
謝謝。
你說的 「不正確」 是什麼意思?你會得到一個錯誤 - 如果是這樣,它是什麼?你沒有得到一個錯誤,但服務沒有按預期工作? – 2010-06-17 12:37:58
是的,我得到文本錯誤: 無法找到與綁定WSHttpBinding的端點匹配scheme https的基地址。註冊的基地址方案是[http]。 – jitm 2010-06-17 12:39:58