小编Luk*_*keH的帖子

ASP.NET Web API:返回401 /未授权响应的正确方法

我有一个MVC webapi站点,它使用OAuth /令牌身份验证来验证请求.所有相关的控制器都具有正确的属性,并且身份验证正常.

问题是并非所有请求都可以在属性范围内被授权 - 某些授权检查必须在由控制器方法调用的代码中执行 - 在这种情况下返回401未授权响应的正确方法是什么?

我试过了throw new HttpException(401, "Unauthorized access");,但是当我这样做时,响应状态代码是500,我也得到了堆栈跟踪.即使在我们的日志记录DelegatingHandler中,我们也可以看到响应是500,而不是401.

c# asp.net-mvc authorization

85
推荐指数
6
解决办法
9万
查看次数

在针对SQL Server 2008运行时,SqlDataReader.HasRows中是否存在错误?

看看这两个查询:

-- #1
SELECT * FROM my_table
WHERE CONTAINS(my_column, 'monkey')

-- #2
SELECT * FROM my_table
WHERE CONTAINS(my_column, 'a OR monkey')  -- "a" is a noise word
Run Code Online (Sandbox Code Playgroud)

当我在Management Studio中运行时,查询#1返回20行.
查询#2返回相同的20行,但我也在消息选项卡中看到以下内容:

信息性:全文搜索条件包含噪声词.

到目前为止,这么无聊 - 正是我期望发生的事情.

现在,看一下这个C#片段:

using (SqlConnection conn = new SqlConnection(...))
{
    SqlCommand cmd = conn.CreateCommand();
    // setup the command object...

    conn.Open();
    using (SqlDataReader dr = cmd.ExecuteReader())
    {
        if (dr.HasRows)
        {
            // get column ordinals etc...

            while (dr.Read())
            {
                // do something useful...
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我针对查询#1运行此代码时,所有内容都按预期运行 - 为20行中的每一行命中"执行一些有用的"部分. …

.net sql sql-server ado.net sql-server-2008

20
推荐指数
2
解决办法
7648
查看次数

两个或多个线程可以在同一个List <t>上迭代而没有任何问题吗?

System.Collections.Generic.List<T>这里谈论.

用下面的例子可以同时执行Method1和Method2,在不同的线程上没有任何问题?

谢谢

class Test
{
    private readonly List<MyData> _data;

    public Test()
    {
        _data = LoadData();
    }

    private List<MyData> LoadData()
    {
        //Get data from dv.
    }

    public void Method1()
    {
        foreach (var list in _data)
        {
            //do something
        }
    }

    public void Method2()
    {
        foreach (var list in _data)
        {
            //do something
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# multithreading

12
推荐指数
1
解决办法
1968
查看次数

在C#中序列化和反序列化V.Large字典

我们有一个v.large Dictionary<long,uint>(几百万条目)作为高性能C#应用程序的一部分.当应用程序关闭时,我们使用BinaryFormatter和将字典序列化为磁盘MemoryStream.ToArray().序列化在大约30秒内返回并生成大小约为200MB的文件.然后,当我们尝试使用以下代码反序列化字典时:

BinaryFormatter bin = new BinaryFormatter();
Stream stream = File.Open("filePathName", FileMode.Open);
Dictionary<long, uint> allPreviousResults =
    (Dictionary<long, uint>)bin.Deserialize(stream);
stream.Close();
Run Code Online (Sandbox Code Playgroud)

返回需要大约15分钟.我们已经尝试了替代方案,慢速部分肯定是bin.Derserialize(stream),即在1秒内从硬盘驱动器(高性能SSD)读取字节.

有人可以指出我们做错了什么,因为我们希望加载时间与保存时间相同.

此致,马克

c# dictionary

11
推荐指数
2
解决办法
1771
查看次数

C#:传递继承

继承是C#中的传递关系吗?

我问,因为我不明白为什么IList<T>器具ICollection<T>IEnumerable<T>作为ICollection<T>已经实现IEnumerable<T>

谢谢你为我澄清这一点.

c# multiple-inheritance

11
推荐指数
2
解决办法
1008
查看次数

Dictionary <TKey,TSource>的神秘行为

我正在开发一个基于Asp.net MVC 3.0并使用Mono-2.10.8(Windows 7)的庞大系统.

一切都很好,直到几天前.

在我的API中,我有几个使用字典的实用程序类.例如,像这样:

public static class KeyUtility  
{
  static KeyUtility() {
    Alphabet = new[] {
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
      'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 
      'T', 'U', 'V', 'X', 'Y', 'Z', '0', '1', 
      '2', '3', '4', '5', '6', '7', '8', '9'
    };

    ReverseAlphabet = Alphabet
      .Select((c, i) => new { Char = c, Value = i })
      .ToDictionary(k => k.Char, v => (byte) v.Value);
  }

  internal static char[] Alphabet;      
  private static …
Run Code Online (Sandbox Code Playgroud)

c# mono dictionary exception thread-safety

8
推荐指数
1
解决办法
1183
查看次数

DataTable to List <object>

如何获取DataTable并将其转换为List?

我在C#和VB.NET中都包含了一些代码,这两个问题都是我们创建一个新对象来返回数据,这是非常昂贵的.我需要返回对象的引用.

DataSetNoteProcsTableAdapters.up_GetNoteRow对象确实实现了INote接口.

我正在使用ADO.NET和.NET 3.5

c#代码

public static IList<INote> GetNotes() 
{ 
    DataSetNoteProcsTableAdapters.up_GetNoteTableAdapter adapter =
        new DataSetNoteProcsTableAdapters.up_GetNoteTableAdapter(); 
    DataSetNoteProcs.up_GetNoteDataTable table =
        new DataSetNoteProcs.up_GetNoteDataTable(); 

    IList<INote> notes = new List<INote>(); 

    adapter.Connection = DataAccess.ConnectionSettings.Connection; 
    adapter.Fill(table); 

    foreach (DataSetNoteProcs.up_GetNoteRow t in table) { 
        notes.Add((INote)t); 
    } 

    return notes;
} 
Run Code Online (Sandbox Code Playgroud)

VB.NET代码

Public Shared Function GetNotes() As IList(Of INote)
    Dim adapter As New DataSetNoteProcsTableAdapters.up_GetNoteTableAdapter
    Dim table As New DataSetNoteProcs.up_GetNoteDataTable

    Dim notes As IList(Of INote) = New List(Of INote)

    adapter.Connection = DataAccess.ConnectionSettings.Connection
    adapter.Fill(table)

    For Each t As DataSetNoteProcs.up_GetNoteRow In table
        notes.Add(CType(t, …
Run Code Online (Sandbox Code Playgroud)

c# dataset

7
推荐指数
2
解决办法
7万
查看次数

从类型名称的字符串表示形式转换为类型

sTypeName = ... //do some string stuff here to get the name of the type

/*
The Assembly.CreateInstance function returns a type
of System.object. I want to type cast it to 
the type whose name is sTypeName.

assembly.CreateInstance(sTypeName)

So, in effect I want to do something like:

*/

assembly.CreateInstance(sTypeName) as Type.GetType(sTypeName);
Run Code Online (Sandbox Code Playgroud)

我怎么做?并且,假设这是C#2.0,我在赋值表达式的左侧做什么.我没有var关键字.

.net c# reflection system.reflection

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

Dictionary.Cld的高运行时添加了大量的项目

我有一个C#-Application,它将来自TextFile的数据存储在Dictionary-Object中.要存储的数据量可能相当大,因此插入条目需要花费大量时间.由于内部数组的大小调整存储了Dictionary的数据,因此在Dictionary中有许多项目会变得更糟.因此,我使用将要添加的项目数量初始化词典,但这对速度没有影响.

这是我的功能:

private Dictionary<IdPair, Edge> AddEdgesToExistingNodes(HashSet<NodeConnection> connections)
{
  Dictionary<IdPair, Edge> resultSet = new Dictionary<IdPair, Edge>(connections.Count);

  foreach (NodeConnection con in connections)
  {
    ...
    resultSet.Add(nodeIdPair, newEdge);
  }

  return resultSet;
}
Run Code Online (Sandbox Code Playgroud)

在我的测试中,我插入~300k项目.我用ANTS Performance Profiler检查了运行时间,发现当我用所需的大小初始化Dictionary时,resultSet.Add(...)的平均时间不会改变.它与我用新的Dictionary()初始化Dictionary时相同; (每次添加平均约0.256毫秒).这肯定是由字典中的数据量引起的(尽管我用所需的大小初始化它).对于前20k项,每个项的Add的平均时间为0.03 ms.

任何想法,如何使添加操作更快?

先谢谢你,弗兰克

这是我的IdPair-Struct:

public struct IdPair
{
  public int id1;
  public int id2;

  public IdPair(int oneId, int anotherId)
  {
    if (oneId > anotherId)
    {
      id1 = anotherId;
      id2 = oneId;
    }
    else if (anotherId > oneId)
    {
      id1 = oneId;
      id2 = anotherId;
    }
    else
      throw new …
Run Code Online (Sandbox Code Playgroud)

c# dictionary

5
推荐指数
2
解决办法
1495
查看次数

从ILGenerator中检索代码

我已经编写了一些函数来使用ILGenerator创建一个exe文件.我想要的是向用户展示使用ILDasm或Reflector等外部工具生成的IL语言.

在我的程序执行期间,我已将每个OpCode添加到ILGenerator,因此我可以使用带有OpCode表示的字符串将每个OpCode保存在列表中,但我希望直接获取IL代码.可以吗?

重要提示:我使用的是Mono 2.6.

.net c# mono command-line-interface ilgenerator

5
推荐指数
2
解决办法
1051
查看次数