how to resolve web service error log

Arash Ghadakchi 20 Reputation points
2024-03-17T10:52:49.85+00:00

Hello. i wrote a c# web service which works via ip address but when i redirect my address to an encrypted mode (SSL) , the ip does not work any longer and web service log generates an error which is here : "On Elapsed Time The request was aborted: Could not create SSL/TLS secure channel"

"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

how can i solve this error ?

please help me .

great regards

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,649 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2024-03-18T02:32:32.0366667+00:00

    Hi @Arash Ghadakchi ,Welcome to Microsoft Q&A,

    If you use IP direct connection, you can connect. This could be due to several reasons, such as the SSL certificate not being trusted or not being configured correctly.

    You can try this experience-filled post:The request was aborted: Could not create SSL/TLS secure channel

    You can try the following example:

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    
    namespace xxx
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                // Specify the base address of your service
                Uri baseAddress = new Uri("https://yourdomain.com/YourService");
    
                // Create a ServiceHost instance
                using (ServiceHost host = new ServiceHost(typeof(YourService), baseAddress))
                {
                    try
                    {
                        // Enable metadata publishing
                        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                        smb.HttpsGetEnabled = true;
                        host.Description.Behaviors.Add(smb);
    
                        // Specify the binding with SSL
                        BasicHttpsBinding binding = new BasicHttpsBinding();
    
                        // Enable SSL certificate validation
                        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
    
                        // Add the endpoint
                        host.AddServiceEndpoint(typeof(IYourService), binding, "");
    
                        // Open the ServiceHost to start listening for messages
                        host.Open();
    
                        Console.WriteLine("The service is ready at {0}", baseAddress);
                        Console.WriteLine("Press <Enter> to stop the service.");
                        Console.ReadLine();
    
                        // Close the ServiceHost
                        host.Close();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("An error occurred: {0}", ex.Message);
                        host.Abort();
                    }
                }
    
            }
    
    
        }
        // Define your service contract and implementation
        [ServiceContract]
        public interface IYourService
        {
            [OperationContract]
            string YourMethod();
        }
    
        public class YourService : IYourService
        {
            public string YourMethod()
            {
                return "Hello, World!";
            }
        }
    
    }
    
    

    Replace "https://yourdomain.com/YourService" with your actual HTTPS endpoint. Enabled ServiceMetadataBehavior to publish metadata over HTTPS. BasicHttpsBinding is used to configure basic HTTPS binding. binding.Security.Transport.ClientCredentialType Set to HttpClientCredentialType.None to disable SSL certificate verification for the client credential type. Replace YourMethod with your actual service method. Make sure you have a valid SSL certificate installed on your server and configured correctly. Also, make sure your clients trust the SSL certificate authority.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful