WebException or Remote server name could not be resolved in WCF Data Service

No.of Views3266
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  22 Aug 2010 03:08:43
Tag : WCF , Utilities
This article will give a brief explanation on how to handle remote server name could not resolved exception in WCF Data Service
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will give a brief explanation on how to handle remote server name could not resolved exception in WCF Data Service

If you are new to this topic, please read Introduction to WCF Data service and ODATA  before going through below article.

Let us say, there is a WCF Data Service up and running on a particular server.  We access that using below code at the client side

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using ConsoleApplication1.ServiceReference1; 
namespace ConsoleApplication1
{class Program
    {static void Main(string[] args)
        {
            DataServiceContext context = new DataServiceContext(new Uri("http://localhost:61091/WcfDataService1.svc/"));
          
            DataServiceQuery<Employee> query = context.CreateQuery<Employee>("Employees");foreach (Employee e in query)
            {
                Console.WriteLine(e.FirstName);
            }
            Console.Read();
        }
    }
}

And output is as below,

 

Image Loading

Every this is fine till this point.  Now let us go ahead and modify the WCF Data Service URL to a false URL 

Image Loading

And URL, I am giving is false one. Server abc does not exist.  Now after modification code will look like

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using ConsoleApplication1.ServiceReference1; 
namespace ConsoleApplication1
{class Program
    {static void Main(string[] args)
        {
            DataServiceContext context = new DataServiceContext(new Uri("http://abc:61091/WcfDataService1.svc/"));
          
            DataServiceQuery<Employee> query = context.CreateQuery<Employee>("Employees");foreach (Employee e in query)
            {
                Console.WriteLine(e.FirstName);
            }
            Console.Read();
        }
    }
}

And when you try to run, you will get the run time exception as below, 

Image Loading

Now to handle this exception put the foreach statement in try catch , so no modified code will look like

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using ConsoleApplication1.ServiceReference1; 
namespace ConsoleApplication1
{class Program
    {static void Main(string[] args)
        {
            
            DataServiceContext context = new DataServiceContext(new Uri("http://abc:61091/WcfDataService1.svc/"));
          
            
            DataServiceQuery<Employee> query = context.CreateQuery<Employee>("Employees");try{foreach (Employee e in query)
                {
                    Console.WriteLine(e.FirstName);
                }
            }catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }
}

And now running output will be as below, 

Image Loading

that's all. enjoy the bug free coding.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</