Search code examples
azure-cognitive-search

Getting socket error while accessing azure cognitive search index


I have created an index in the azure portal and I am trying to access the index in my code to get the data. Everytime I do it I get a {System.Net.Sockets.SocketException (11001): No such host is known at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)}.

Is there anything I can do for this?

Below is my code

 static void Main(string[] args)
        {
            Index();
        }
        public static  DocumentSearchResult<SearchResult> Index()
        {
            IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            IConfigurationRoot configuration = builder.Build();

            string serviceName = configuration["SearchServiceName"];
            string key = configuration["SearchServiceQueryApiKey"];

            //Creating search client
            SearchServiceClient serviceClient = new SearchServiceClient(serviceName, new SearchCredentials(key));

            SearchIndexClient indexClient = new SearchIndexClient(serviceName, "cognitivesearchpoc", new SearchCredentials(key));


            SearchParameters parameters;
            DocumentSearchResult<SearchResult> results;

            parameters = new SearchParameters() { Select = new[] { "*" } };

            return  indexClient.Documents.Search<SearchResult>("*");
        }

Solution

  • The error message is telling you that the host name to which you’re trying to connect is incorrect. Use just the host name portion of your service endpoint as the service name. In your case, that’s just “devglobalsearch” without the DNS suffix.