开发者

WCF Service : WSHttpBinding

开发者 https://www.devze.com 2023-01-04 07:03 出处:网络
I\'ve created the test self-hosted wcf application and tried to add support https. Code of server application is:

I've created the test self-hosted wcf application and tried to add support https. Code of server application is:

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.PeerOrCha开发者_StackOverflow社区inTrust;
            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);
        }
    }
}

What name(address) should I out into line

sh.AddServiceEndpoint(c, b, "hello");

because "hello" is incorrect ?

Thanks.


 sh.AddServiceEndpoint(c, b, "https://xxxx:xx/service");


Basically, the third parameter in AddServiceEndpoint is the address of the service.

If you have defined a base address (as you have - http://{0}:8002/hello), it's a relative address - it will be added to the base address for the appropriate protocol.

So in your case, by adding this service endpoint, you'd get an endpoint at:

http://{0}:8002/hello/hello

Can you connect to that endpoint and talk to the service??

Or you can define a fully specified address - that's especially useful if you don't have any base addresses. If you specify a full address, that address will be used (overriding the base address defined). So if you use:

AddServiceEndpoint(c, b, "http://server:8888/HelloService")

then your service will be accessible at that specific URL - regardless of your base address defined before.

Update: thanks for your comment. Yes, if you define the security mode to be "Transport", then you need to use https:// for all your addresses.

Defining base address:

string addressHttp = String.Format("https://{0}:8002/hello", System.Net.Dns.GetHostEntry("").HostName);

or overriding with a fully qualified address:

AddServiceEndpoint(c, b, "https://server:8888/HelloService")
0

精彩评论

暂无评论...
验证码 换一张
取 消