Luke
Luke

Reputation: 161

INNER EXCEPTION #1: MSG: The underlying provider failed on Open. On Server

I apologize in beforehand because I see that a lot of people have already answered this question but still I cannot find the answer , I am looking for. I have one static class Which has static constructor

 public static class AppSettingsWrapper
{
    public static List<uspGetFilePath_Result> FilePaths = new List<uspGetFilePath_Result>();

    static AppSettingsWrapper()
    {
        using (MakEntities db = new MakEntities ())
        {
            FilePaths = db.uspGetFilePath().ToList();
        }
    }
}

i am accessing this property FilePaths from this function

     public static uspGetFilePath_Result GetUploadInfo(EnumHelper.FilePathDesc filePath)
    {
        try
        {
            return AppSettingsWrapper.FilePaths.Where(x => x.ID == Convert.ToInt32(filePath)).FirstOrDefault();
        }
        catch (Exception)
        {
            throw;
        }
    }

But some time whenver i call this fucntion i am getting this exception The type initializer for 'PQCTrack.Utility.AppSettingsWrapper' threw an exception.

System.TypeInitializationException

INNER EXCEPTION #1: MSG: The underlying provider failed on Open., SOURCE: EntityFramework, FILE: , METHOD: Open, LINE: 0 INNER EXCEPTION #2: MSG: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server), SOURCE: .Net SqlClient Data Provider, FILE: , METHOD: TryGetConnection, LINE: 0 INNER EXCEPTION #3: MSG: The network path was not found, SOURCE: , FILE: , METHOD: , LINE:

So whenever this issue come i have restart the IIS app pool and then issue is resolved it does not come again

When i see the error logs i see that static constructor is called many a times, becuase of which it is throwing error Can any one tell me where the issue is?

Upvotes: 0

Views: 153

Answers (1)

GDI89
GDI89

Reputation: 54

TypeInitializationException occurs when a static constructor has an error. It wraps the errors from static constructors. It cannot be reliably trapped outside of the static constructor. I think that it can be when you call method db.uspGetFilePath() or in constructor new MakEntities() where you are using Sql connection. An error in the inner exception: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible". You must verifyed your Sql connection string.

Upvotes: 1

Related Questions