我创建了测试自托管的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"是不正确的吗?
谢谢。
发布于 2010-06-17 12:35:59
sh.AddServiceEndpoint(c, b, "https://xxxx:xx/service");发布于 2010-06-17 12:39:34
基本上,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")https://stackoverflow.com/questions/3061585
复制相似问题