小编jar*_*ryd的帖子

异常处理ASP.NET MVC Web API

我将开始,是的,我们已经创建并使用从ExceptionFilterAttribute继承的异常过滤器.它在我们的身份过滤器之后立即在应用程序启动时的配置中注册,并且如果在我们的API内部发生错误,则可以按预期工作.

话虽如此,我正在寻找一种方法来处理在到达API之前发生的错误.

推理:我们永远不想返回YSOD和/或IIS HTML错误.我们总是希望点击自定义异常过滤器/处理程序,以便我们可以正确处理日志记录并向用户返回JSON响应.

截至目前,使用Fiddler发出请求,我可以附加到w3wp.exe进程并看到请求命中了global.asax中的Application_BeginRequest方法.之后,它只返回500响应.它永远不会在代码中出现异常或在此之后触及我的任何断点.它似乎返回IIS错误.我们从不希望这种情况发生.我们需要能够捕获所有这些"低级"异常,记录它们,并向用户返回有意义的内容.

有什么我们可以做的事情来处理错误,似乎是什么似乎击中了ASP.NET MVC Web API代码?

.net asp.net-mvc iis-7 asp.net-web-api

11
推荐指数
1
解决办法
5686
查看次数

WCF REST未异步处理

我们目前正在IIS中为我们的站点实现一个新的WCF REST服务,在许多页面上,我们可能会异步使用JQuery进行少量的AJAX调用.问题是好像WCF(在服务器端)同步执行.

在页面加载时,我们对3种不同的方法进行3次单独调用.使用日志记录,我可以看到它们都在大约5ms之内点击了global.asax文件.从那里,日志记录显示按退出global.asax的顺序执行的所有操作(不一定是我们通过javascript从页面调用的顺序).我希望每次调用都能收到自己的线程并单独返回.即使在使用调试器附加时,我也可以看到它在执行当前方法之前不会执行下一个方法.

以下是我为实现使用异步模型而实现的三种方法的操作合同.

    [OperationContract(AsyncPattern = true)]
    [WebInvoke(
        Method = "POST"
         , UriTemplate = "/ListUserPreferences"
        , BodyStyle = WebMessageBodyStyle.Wrapped
        , ResponseFormat = WebMessageFormat.Json
        , RequestFormat = WebMessageFormat.Json
    )]
    IAsyncResult BeginListUserPreferences(AsyncCallback callback, object state);
    Result<List<Data.EnumerationItem<UserPreferenceType>>> EndListUserPreferences(IAsyncResult asyncResult);

    [OperationContract(Name = "GetUserSecure", AsyncPattern = true)]
    [WebInvoke(
        Method = "POST"
         , UriTemplate = "/GetUser"
        , BodyStyle = WebMessageBodyStyle.Wrapped
        , ResponseFormat = WebMessageFormat.Json
        , RequestFormat = WebMessageFormat.Json
    )]
    IAsyncResult BeginGetUser(AsyncCallback callback, object state);
    Result<Data.User> EndGetUser(IAsyncResult asyncResult);

    [OperationContract(AsyncPattern = true)]
    [WebInvoke(
        Method = "POST"
         , UriTemplate …
Run Code Online (Sandbox Code Playgroud)

.net c# rest wcf asynchronous

7
推荐指数
1
解决办法
6650
查看次数

使用SqlCommand返回值

我正在尝试获取结果集并在SQL 2008服务器上返回存储过程的值.当我在sql management studio中运行proc时,我得到结果集和返回值.

但是,当我尝试在C#4.0中获取值时,参数的值为null.这是我的C#代码:

using (ConnectionManager<SqlConnection> cn = ConnectionManager<SqlConnection>.GetManager(CultureInfo.CurrentCulture.Name))
{
    using (SqlCommand cm = cn.Connection.CreateCommand())
    {
        cm.CommandText = "Name of proc here";
        cm.CommandType = CommandType.StoredProcedure;

        cm.Parameters.AddWithValue("@ApplicationId", ApplicationId);                                        
        cm.Parameters.AddWithValue("@Index", Index);

        if (PageSize > 0)
            cm.Parameters.AddWithValue("@PageSize", PageSize);

        cm.Parameters.Add("@ReturnValue", SqlDbType.Int);
        cm.Parameters["@ReturnValue"].Direction = ParameterDirection.ReturnValue;

        using (IDataReader dr = cm.ExecuteReader())
        {
            SafeDataReader sdr = new SafeDataReader(dr);
            while (sdr.Read())
            {
                UserApplicationEntity uae = new UserApplicationEntity();
                uae.UserId = sdr.GetGuid("UserId");
                uae.ExternalId = sdr.GetString("ExternalId");
                Result.Value.Collection.Add(uae);
            }

            Result.Value.TotalResults = (int)cm.Parameters["@ReturnValue"].Value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我调用Result.Value.TotalResults =(int)cm.Parameters ["@ ReturnValue"]的最后一行.值; 是值为null的位置.我发现的每个教程或帖子,我似乎正在做正确的事情.在这一点上,我想我只是缺少一些小东西,需要另一组眼睛.我也试过在所有其他人之前设置返回参数作为我在MS网站上发现的一个帖子说我需要,但无论它在哪里,它都返回null.

.net c# csla sql-server-2008

6
推荐指数
1
解决办法
8140
查看次数

StructureMap注册表配置中的类的调用方法

我不禁想到,比我的StructureMap Registry中的当前代码有更好的方法。

  For<ISchedulerFactory>().Use(() => new StdSchedulerFactory());
  For<IScheduler>().Use(() => new StdSchedulerFactory().GetScheduler());
Run Code Online (Sandbox Code Playgroud)

有没有办法让它使用以前的注册类型并从中调用方法? (GetScheduler()在ISchedulerFactory接口上)

c# structuremap dependency-injection

3
推荐指数
1
解决办法
1283
查看次数