Как настроить HTTPS в netHttpBinding в wcf?C#

Место общения программистов C#
Anonymous
Как настроить HTTPS в netHttpBinding в wcf?

Сообщение Anonymous »

Мне нужно создать пример проекта, в котором клиент и сервер взаимодействуют с netHttpBinding через https(wss). Мой код и конфигурация отлично работают с привязкой HTTP. Но когда я использую HTTPS, выдает следующую ошибку:

System.ServiceModel.CommunicationException: «Произошла ошибка при выполнении HTTP-запроса к https://». локальный хост: 8080/NetHttp. Это может быть связано с тем, что сертификат сервера неправильно настроен с помощью HTTP.SYS в случае HTTPS. Это также может быть вызвано несоответствием привязки безопасности между клиентом и сервером.'

Код и конфигурации следующие
  • IHTTPService.cs (ServiceContract)

Код: Выделить всё

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace NetHttpBindingPocSvc
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract(CallbackContract = typeof(IDuplexServiceCallBack))]
public interface IHttpService
{

[OperationContract]
void SendMessage(string message);
}

public interface IDuplexServiceCallBack
{
[OperationContract]
void ReceiveMessage(string message);
}
}
  • Внедрение услуг

Код: Выделить всё

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class HttpService : IHttpService
{
public void SendMessage(string message)
{
Console.WriteLine($"Message from client received: {message}");

var callback = OperationContext.Current.GetCallbackChannel();
if (callback != null)
{
callback.ReceiveMessage("Message has been received");
}
}
  • Хостинг услуги

Код: Выделить всё

 internal class Program
{
static void Main(string[] args)
{
var serviceInstance = new ServiceHost(typeof(NetHttpBindingPocSvc.HttpService));
serviceInstance.Open();
Console.WriteLine($"Service is up and running");
Console.ReadLine();
}
}
  • App.Config (Сервер)
  • Код на стороне клиента (потребление услуг)

Код: Выделить всё

   internal class Program
{
static void Main(string[] args)
{

var message = Console.ReadLine();
string url = $"wss://localhost:8080/NetHttp";
var instanceContext = new InstanceContext(new DuplexMessageReceiver());
NetHttpService.HttpServiceClient client = new NetHttpService.HttpServiceClient(instanceContext);
client.Endpoint.Address = new System.ServiceModel.EndpointAddress(new Uri(url), EndpointIdentity.CreateDnsIdentity("localhost"));
//client.ClientCredentials.ClientCertificate.SetCertificate(System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, "localhost");
client.Open();
client.SendMessage(message);
Console.ReadLine();
}
public class DuplexMessageReceiver : NetHttpService.IHttpServiceCallback
{
public void ReceiveMessage(string message)
{
Console.WriteLine(message);
}
}
}
  • Конфигурация на стороне клиента
Я не уверен, что я сделал не так. Та же конфигурация сертификата отлично работает для привязки net.tcp


Подробнее здесь: https://stackoverflow.com/questions/787 ... ing-in-wcf

Вернуться в «C#»