小编Wil*_*ill的帖子

为什么这个 System.IO.Pipelines 代码比基于 Stream 的代码慢得多?

我编写了一个小解析程序来比较.NET Core 中System.IO.Stream的新旧版本System.IO.Pipelines。我期望管道代码具有相同的速度或更快。但是,它慢了大约 40%。

程序很简单:它在一个 100Mb 的文本文件中搜索关键字,并返回关键字的行号。这是流版本:

public static async Task<int> GetLineNumberUsingStreamAsync(
    string file,
    string searchWord)
{
    using var fileStream = File.OpenRead(file);
    using var lines = new StreamReader(fileStream, bufferSize: 4096);

    int lineNumber = 1;
    // ReadLineAsync returns null on stream end, exiting the loop
    while (await lines.ReadLineAsync() is string line)
    {
        if (line.Contains(searchWord))
            return lineNumber;

        lineNumber++;
    }
    return -1;
}
Run Code Online (Sandbox Code Playgroud)

我希望上面的流代码比下面的管道代码慢,因为流代码将字节编码为 StreamReader 中的字符串。管道代码通过对字节进行操作来避免这种情况:

public static async Task<int> GetLineNumberUsingPipeAsync(string file, string searchWord)
{
    var searchBytes = Encoding.UTF8.GetBytes(searchWord); …
Run Code Online (Sandbox Code Playgroud)

c# performance .net-core system.io.pipelines

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

Code Contracts如何知道ToString覆盖不应该返回null?

我在C#中使用Microsoft的Code Contracts扩展.当我编写一个具有重写的ToString实现的类,它返回null时,它正确地识别问题:

CodeContracts字符串不能为空

我认为这是因为Microsoft在内部使用了Code Contracts,并且他们添加了一个Contract.Ensures调用Object.ToString.但是,当我查看Object.ToString源代码时,我没有看到任何合同(我确实看到了其他合同,但不是我正在寻找的合同).Code Contracts如何确定ToString不应该返回null?

c# code-contracts

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

使用OAuth访问Gmail原子Feed

我正在尝试使用OAuth从python应用程序中获取Gmail原子提要.我有一个可以下载Google阅读器Feed的工作应用程序,我认为应该只是更改范围和Feed网址.替换URL后,我仍然可以成功获取请求和访问令牌,但是当我尝试使用访问令牌获取源时,我收到"401 Unauthorized"错误.这是我的简单测试程序:

import urlparse
import oauth2 as oauth

scope = "https://mail.google.com/mail/feed/atom/"
sub_url = scope + "unread"

request_token_url = "https://www.google.com/accounts/OAuthGetRequestToken?scope=%s&xoauth_displayname=%s" % (scope, "Test Application")
authorize_url = 'https://www.google.com/accounts/OAuthAuthorizeToken'
access_token_url = 'https://www.google.com/accounts/OAuthGetAccessToken'

oauth_key = "anonymous"
oauth_secret = "anonymous"

consumer = oauth.Consumer(oauth_key, oauth_secret)
client = oauth.Client(consumer)

# Get a request token.
resp, content = client.request(request_token_url, "GET")
request_token = dict(urlparse.parse_qsl(content))

print "Request Token:"
print "    - oauth_token        = %s" % request_token['oauth_token']
print "    - oauth_token_secret = %s" % request_token['oauth_token_secret']
print

# …
Run Code Online (Sandbox Code Playgroud)

python gmail oauth gdata

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

在WCF中填充PrimaryIdentity

我正在使用简单的HTTP标头将令牌传递给WCF服务进行身份验证(WCF服务需要使用basicHTTPBinding,所以我很遗憾不能使用canned ws-security实现).我想填充PrimaryIdentity对象,以便WCF服务可以检查它以确定经过身份验证的用户.

问题是该OperationContext.Current.ServiceSecurityContext.PrimaryIdentity属性在我尝试填充时是只读的.我已经尝试使用SecurityTokenAuthenticators和IAuthorizationPolicy对象来设置身份信息,但该路由似乎需要使用消息级安全性(例如总是发送用户名和密码),这不是我想要的.

任何人都可以阐明我如何设置PrimaryIdentity字段?

.net security wcf

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