我创建了测试自托管的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