WCF Web服务,返回错误/exception字符串

我有一组WCF Web服务,如果发生exception,将使用OutgoingWebResponseContext将错误描述返回给调用者。

我的问题是,有时,当出现问题时,Web服务显示为失败 ,带有ERR_CONNECTION_RESET消息,而不是返回错误消息,我想防止这种情况发生。

例如,这是我的漂亮的C#代码,它查询我的Northwind数据库,并返回一个Customer名称列表。

或者更确切地说, 它会这样做 ,但我故意在那里插入一个例外。

 public List GetAllCustomerNames() { // Get a list of unique Customer names. // try { throw new Exception("Oh heck, something went wrong !"); NorthwindDataContext dc = new NorthwindDataContext(); var results = (from cust in dc.Customers select cust.CompanyName).Distinct().OrderBy(s => s).ToList(); return results; } catch (Exception ex) { OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse; response.StatusCode = System.Net.HttpStatusCode.InternalServerError; response.StatusDescription = ex.Message; return null; } } 

在我的AngularJS控制器中,我可以调用此Web服务…

 $http.get('http://localhost:15021/Service1.svc/getAllCustomerNames') .then(function (data) { // We successfully loaded the list of Customer names. $scope.ListOfCustomerNames = data.GetAllCustomerNamesResult; }, function (errorResponse) { // The WCF Web Service returned an error var HTTPErrorNumber = errorResponse.status; var HTTPErrorStatusText = errorResponse.statusText; alert("An error occurred whilst fetching Customer Names\r\nHTTP status code: " + HTTPErrorNumber + "\r\nError: " + HTTPErrorStatusText); }); 

…如果发生此exception,我确实会看到完整的exception消息……

在此处输入图像描述

精彩。

对于所有想要了解从WCF Web服务返回和显示例外的简单通用方法的读者,有您的解决方案!

好的,现在我将删除我的Exception代码行,并且Web服务再次正常运行。 Customer数据将毫无问题地返回到Angular控制器。

但是..如果连接到数据库时发生exception(例如,它超时,我的连接字符串中有错误的数据库名称等),那么以下代码行引发exception……

 NorthwindDataContext dc = new NorthwindDataContext(); 

…我的catch代码确实启动了(我在它上面放了一个断点来检查),然后我们将StatusDescription设置为Exception消息。

例外

但是发回的响应不包含我的HTTP状态号或文本。

我的Angular控制器只接收HTTP状态代码0,没有StatusMessage。

如果我在Chrome中点击F12,您可以看到它实际上说Web服务失败了 ,而不是返回状态码403,并且没有消息。

在此处输入图像描述

所以我的问题很简单,有没有办法防止这种失败发生?

显然,它更友好地返回对错误的描述,而不仅仅是“网络服务失败了……但我们无法告诉你为什么。”

如果我能解决这个问题,这将是一个很好的方式,使我们的内部应用程序中的所有WCF Web服务错误消息友好。 但是,当然,对于生产系统,当然,你真的不想用技术exception消息轰炸用户。

几小时后更新..

我找到了原因和解决方案。

你准备好了吗?

问题是当DataContext行引发exception时……

 NorthwindDataContext dc = new NorthwindDataContext(); 

…exception消息中包含换行符

 Cannot open database "Northwind" requested by the login. The login failed.Login failed for user 'MikesWrongUserName'.This session has been assigned a tracing ID of '1f0513d1-9ce1-47ef-9d44-1f4546eb0b73'. Provide this tracing ID to customer support when you need assistance. 

换行导致了这个问题。 如果您尝试在StatusDescription字段中返回该StatusDescription ,则会导致整个Web服务报告为“失败”。

因此,解决方案是在返回exception消息之前简单地删除任何换行符。

最后,这里是一个try..catch在WCF Web服务中使用try..catch的示例,因此它们总是将exception消息返回给调用者。

 public List GetAllCustomerNames() { // Get a list of unique Company Names. // try { NorthwindDataContext dc = new NorthwindDataContext(); var results = (from cust in dc.Customers select cust.CompanyName).Distinct().OrderBy(s => s).ToList(); return results; } catch (Exception ex) { OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse; response.StatusCode = System.Net.HttpStatusCode.InternalServerError; // If a database Exception occurs, it might have a line-feed in it, // which will cause the web service to completely fail (no Status // Code or Status Message will get returned.) response.StatusDescription = ex.Message.Replace("\r\n", ""); return null; } } 

感谢您的所有建议!