Skip to main content

SocketException: No such host is known

Visual Studio provides all-in-one experience for software developers. From coding to testing, it makes IT professional’s lives easier. Error messages like “Socket Exception: No such host is known” refers to certain issues in the code or configuration of the application. In this post, I will explain the root cause and solution for this error message.

This error message may appear in may forms. Here is one of them:

System.Net.Sockets.SocketException: No such host is known

Error "No such host is known"

In some cases, you may see the exception code 0x80004005 along with the error message above. In addition, if you are receiving ThreadAbortException, check this post out.

The root cause of the “No such host is known” error

In the scenario I tested and in the online posts I reviewed, the root cause was the same: DNS name resolution failure. Basically, your application tries to call an API URL or to connect to a service but it is not able to find the hostname or your server is not able to do DNS lookup for that hostname.

Since it is an issue with DNS name resolution, nothing you do with Visual Studio, IIS, TFS or Windows will help fixing this issue. However, a simple code update may do the magic!

Similar issues on external sites: CodeProject, GitHub, Microsoft Support.

Another Visual Studio error: Remote Debugging Monitor (MSVSMON.EXE)

Solution for “No such host is known” error

The solution is fairly simple. Make sure your server is able to access to the DNS server and your DNS server is able to lookup the hostname in your code. Follow the steps below to troubleshoot and solve this issue.

  • In Command Prompt, run ipconfig /all. This will list the IP addresses of the DNS server you use. Try to ping them. Additionally, check with a colleague to see he or she has the same configuration
  • Run nslookup. Enter the hostname in your code. Make sure the DNS server is able to lookup this hostname.
  • Make sure there is no typo or white spaces in the hostname. If you are getting this input from a JSON call or configuration file, it is possible that the source string and the variable you have are different.

If this is an ASP.NET WebForms application, you may also come across Validation of viewstate MAC failed error. Follow the link for the specific solution for it.

How to add JavaScript pages to React Core Boilerplate template?

If you use a Visual Studio template that comes with TypeScript by default, you may need to extend the capability of it to use JavaScript. In this post, we will learn how to add JavaScript pages to React Core Boilerplate template.

React is a JavaScript library that is developed by Facebook and used by developers to create interactive user interfaces. It updates and render only the specific components on your page when the data changes.

React Core Boilerplate is a Visual Studio template that helps developers to easily embed and use React in their web applications.

Add JavaScript pages to React Core Boilerplate template

React Core Boilerplate template automatically creates TypeScript files (.tsx extension) for application logic. Therefore, you need to put some extra effort to enable this template to render JavaScript files.

Add Javascript files to React Core Boilerplate template
How a JavaScript page looks in Visual Studio’s React Core Boilerplate

Follow the steps below to add JavaScript files to React Core Boilerplate template:

  • Right click “pages” folder. Select “Add > New Item
  • Search for “JavaScript File” template. Name it as “Home.js
Add JavaScript files to React Core Boilerplate template
  • Add JS content to “Home.js”. Example: 
import React, { Component } from 'react';
 
export default class Home extends Component {
    render() {
        return (
            

Hello, world! (JS Page)

Welcome to your new single-page application

); } }
  • Open “routes.tsx” in “ClientApp” folder. Add the following 2 lines
import Home from '@Pages/Home.js';
<approute layout="{AuthorizedLayout}" exact="" path="/test" component="{Home}"></approute>
  • Open “webpack.config.js” file in the root folder. Replace the code with the following block (We are adding a line to tell compiler to render js files with babel-loader
module: {
	rules: [
		{ test: /\.tsx?$/, include: /ClientApp/, use: ['babel-loader', 'ts-loader', 'ts-nameof-loader'] },
		{
			test: /\.(gif|png|jpe?g|svg)$/i,
			use: [
				'file-loader',
				{
					loader: 'image-webpack-loader'
				}
			]
		}, 
		{ test: /\.js?$/, include: /ClientApp/, use: ['babel-loader'] }
	]
},
  • Run these commands in Command Prompt of the project root folder: 
npm install @babel/preset-env
npm install @babel/preset-react
  • Right click the project name. Select “Add > New Item
  • Search for “Text File” template. Name it as “.babelrc” 
Add JavaScript files to React Core Boilerplate template
  • Enter this content into “.babelrc” 
{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ]
}
  • Run npm run build:dev in Command Prompt. There shouldn’t be any errors or warnings in the output. If there is, they should be fixed before proceeding with the next step 
Add JavaScript files to React Core Boilerplate template

Looking for steps to publish ASP.NET web site in Visual Studio and deploy it in IIS? Check this post out.

References