首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF服务: WSHttpBinding

WCF服务: WSHttpBinding
EN

Stack Overflow用户
提问于 2010-06-17 12:30:15
回答 2查看 5K关注 0票数 0

我创建了测试自托管的wcf应用程序,并尝试添加支持https。服务器应用程序的代码是:

代码语言:javascript
复制
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);
        }
    }
}

我应该排什么名(地址)?

代码语言:javascript
复制
sh.AddServiceEndpoint(c, b, "hello");

因为"hello"是不正确的吗?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-06-17 12:35:59

代码语言:javascript
复制
 sh.AddServiceEndpoint(c, b, "https://xxxx:xx/service");
票数 0
EN

Stack Overflow用户

发布于 2010-06-17 12:39:34

基本上,AddServiceEndpoint中的第三个参数是服务的地址。

如果您已经定义了一个基本地址(正如您已经定义了- http://{0}:8002/hello),那么它就是一个相对地址--它将被添加到相应协议的基址中。

因此,在您的示例中,通过添加此服务端点,您将在以下位置获得一个端点:

代码语言:javascript
复制
http://{0}:8002/hello/hello

您能连接到那个端点并与服务对话吗??

或者您可以定义一个完全指定的地址--如果您没有任何基址,这就特别有用。如果指定完整地址,则将使用该地址(覆盖定义的基地址)。所以如果你用:

代码语言:javascript
复制
AddServiceEndpoint(c, b, "http://server:8888/HelloService")

然后,您的服务将可以访问该特定的URL -不管您之前定义的基本地址。

更新:感谢您的评论。是的,如果您将安全模式定义为“传输”,则需要对所有地址使用https://

定义基址:

代码语言:javascript
复制
string addressHttp = String.Format("https://{0}:8002/hello", System.Net.Dns.GetHostEntry("").HostName);

或以完全限定的地址覆盖:

代码语言:javascript
复制
AddServiceEndpoint(c, b, "https://server:8888/HelloService")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3061585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档