【发布时间】:2010-06-17 12:30:15
【问题描述】:
我已经创建了测试自托管 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");
因为 "hello" 不正确?
谢谢。
【问题讨论】:
-
“不正确”是什么意思?你得到一个错误 - 如果是这样,它是什么?您是否没有收到错误消息,但服务未按预期工作?
-
是的,我收到文本错误:找不到与绑定 WSHttpBinding 的端点的方案 https 匹配的基地址。注册的基地址方案是 [http]。
标签: c# wcf wshttpbinding