Tag: error handling

无法在ServiceModel客户端配置部分中找到引用合同“IAuthenticationService”的默认端点元素

我尝试创建一个简单的WCF应用程序,我没有更改任何现有的默认配置。 我尝试使用使用svcutil.exe生成的TestClient来使用该服务。 它”Could not find default endpoint element that references contract ‘IAuthenticationService’ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.”显示错误消息”Could not find default endpoint element that references contract ‘IAuthenticationService’ in […]

未处理的exception未被Global.asaxerror handling程序或自定义IHttpModuleerror handling程序捕获

我有一个类(DPCal_EventMove)的方法,我想限制使用角色的访问。 我有一个Global.asax.cserror handling程序和一个自定义IHttpModuleerror handling程序,用于捕获未处理的exception和Server.Transfer他们GlobalExceptionHandler.aspx,它检查错误是否是源自失败的PrincipalPermission检查的SecurityExceptions。 由于某种原因,由PricipalPermission修饰方法引起的未处理exception不会通过我的任何error handling程序进行路由。 我的问题是:这个exception被路由到哪里以及如何捕获和处理它? public partial class DayView : Page { protected void Page_Load(object sender, EventArgs e) { // Do some stuff } [PrincipalPermission(SecurityAction.Demand, Role = “Investigator”)] [PrincipalPermission(SecurityAction.Demand, Role = “Administrator”)] protected void DPCal_EventMove(object sender, DayPilot.Web.Ui.Events.EventMoveEventArgs e) { // If no overlap, then save int eventId = Convert.ToInt32(e.Value); MembershipUser user = Membership.GetUser(); […]

C#Nested Try Catch语句或方法?

简单的最佳实践问题。 你应该嵌套try catch语句还是只使用方法。 例如,如果您有一个打开文件的方法可以正常工作并关闭文件,那么您可以在try catch外部打开和关闭,或者在finally块中关闭。 现在如果你的open方法失败了,那么该方法会断言吗? 那么你应该在try catch块中包装它还是应该从另一个方法调用它,而另一个方法又是一个try catch块?

c#插件事件处理

我编写了一个使用接口的插件系统,并且满足此合同的任何插件都在运行时加载到主系统中。 插件有效地返回插入主应用程序的TabPage,并从插件dll中进行控制。 如果插件中发生错误,则会显示标准Windows错误消息。 我想要做的是创建一个返回错误消息的事件,以便我可以在我为文本保留的区域中显示它。 我是否需要跟踪所有连接的插件/接口实例,以便能够设置一个事件来监控每个实例? 目前,我的系统循环遍历app文件夹中的dll,并且那些满足接口契约的dll被加载,每次控件被丢弃时,接口的实际实例被丢弃,然后通过加载的按钮事件将其移交给dll。 TabPage并在插件中处理。 我希望这一切都有道理。

PHPexception处理vs C#

这是一个非常基本的问题(我希望)。 我所做的大多数exception处理都是使用c#。 在c#中,try catch块中出错的任何代码都由catch代码处理。 例如 try { int divByZero=45/0; } catch(Exception ex) { errorCode.text=ex.message(); } 该错误将显示在errorCode.text中。 如果我尝试在php中运行相同的代码但是: try{ $divByZero=45/0; } catch(Exception ex) { echo ex->getMessage(); } 捕获代码未运行。 根据我的理解,php需要一个抛出。 这不是打败错误检查的全部目的吗? 这不会减少尝试捕获到if then语句吗? if(除以零)抛出错误请告诉我,我不必预测try中的每个可能的错误。 如果我这样做,那么无论如何都要让php的error handling更像c#吗?

如何使用Moq模拟SoapException以进行unit testingerror handling

我inheritance了一个调用SOAP Web服务的小型控制台应用程序。 这是一个嵌套的try-catches的悲剧性混乱,它以各种方式记录exception,并且我想围绕抛出SoapException时它的行为进行一些测试。 问题:当我无法模拟接口并且无法使属性或方法“虚拟”时,如何使用Moq模拟像SoapException这样的类? 再解释一下: 要测试此error handling,我需要控制SoapException对象的Actor属性,以及Detail属性以validationerror handling。 我的unit testing代码的片段: [TestMethod] public void MyTestMethod() { Mock soapMock = new Mock(MockBehavior.Strict); soapMock.SetupGet(ex => ex.Actor).Returns(“Test Actor”); 由于我正在模拟一个具体的类,并且Actor属性未标记为“Virtual”,因此在测试运行期间执行SetupGet(…)行时Moq会抛出此exception: System.NotSupportedException:非虚拟(在VB中可覆盖)成员上的无效设置:ex => ex.Actor 经过一些阅读,理想的答案是模拟一个界面,在这种情况下我无法做到。 由于这是.NET框架中的一个类,我也无法将Actor属性神奇地标记为虚拟。 我如何模拟SoapException,或者我可以通过不同的方式validationerror handling? 顺便说一下,我首先想要创建一个实际的SoapException,其中包含一些XML节点,但我很快就陷入了以编程方式创建XML文档树的漏洞。 我可以做到,但它需要更多的测试设置代码,如果可能的话,我想避免使用。

如何检查异步Web服务调用中的错误

我正在开发ASP.Net asmx Web服务。 在客户端,如果对服务器的请求返回Http错误代码,如http 500,我怎么能从Web服务客户端知道(我使用添加Web引用自动生成的客户端代理)? 乔治,提前谢谢

Web请求超时处理?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = 20000; using (WebResponse response = request.GetResponse()) using (var stream = response.GetResponseStream()) using (var reader = new StreamReader(stream)) { var result = reader.ReadToEnd(); // Do something with result } 在上面的例子中我定义了一个超时,如果碰巧超时,我怎么知道,结果是空的? 我收到任何响应类型吗? 我怎样才能确保我超时?

C#错误代码与exception

我有一个控制电路,我通过串口进行通信。 如果响应命令与某种格式不匹配,我认为它是一个错误,并想知道我是否应该返回错误代码或抛出exception? 例如: public double GetSensorValue(int sensorNumber) { … string circuitCommand = “GSV,01,” + sensorNumber.ToString(); // Get measurement command for specified sensor. string responseCommand; string expectedResponseCommand = “GSV,01,1,OK”; string errorResponseCommand = “ER,GSV,01,1,62”; responseCommand = SendReceive(circuitCommand); // Send command to circuit and get response. if(responseCommand != expectedResponseCommand) // Some type of error… { if(responseCommand == errorResponseCommand) […]

ServiceStack:如何处理错误?

到目前为止,我正在使用ServiceStack取得了很好的效果,除了处理错误似乎很棘手。 如果在序列化消息期间出现问题(因为我忘了在消息中添加默认构造函数),所有客户端都会返回一条消息,表明服务器有内部错误,状态代码为500.添加一个监听器Global.asax中的HttpApplication.Error事件不起作用,因为它永远不会被击中。 Application_Error也没有。 这不仅对最终用户场景不够,它使得调试这些错误非常麻烦,因为找出错误的唯一方法是快速监视中的这种丑陋表达: Encoding.Default.GetString( ((System.IO.MemoryStream)((SyncMemoryStream)((System.Net.HttpWebResponse)(((WebException)ex).Response)).ResponseStream))._buffer) 我想要的是捕获服务器端的任何和所有错误(无论是ServiceStack序列化,还是我的服务上的错误),并将所需信息添加到我所有消息类型都有的Errors集合中。