Tag: exception处理

我应该只捕获exception才能记录它们吗?

我是否应该捕获日志记录的exception? public foo(..) { 尝试 { … } catch(Exception ex){ Logger.Error(前); 扔; } } 如果我在每个层(DataAccess,Business和WebService)中都有这个,这意味着多次记录exception。 如果我的图层在单独的项目中并且只有公共接口中有try / catch,那么这样做是否有意义? 为什么? 为什么不? 我可以使用不同的方法吗?

在MVC中使用PartialView进行全局error handling

我在ASP.NET MVC中查看了一篇关于exception处理的完美文章,并希望在本文的方法6中实现一个方法,以便在错误和exception情况下为所有其他模式对话重用相同的错误页面。 另一方面,当我使用弹出窗口时,我需要在modal dialog中渲染PartialView而不是重定向页面。 是否有可能做到这一点? AJAX电话: $.ajax({ type: “POST”, url: ‘@Url.Action(“Delete”, “Person”)’, cache: false, dataType: “json”, data: formdata, success: function (response, textStatus, XMLHttpRequest) { if (response.success) { //display operation result } else { /* At this stage I need to render the Error view as partial without redirecting to Error page on error. The problem […]

n层应用程序中的exception处理?

在分层应用程序中处理exception的建议方法或最佳实践是什么? 你应该在哪里放置try/catch块? 你应该在哪里实施记录? 是否有建议的模式来管理n层应用程序中的exception? 考虑一个简单的例子。 假设您有一个调用业务层的UI,它调用数据层: //UI protected void ButtonClick_GetObject(object sender, EventArgs e) { try { MyObj obj = Business.GetObj(); } catch (Exception ex) { Logger.Log(ex); //should the logging happen here, or at source? MessageBox.Show(“An error occurred”); } } //Business public MyObj GetObj() { //is this try/catch block redundant? try { MyObj obj = DAL.GetObj(); […]

在ASP.NET Web Api中使用“ExceptionHandler”需要一个完整的示例来处理未处理的exception?

我查看了这个链接http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling 。 在这个链接中他们提到了这样的 class OopsExceptionHandler : ExceptionHandler { public override void HandleCore(ExceptionHandlerContext context) { context.Result = new TextPlainErrorResult { Request = context.ExceptionContext.Request, Content = “Oops! Sorry! Something went wrong.” + “Please contact support@contoso.com so we can try to fix it.” }; } private class TextPlainErrorResult : IHttpActionResult { public HttpRequestMessage Request { get; set; } […]

WCF奇怪的行为

我在使用Web服务时得到了这个: 合同’IServices’的操作’Login’指定要序列化的多个请求体参数,而不包含任何包装元素。 最多可以在没有包装元素的情况下序列化一个body参数。 删除额外的body参数或将WebGetAttribute / WebInvokeAttribute上的BodyStyle属性设置为Wrapped。 我使用界面看起来像: namespace DreamServices { // NOTE: You can use the “Rename” command on the “Refactor” menu to change the interface name “IService1” in both code and config file together. [ServiceContract] public interface IServices { [OperationContract] [WebInvoke(Method = “GET”, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = “LogIn/{username}/{password}”)] string Login(string […]

.NET中的SOAP请求

我们正在尝试使用SOAP请求使用Visual Studio C#与预订API进行交互。 我们已经与另一个API的Web服务接口而没有问题,但这个问题certificate是有问题的。 我们已将其添加为服务引用,并且Visual Studio已为其生成所有类和web.config条目,以便一切看起来都很好。 但是,当我们尝试向客户端SOAP服务发出简单请求时,我们会收到以下错误: rpc \ literal SOAP不支持Namespace =’com.hrs.soap.hrs’。 包装元素必须是不合格的。 代码是: PingRequest pingrequest = new PingRequest(); SoapServiceClient service = new SoapServiceClient(); service.ping(pingrequest); 这是Web.config文件中添加的内容: 根据文档,请求应如下所示: 完整堆栈跟踪如下: [InvalidOperationException: Namespace=’com.hrs.soap.hrs’ is not supported with rpc\literal SOAP. The wrapper element has to be unqualified.] System.Xml.Serialization.XmlReflectionImporter.CheckTopLevelAttributes(XmlAttributes a, String accessorName) +842469 System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String […]

接受的方法是防止“远程主机关闭连接”exception

我经常遇到以下exception,这是由用户启动下载引起的,因此失败(或被取消): 错误消息:远程主机关闭了连接。 错误代码是0x80072746。 堆栈跟踪:在System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte []状态,Byte []标头,Int32 keepConnected,Int32 totalBodySize,Int32 numBodyFragments,IntPtr [] bodyFragments,Int32 [] bodyFragmentLengths,Int32 doneWithSession,Int32 finalStatus,Boolean& System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean finalFlush)at System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal)atync) 我在互联网上搜索过, 发现了一篇有趣的文章 ,但似乎没有一个明确的答案,因为这是防止这种填充日志的最佳方法。 用户没有看到任何错误,并且在应用程序中没有实际问题,因为它仅在我的理解中发生(在我看来)在其无法控制的情况下(用户取消下载或丢失连接)但是必须有一种方法来防止这样的exception报道。 我不想这么说但是我很想检查这个exception并且空了阻止它的屁股 – 但这让我觉得自己像个肮脏的程序员。 那么 – 防止此exception填满我邮箱的可接受方法是什么?

exception处理中间件和页面

我是中间件概念的新手,我目前正在努力处理我的MVC Core项目中的exception。 我想要发生的是捕获,记录exception,然后将用户发送到带有消息的友好错误页面。 起初,我试图在中间件中管理所有这些,但意识到我可能没有正确地做到这一点。 因此,如果我想要这个流程发生,我应该使用我的exception日志记录中间件和app.UseExceptionHandler(“/ Error”),以便中间件重新抛出exception到页面? 如果是这样,我如何在错误页面中获取exception详细信息? 我想首先拦截的exception处理机制应该是Configure中的最后一个吗?这是正确的吗? 我发现的所有示例都严格处理HTTP状态代码错误,如404; 我正在寻找处理实际的exception(及其子类)。 这样,在我的View页面中,我可以在适用的情况下抛出自己的exception(例如,如果为必填字段提供了一个null。) Startup.cs片段: public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, LoanDbContext context) { loggerFactory.AddConsole(Configuration.GetSection(“Logging”)); loggerFactory.AddDebug(); app.UseStatusCodePages(); if (env.IsDevelopment() || env.IsEnvironment(“qa”)) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler(“/Error”); } app.UseMiddleware(loggerFactory); // … rest of Configure is irrelevant to this issue MyExceptionHandler.cs using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using […]

WCF中的远程主机强制关闭现有连接

我有一个名为’Template’的抽象类,定义如下: [DataContract] public abstract class Template { [DataMember] public virtual int? Id { get; set; } [DataMember] public virtual string Title { get; set; } [DataMember] public virtual byte[] TemplateDoc { get; set; } [DataMember] public virtual bool IsSystemTemplate { get; set; } } 两个派生类:UserTemplate和SystemTemplate实现上面的抽象类,定义如下: public class UserTemplate : Template { [DataMember] public virtual Int32? […]

AlreadyAllocated调用waveOutOpen错误

private void receiveAudio(object sender) { IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0); MemoryStream audioDataStream; BufferedWaveProvider bufferedWaveProvider; //RawSourceWaveStream receivedRawAudioData; byte[] receivedAudioData; waveOutStream = new WaveOut(); while (IsAudioTransferActive) { receivedAudioData = audioUDPClient.Receive(ref senderEP); audioDataStream = new MemoryStream(receivedAudioData); //receivedRawAudioData = new RawSourceWaveStream(audioDataStream, audioFormat); bufferedWaveProvider = new BufferedWaveProvider(audioFormat); //How can I feed “audioDataStream ” to “bufferedWaveProvider ” waveOutStream.Init(bufferedWaveProvider); waveOutStream.Play(); } […]