2012-05-02 10 views
10

Używam ASP.Net MVC 3 (Entity Framework) z serwerem Sql 2008. Ciąg połączenia jest w porządku w sieci .config. Ale czasami pojawia się następujący wyjątek z mojej aplikacji:Wystąpił błąd związany z siecią lub wystąpieniem konkretnej instancji podczas nawiązywania połączenia z serwerem SQL - ASP.NET MVC Entity Framework

System.Data.SqlClient.SqlException: 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) 

Generated: Wed, 02 May 2012 05:05:21 GMT 

System.Data.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: 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) 
    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 
    at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity) 
    at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) 
    at System.Data.SqlClient.SqlInternalConnectionTds.LoginWithFailover(Boolean useFailoverHost, ServerInfo primaryServerInfo, String failoverHost, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) 
    at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) 
    at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) 
    at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) 
    at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) 
    at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) 
    at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) 
    at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) 
    at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) 
    at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) 
    at System.Data.SqlClient.SqlConnection.Open() 
    at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) 
    --- End of inner exception stack trace --- 
    at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) 
    at System.Data.EntityClient.EntityConnection.Open() 
    at System.Data.Objects.ObjectContext.EnsureConnection() 
    at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) 
    at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() 
    at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source) 
    at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence) 
    at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot) 
    at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression) 
    at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source) 
    at MyApp.Data.UserRepository.GetUserDetails(String username) 
    at MyApp.Core.Services.SiteService.GetUserDetails(String username, Int64 userId) 
    at MyApp.PublicSite.Web.Controllers.BaseController.Initialize(RequestContext requestContext) 
    at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) 
    at MyApp.PublicSite.Web.Controllers.BaseController.Execute(RequestContext requestContext) 
    at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) 
    at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5() 
    at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0() 
    at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() 
    at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d() 
    at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) 
    at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) 
    at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) 
    at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

ciąg połączenia jest:

<connectionStrings> 
    <add name="MyAppDBContainer" connectionString="metadata=res://*/MyAppDB.csdl|res://*/MyAppDB.ssdl|res://*/MyAppDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SERVERNAME;initial catalog=MyAppDB;persist security info=True;user id=USERID;Password=PASSWORD;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 

Ten wyjątek występuje losowo i nie jest w stanie dostać/odtworzyć go za każdym razem. Jakieś możliwe ustawienia, których może tu brakować? Proszę zasugeruj.

+0

Gdy występuje ten błąd, Czy Twój komputer w sieci, czy nie? Myślę, że powinieneś najpierw sprawdzić połączenie LAN lub Internet. –

+0

W lokalnym nie dzieje się, otrzymuję ten wyjątek tylko z wdrożonego serwera. – Prasad

+0

@Prasad sprawdzam ciąg połączenia, który dostarczyłem .... –

Odpowiedz

2

Biorąc pod uwagę fakt, że czasami/przeważnie działa (jak mówisz):

  • Ciąg połączenia jest OK
  • SQL jest skonfigurowany prawo (czyli pozwala na zdalne połączenia etc.)

Jedynym powodem może być to, że sieć spadła między twój serwer (twój kod) i SQL Server, lub SQL Server jest zamknięty z jakiegoś powodu.

+0

Po uzyskaniu dostępu do tej samej strony, od której otrzymałem ten wyjątek, działa, więc wyłączenie serwera może nie być uzasadnione. A także strona internetowa i serwer sql znajdują się na tym samym serwerze, więc nie jestem pewien, czy powodem może być spadek sieci. – Prasad

+0

[Googling] (https://www.google.com/search?sourceid=chrome&ie=UTF-8&q=A+nazwiązane z siecią-lub+urządzenie-przypominające+wsparcie+uzyskanie+uzyskiwania+złączenia+do połączenia + SQL + Server) ten błąd daje wiele możliwych rozwiązań. Przezwyciężyłeś je? Ponadto, w szczególności, proszę uruchomić 'ping -t localhost' (lub nazwę komputera SQL), pozostawić go uruchomionym przez jakiś czas i sprawdzić, czy od czasu do czasu tracisz pakiety. –

+0

Próbowałem tych opcji od Google, wszystkie opcje są tam poprawnie skonfigurowane, ale wciąż otrzymuję ten wyjątek :(. Sprawdziłem również ping i tam jest odpowiedź bez żadnych błędów – Prasad

0

Spróbuj tego ....

<add name="MyAppDBContainer" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string='data source=SERVERNAME;initial catalog=MyAppDB;persist security info=True;user id=USERID;Password=PASSWORD;multipleactiveresultsets=True;'" providerName="System.Data.EntityClient" /> 

Mam edytowane moją odpowiedź .....

+0

Daje błąd: System.ArgumentException: Brak niektórych wymaganych informacji z ciągu połączenia Słowo kluczowe "dostawca" jest zawsze wymagane – Prasad

+0

naim, zmieniłem ciąg połączenia z tym, który zaktualizowałeś i nadal generuje błąd połączenia czasami – Prasad

+0

czasami !, więc to działa przez jakiś czas, a czasem nie ... –

0

Mamy Aby sprawdzić następujące rzeczy dla tego

1. Sprawdź ustawienia Tcp/IP w ustawieniach menedżera konfiguracji sqlserver.Jeśli jest wyłączony, make sure we have to enable the status for TCP/IP

2.Dodaj port do wyjątku ściany przeciwpożarowej przy ustawieniach ognia. Szczegóły SQL porcie:

Nazwa: SQL

Numer portu: 1433

Protokół: Wybierz TCP

Powiązane problemy