我想知道是否可以IDictionary
通过其键删除项目,同时获取已删除的实际值?
就像是:
Dictionary<string,string> myDic = new Dictionary<string,string>();
myDic["key1"] = "value1";
string removed;
if (nameValues.Remove("key1", out removed)) //No overload for this...
{
Console.WriteLine($"We have just remove {removed}");
}
Run Code Online (Sandbox Code Playgroud)
//We have just remove value1
Run Code Online (Sandbox Code Playgroud) 我正在对使用该IMemoryCache
接口的 ClientService 进行单元测试:
客户端服务.cs:
public string Foo()
{
//... code
_memoryCache.Set("MyKey", "SomeValue", new TimeSpan(0, 0, 60));
}
Run Code Online (Sandbox Code Playgroud)
当我尝试模拟IMemoryCache
的Set
扩展名时:
AutoMock mock = AutoMock.GetLoose();
var memoryCacheMock = _mock.Mock<IMemoryCache>();
string value = string.Empty;
// Attempt #1:
memoryCacheMock
.Setup(x => x.Set<string>(It.IsAny<object>(), It.IsAny<string>(), It.IsAny<TimeSpan>()))
.Returns("");
// Attempt #2:
memoryCacheMock
.Setup(x => x.Set(It.IsAny<object>(), It.IsAny<object>(), It.IsAny<TimeSpan>()))
.Returns(new object());
Run Code Online (Sandbox Code Playgroud)
它抛出以下异常:
System.NotSupportedException:不支持的表达式:x => x.Set(It.IsAny(), It.IsAny(), It.IsAny()) 扩展方法(此处:CacheExtensions.Set)不能用于设置/验证前
这是命名空间的缓存扩展 Microsoft.Extensions.Caching.Memory
public static class CacheExtensions
{
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 MVC 客户端设置 IdentityServer4。
一切正常,直到我想添加 ASP 标识。当我添加代码以使用 SQL 服务器和身份时,在成功登录后身份服务器不会将我重定向回我的客户端,而只是“刷新”页面。
IdentityServer 应用程序启动:
public class Startup
{
public IWebHostEnvironment Environment { get; }
public IConfiguration Configuration { get; }
public Startup(IWebHostEnvironment environment, IConfiguration configuration)
{
Environment = environment;
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
// uncomment, if you want to add an MVC-based UI
services.AddControllersWithViews();
services.AddDbContext<NebankaDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<NebankaUser, IdentityRole>()
.AddEntityFrameworkStores<NebankaDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication()
.AddGoogle("Google", options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.ClientId = "695872592852-tc9u84trcicjuhrrei1ikdmriarl3gmf.apps.googleusercontent.com";
options.ClientSecret = "sVDWez0nZHEzLiSyx165YToF";
});
var builder …
Run Code Online (Sandbox Code Playgroud) 我在尝试运行 .net 3.1 应用程序时遇到异常。
public class Program
{
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build(); --> Exception here!
Run Code Online (Sandbox Code Playgroud)
这是例外情况:
System.ArgumentNullException: 'Value cannot be null. (Parameter 'configure')'
Run Code Online (Sandbox Code Playgroud)
不确定是什么问题?
using Microsoft.AspNetCore.Authorization; (Version=5.0.0.0)
using Microsoft.AspNetCore.Mvc.Authorization;
services.AddControllers(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
Run Code Online (Sandbox Code Playgroud) 我是新来的node.js
,这个错误让我付出了很多努力,所以我分享这个。
我只尝试在我的 index.js 中声明express
和一些基本路由器:
const express = require('express');
const app = express();
app.get('/api/courses', (req, res)=>{
res.send(courses);
});
app.get('/api/courses:id', (req, res)=>{
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) res.send('The given id was not found...');
res.send(course);
});
app.get();
Run Code Online (Sandbox Code Playgroud)
错误详细信息:
\node_modules\path-to-regexp\index.js:63 path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/ ?')) ^
类型错误:无法在新层 (C:\Users...\node_modules) 的 pathtoRegexp (C:\Users...\node_modules\path-to-regexp\index.js:63:49) 处读取未定义的属性“长度” \express\lib\router\layer.js:45:17) 在 Function.route (C:\Users...\node_modules\express\lib\router\index.js:494:15) 在 Function.app.(匿名函数)[as get] (C:\Users...\node_modules\express\lib\application.js:481:30) …
我从Java背景来到C#世界.
我需要更新map/dictionary中的值并获取之前的值(如果没有则返回null).我会在Java中执行以下操作:
String oldValue = myMap.put(key, newValue);
someFunction(oldValue, newValue);
Run Code Online (Sandbox Code Playgroud)
在C#中我使用了Dictionary,但是我没有找到任何方法来获取更新前的值.到目前为止,我需要执行2次查找才能完成此任务,我认为在性能和代码行方面并不是最优的
string oldValue = null;
myDictionary.TryGetValue(key, oldValue);
myDictionary[key] = newValue;
SomeFunction(oldValue, newValue);
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法来更新价值并获得前一个?
我正在尝试使该方法返回 Tuple:Tuple<DateTime?, DateTime?>
。
如果代码能够生成两种 DateTime 类型,我将使用以下命令Tuple.Create
创建一个 return 语句:
public Tuple<DateTime?, DateTime?> GeTuple()
{
if (something)
{
return Tuple.Create(startDate, endDate);
}
else
{
return Tuple.Create(null, null); //--> This line produce the error.
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
无法从用法中推断出方法“Tuple.Create(T1, T2)”的类型参数。尝试显式指定类型参数。
我正在尝试使用 EF Core 运行 .NET Core Web 应用程序。为了测试存储库,我添加了一个MyDbContext
继承 EFDbContext
和 interface 的IMyDbContext
。
public interface IMyDbContext
{
DbSet<MyModel> Models { get; set; }
}
public class MyDbContext : DbContext, IMyDbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
public virtual DbSet<MyModel> Models { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
上下文接口被注入到我的通用存储库中:
public class GenericRepository<TEntity> : IGenericRepository<TEntity>
{
private readonly IMyDbContext _context = null;
public GenericRepository(IMyDbContext context)
{
this._context = context;
}
}
Run Code Online (Sandbox Code Playgroud)
当我在 startup.cs 上使用此代码(不带接口)时:
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(...));
Run Code Online (Sandbox Code Playgroud)
我收到以下运行时错误: …
我正在尝试从使用 Google reCaptcha v2 迁移到不可见的 reCaptcha。我使用 Parsley.js 进行表单验证,并使用 Malsup Ajax 表单插件提交表单。我当前的代码如下所示:
HTML:
<form action="send1.php" method="post" class="contact_form">
<label for="name_1">Name</label>
<input type="text" name="name_1" id="name_1" value="" required />
<div class="g-recaptcha" data-sitekey="XXX"></div>
<input type="submit" class="button" value="">
</form>
Run Code Online (Sandbox Code Playgroud)
JS:
$('.contact_form').parsley();
$('.contact_form').submit(function(){
if($('.contact_form').parsley().validate()){
$('.contact_form').ajaxSubmit();
}
return false;
});
Run Code Online (Sandbox Code Playgroud)
这成功地将 g-recaptcha-response 传递到 send1.php,其中 reCaptcha 被验证。
如何将 Invisible reCaptcha 与此脚本集成?
我尝试使用这个:
<div id='recaptcha' class="g-recaptcha"
data-sitekey="XXX"
data-callback="onSubmit"
data-size="invisible"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
但我不确定如何使用数据回调。如果我添加
grecaptcha.execute();
Run Code Online (Sandbox Code Playgroud)
在 ajaxSubmit() 之前,字段 g-recaptcha-response 被添加到 Ajax 调用中,但它的值为空...
有什么帮助吗?
考虑这个简单的示例代码:
var cts = new CancellationTokenSource();
var items = Enumerable.Range(1, 20);
var results = items.AsParallel().WithCancellation(cts.Token).Select(i =>
{
double result = Math.Log10(i);
return result;
});
try
{
foreach (var result in results)
{
if (result > 1)
cts.Cancel();
Console.WriteLine($"result = {result}");
}
}
catch (OperationCanceledException e)
{
if (cts.IsCancellationRequested)
Console.WriteLine($"Canceled");
}
Run Code Online (Sandbox Code Playgroud)
对于并行结果中的每个结果,它会打印结果,直到 result > 1
此代码输出类似于:
result = 0.9030899869919435
result = 0.8450980400142568
result = 0.7781512503836436
result = 0
result = 0.6020599913279624
result = 0.47712125471966244
result = 0.3010299956639812
result = …
Run Code Online (Sandbox Code Playgroud) c# ×8
.net-core ×2
ajax ×1
asp.net-core ×1
dbcontext ×1
dictionary ×1
express ×1
forms ×1
idictionary ×1
jquery ×1
linq ×1
mocking ×1
moq ×1
node.js ×1
openid ×1
parsley.js ×1
recaptcha ×1
tuples ×1