小编Sun*_*nil的帖子

C#中类库的全局错误处理程序

有没有一种方法可以捕获和处理在类库的任何方法中引发的所有异常的异常?

我可以在每个方法中使用try catch构造,如下面的示例代码所示,但是我正在寻找类库的全局错误处理程序。该库可由ASP.Net或Winforms应用程序或其他类库使用。

这样做的好处是开发更容易,并且不需要在每种方法中重复执行相同的操作。

public void RegisterEmployee(int employeeId)
{
   try
   {
     ....
   }
   catch(Exception ex)
   {
     ABC.Logger.Log(ex);
   throw;
   }
}  
Run Code Online (Sandbox Code Playgroud)

c# error-handling class-library

5
推荐指数
1
解决办法
4665
查看次数

wsHttpsBinding与wsHttpBinding

在为WCF项目编写代码时,我经常遇到wsHttpBinding。但是,我从未使用过wsHttp Binding

wsHttp 绑定是否甚至存在于WCF或其自定义绑定中?如果存在,那么为什么开发人员会使用wsHttp s Binding而不是wsHttpBinding

wcf wcf-wshttpbinding

5
推荐指数
1
解决办法
2608
查看次数

jQuery中的多个条件选择器

我有以下代码,我想选择type = text和id为'announcement_date'的元素.我将如何在jQuery中执行此操作?请注意,这两个元素具有相同的ID.这是我正在使用的传统经典ASP页面.

<input type="hidden" name="announcement_date" id="announcement_date" value="1111"></input>
<input type="text" name="announcement_date" id="announcement_date" value="323"></input>
<input type="text" name="eff_date" id="eff_date" value="123"></input>
<input type="text" name="end_date" id="end_date" value="123"></input>
Run Code Online (Sandbox Code Playgroud)

我尝试了下面的选择器,但它无法选择正确的元素.

$("#announcement_date type=text").addClass("error");
Run Code Online (Sandbox Code Playgroud)

jquery

4
推荐指数
1
解决办法
8266
查看次数

验证SQL Server 2008 R2中是否启用了SNAPSHOT隔离级别

我将在SSMS for SQL Server 2008 R2中执行哪些SQL以了解数据库中是否已打开SNAPSHOT隔离级别?

snapshot-isolation sql-server-2008-r2

4
推荐指数
1
解决办法
3494
查看次数

为什么.Net框架类中的静态方法始终是线程安全的?

我注意到.Net框架文档的大多数地方都有以下声明.

问题:这个秘诀是什么?我不认为静态类总是线程安全的.我的问题涉及.Net框架中可用的标准类,而不是开发人员创建的自定义类.

所有.Net Framework类文档中的线程安全说明

下面的静态类中的方法'GetString'是否是线程安全的,因为该方法是一个静态方法?

public static class MyClass
{
    static int x = 0;

    static MyClass()
    {
        x = 23;
    }

    public static string GetString()
    {
        x++;
        return x.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# static-methods thread-safety

4
推荐指数
1
解决办法
672
查看次数

如何知道 SQL Azure 中是否启用了“包含的数据库身份验证”?

是否有我可以运行的查询来了解SQL Azure 或 SQL Server 2014 中的用户定义数据库中是否打开了“包含的数据库身份验证”?

我尝试了以下查询,但它确实有一列用于“包含的数据库身份验证”?

select *  from   sys.databases 
where name='myDatbase';
Run Code Online (Sandbox Code Playgroud)

sql-server-2012 azure-sql-database

4
推荐指数
1
解决办法
3153
查看次数

在C#中对File类使用静态方法是否安全?

我在ASP.Net应用程序的代码隐藏中有以下代码,其中正在读取文件,然后写入文件.

var state= File.ReadAllText(Server.MapPath(string.Format("~/state/{0}", fileName)));
if(state.indexOf("1") == 0) 
{
  File.WriteAllText(Server.MapPath(string.Format("~/state/{0}", fileName)), newState);
}
Run Code Online (Sandbox Code Playgroud)

有时候,但并非总是如此,我得到以下异常.

例外

The process cannot access the file 'C:\inetpub\wwwroot\mywebsite1\state\20150905005929435_edf9267e-fad1-45a7-bfe2-0e6e643798b5' because it is being used by another process.

我猜测文件读取操作有时不会在写入操作发生之前关闭文件,或者可能是文件写入操作未在Web应用程序发出下一个请求之前关闭文件.但是,我找不到究竟是什么原因.

问题:如何避免发生此错误?使用File类是不安全的,而是使用FileStream对象的传统方法,我总是显式地处理FileStream对象?

更新1

我尝试了一种重试循环方法,但即使这似乎也没有解决问题,因为如果ASP.Net页面一次又一次非常快速地提交,我能够重现相同的错误.所以我回到了我的案例中找到一个万无一失的解决方案.

  string state = null;
  int i = 0;
  while (i < 20) {
    try {

        state = File.ReadAllText(Server.MapPath(string.Format("~/state/{0}", fileName)));

    } catch (Exception ex2) {
        //log exception
        Elmah.ErrorSignal.FromCurrentContext().Raise(ex2);
        //if even retry doesn't work then throw an exception
        if (i == 19) {
            throw; …
Run Code Online (Sandbox Code Playgroud)

c# asp.net file-io

4
推荐指数
1
解决办法
1139
查看次数

是否在ASP.Net Webforms中自动应用文化?(不是UICulture)

我有一个Webforms ASP.Net 4.5应用程序,该应用程序没有在web.config或代码中的其他位置指定文化或uiculture设置。我担心日期格式是否适合来自不同国家/地区的用户使用,即文化设置而不是UICulture设置。

问题:如果来自英国,德国和美国的用户使用此ASP.Net应用程序,则在asp:Label控件中显示日期值时会自动将其格式化,还是开发人员需要明确进行此格式化?

Label控件使用ASP.Net Webforms的数据绑定语法进行数据绑定,如下面的代码段所示。例如,如果对于美国用户而言,订购日期为10/4/2014,那么对于英国或德国用户而言,则应显示为4/10/2014。

HTML

<asp:Label id="lblOrderDate" runat="server" Text="<%# this.OrderDate %>"></asp:Label>
Run Code Online (Sandbox Code Playgroud)

代码隐藏

protected void Page_Load(object sender,   EventArgs e)
{ 
   string orderId = this.txtOrderId.Text;
   Order order = DAL.GetOrder( orderId );
   this.OrderDate = order.OrderDate;
   this.Page.DataBind();
}

public DateTime OrderDate { get;set; }
Run Code Online (Sandbox Code Playgroud)

更新1

我不确定是否需要在页面代码后面包含以下代码来设置区域性,还是由ASP.Net自动完成?我的猜测是ASP.Net会自动执行此操作,但不确定。

protected override void InitializeCulture()
{
    string language = "en-us";

    //Detect User's Language.
    if (Request.UserLanguages != null)
    {
        //Set the Language.
        language = Request.UserLanguages[0];
    }

    //Set the Culture.
    Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(language); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net webforms date-formatting

4
推荐指数
1
解决办法
1310
查看次数

如何从HTMLCollection中删除项目?

我有一些Javascript代码从HTMLCollection中删除项目,如下面的代码.我得到拼接时调用一个错误,指出:allInputs.splice is not a function.如果元素类型不是按钮类型,我需要从HTMLCollection中删除项目.

问题:如何从此类集合中删除项目?

我可以将未删除的项目传输到数组,然后我可以使用数组而不是原始的HTMLCollection,但不确定是否还有其他更短的方法.

JavaScript代码

    var allInputs = contentElement.getElementsByTagName('input');
    for (var i = (allInputs.length - 1) ; i >= 0; i--) {
        if (allInputs[i].type !== "button") {
            allInputs.splice(i, 1);//this is throwing an error since splice is not defined
        }
    }
Run Code Online (Sandbox Code Playgroud)

javascript htmlcollection

4
推荐指数
2
解决办法
6427
查看次数

在MVC 5期货中找不到Html.Serialize助手

我刚刚使用Package Manager在我的解决方案中安装了MVC 5期货,但我找不到这个帮助方法Html.Serialize,这是之前的MVC Futures版本.

我的问题:我需要包含哪些命名空间来开始在MVC 5期货中使用Html.Serialize帮助方法?

asp.net-mvc-futures asp.net-mvc-5

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