嗨,我正在尝试通过 WCF 获取顾问列表,因为我收到了标题中提到的这个错误,所以我会在调试时告诉你所有代码,这样你就可以跟踪错误的位置,
当我在负载上运行表单时,
private void LoadData()
{
try
{
CIService.CIServiceClient serv = new CIServiceClient();
serv.GetConsultantListCompleted += new EventHandler<GetConsultantListCompletedEventArgs>(serv_GetConsultantListCompleted);
serv.GetConsultantListAsync();
serv.Close();
}
catch (Exception ex)
{
string error = "<script language=javascript>alert('Failed to Load Consultants at PageLoad \r\n" + ex.StackTrace + "');</script>";
if (!ClientScript.IsClientScriptBlockRegistered("ConLoadErr"))
ClientScript.RegisterClientScriptBlock(Page.GetType(), "ConLoadErr", error);
}
}
public ConsultantService():base(connectionString)
{
}
public DataAccessBase(string connectionString)
{
this.sqlCommand = new SqlCommand();
this.sqlConnection = new SqlConnection(connectionString);
this.sqlCommand.Connection = this.sqlConnection;
this.commandType = System.Data.CommandType.Text;
}
List<Consultant> ICIService.GetConsultantList()
{
ConsultantService service = new ConsultantService();
return service.GetConsultantList();
}
public List<Consultant> GetConsultantList()
{
DataAccessManager.SystemSetup sysSetup = new SystemSetup();
return sysSetup.GetConsultantList();
}
public List<Consultant> GetConsultantList()
{
InspectionEntities dbContext = new InspectionEntities();
return dbContext.Consultants.ToList();
}
public InspectionEntities() :
base("name=InspectionEntities", "InspectionEntities")
{
this.OnContextCreated();
}
public List<Consultant> GetConsultantList()
{
InspectionEntities dbContext = new InspectionEntities();
return dbContext.Consultants.ToList();
}
再次返回它时,它会在“return dbContext.Consultants.ToList();”上给出错误
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. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"})
和
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.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at DataAccessManager.SystemSetup.GetConsultantList() in G:\Design Scoring\DataAccessManager\SystemSetup.cs:line 42
at InspectionServices.ConsultantService.GetConsultantList() in G:\Design Scoring\InspectionServices\Services\ConsultantService.cs:line 112
at InspectionServices.CIService.InspectionServices.ICIService.GetConsultantList() in G:\Design Scoring\InspectionServices\CIService.cs:line 253
at SyncInvokeGetConsultantList(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
希望你的建议
谢谢
编辑:还有一件事我想提一下,在这个应用程序的其他形式上,我正在从 db 获取数据而没有任何错误