我试图生成6个字符或更少的随机base32数字.这应该给出大约10亿种不同的组合.
我创建了一个程序来生成这些"随机"数字.但是,它似乎每40,000代平均产生一次重复.
当有超过十亿种不同的组合时,为什么这些"随机"数字经常重复?
这是我的代码:
static void Main(string[] args)
{
int seed = Environment.TickCount;
Random r = new Random(seed);
Dictionary<int, int> resultDictionary = new Dictionary<int, int>();
for (int x = 1; x <= 1000; x++)
{
Dictionary<string, int> dictionary = new Dictionary<string, int>();
try
{
while (true)
{
int rand = r.Next(0, 1073741823);
CrockfordBase32Encoding encoding = new CrockfordBase32Encoding();
string encodedRand = encoding.Encode((ulong)rand, false);
dictionary.Add(encodedRand, rand);
}
}
catch (Exception)
{
}
Console.WriteLine(string.Format("{0} - {1}", x, dictionary.Count));
resultDictionary.Add(x, dictionary.Count);
x++;
}
Console.WriteLine(); …
Run Code Online (Sandbox Code Playgroud) 我是实体框架的新手,所以请耐心等待.
我有一个程序,我想从表中选择多个记录并将其存储在队列中:
private Queue<RecordsToProcess> getRecordsToProcess()
{
Queue<RecordsToProcess> results = new Queue<RecordsToProcess>();
using (MyEntity context = new MyEntity())
{
var query = from v in context.RecordsToProcess
where v.Processed == false
select v;
foreach (RecordsToProcess record in query)
{
results.Enqueue(record);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我启动了多个工作线程.每个工作线程获取队列中的一个项目,对其进行处理,然后将其保存到数据库中.
private void processWorkerThread(object stateInfo)
{
while (workQueue.Count > 0)
{
RecordToProcess record = new RecordToProcess;
lock(workQueue)
{
if (workQueue.Count > 0)
RecordToProcess = workQueue.Dequeue();
else
break;
}
//Do the record processing here
//How do I save that record …
Run Code Online (Sandbox Code Playgroud) 我创建了一个ASP.Net vNext Web API.
我已经成功地使用了一个简单的接口,例如:
services.AddScoped<ILinearRegressionCalculator, LinearRegressionCalculator>();
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何使用通用接口进行依赖性约定.
如何为此接口设置依赖注入:
public interface IMongoConnectionHandler<T> where T : IMongoEntity
Run Code Online (Sandbox Code Playgroud) 我开发了我的第一个Web API,并尝试将其部署到我的QA服务器.我在IIS中创建了一个名为qa.mysite.com的网站.我将Web API部署到此文件夹,并尝试运行它.
当我在本地调试网站并转到
http://localhost:3735/api/Bid_Received
它工作正常.但是,当我去
http://qa.mysite.com/api/Bid_Received
我收到404错误.
我究竟做错了什么?我没有上传正确的文件吗?
当我查看我部署了我的Web API的文件夹时,我看到以下项目:
文件夹:bin,内容,图像,脚本,视图文件:favicon.ico,Global.asax,packages.config,Web.config
我只是想绕过这个概念.我编写了几个不同的Web API,但它们一直被网站使用并通过JSON进行交互.我有一个关于如何在Windows服务使用Web API时构建实现的问题.
在这种情况下,已经有一个现有的数据库,所以我想使用Entity Framework的Database First方法.
我为模型创建了一个类库项目,并使用Entity Framework查看现有数据库并生成所有必需的类.
然后,我创建一个Web API项目,并将包含所有模型的类库添加到其中.到目前为止,我很好.
我的问题是,当我构建将与Web API交互的Windows服务时,如何从类库模型项目中访问类?我知道我可以将该项目添加到我的Windows服务中,但这似乎不是正确的方法,因为这几乎会绕过Web API.
我想我的问题是,如果我想从我的Windows服务创建并将一个Employee对象传递给我的Web API(因此它可以将其插入到数据库中),Windows服务如何获取Employee对象而不将类库添加到Windows服务项目?
我有一个外部公开的MVC项目.我有一个内部Web API项目.
由于我无法控制的原因,我无法直接公开Web API项目,也无法将Web API控制器添加到我的MVC项目中.
我需要创建一个MVC控制器,它将充当Web API控制器的代理.我需要MVC Controller的响应看起来好像是直接调用了Web API.
完成此任务的最佳方法是什么?
有没有比我到目前为止更好的方法?
如何修复我得到的错误?
这是我到目前为止:
MyMVCController
[HttpGet]
public HttpResponseMessage GetData(HttpRequestMessage request)
{
...
var response = proxy.GetData();
return request.CreateResponse();
}
Run Code Online (Sandbox Code Playgroud)
MyProxyClass
public HttpResponseMessage GetData()
{
...
return HttpRequest(new HttpRequestMessage(HttpMethod.Get, uri));
}
private HttpResponseMessage HttpRequest(HttpRequestMessage message)
{
HttpResponseMessage response;
...
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromSeconds(120);
response = client.SendAsync(message).Result;
}
return response;
}
Run Code Online (Sandbox Code Playgroud)
在MVC控制器中,我在request.CreateResponse()行上收到InvalidOperationException.错误说:
请求没有关联的配置对象,或者提供的配置为null.
任何帮助将不胜感激.我搜索过谷歌和StackOverflow,但我找不到在MVC和Web API之间创建这个代理的好解决方案.
谢谢!
我正在使用 ASP.Net Web API 2 构建 REST API。我有一个 GET 端点,它接受查询字符串中的复杂对象。
public async Task<IHttpActionResult> GetAsync([FromUri]SpendingAccountSearchParams spendingAccountSearchParams) {...}
Run Code Online (Sandbox Code Playgroud)
搜索对象有很多属性,这些属性要么可以为空,要么在对象的构造函数中填充默认值。例如:
public SpendingAccountSearchParams()
{
AsOfDate = DateTime.Now;
Skip = 0;
Top = 20;
}
Run Code Online (Sandbox Code Playgroud)
如果我在查询字符串中传递至少 1 个参数,则我的 SpendingAccountSearchParams 对象将填充到控制器中,并且所有默认值都会正确设置。
但是,如果我不传递任何值,那么我的 SpendingAccountSearchParams 对象在构造函数中为 null。
如果没有传递查询字符串参数,如何自动强制所有属性获取默认值?
我想要与此等效的内容,但显然它无效并且不起作用:
public async Task<IHttpActionResult> GetAsync([FromUri]SpendingAccountSearchParams? spendingAccountSearchParams = new SpendingAccountSearchParams()) {...}
Run Code Online (Sandbox Code Playgroud)
我知道在控制器中我可以检查 null 并创建一个新对象(如果它不存在),但这只是一个端点的一个示例。最终,我将拥有许多具有不同搜索参数的端点,并且我希望所有端点都能自动发生。
任何帮助,将不胜感激。
谢谢!
我有一个 Angular 应用程序,它获取日期(仅日期而不是时间)并将它们发布到 Web API 2 REST 服务。当印度人由于时区问题使用该应用程序时,我们遇到了一个问题。
目前,Angular 应用程序正在将日期转换为 UTC 时区的 ISO8601 格式,并将它们发送到 Web API。在 Web API 端接收数据时,日期最终不正确。如果将 2016 年 6 月 21 日放入表单中,则日期最终会变为 2016 年 6 月 20 日。所需的解决方案是让表单中输入的实际日期值成为 API 接收到的日期值。
一种建议的解决方案是将日期视为字符串而不是日期,然后只传递日期部分。这对我来说似乎是一种黑客行为,似乎不是“正确”的做法。
处理这种情况的正确方法是什么?
鉴于该应用程序具有大量日期字段输入这一事实,是否有一种简单的方法可以跨所有日期输入值实施该解决方案?
有很多文章讨论如何使用ASP.NET Core的Structure Map,但谈论控制台应用程序或Windows服务并不是很多.ASP.Net Core中的默认行为是StructureMap为每个HTTPRequest创建一个嵌套容器,以便每个HTTP请求只对实例化一个具体类.
我正在使用PeterKottas.DotNetCore.WindowsService nuget包创建.Net核心Windows服务.我使用这篇文章设置了StructureMap:https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/
我的Windows服务是在Timer上设置的,每X秒执行一次动作.我希望这些操作中的每一个都使用类似于ASP.NET的嵌套容器.换句话说,我希望在轮询传递完成后处理为轮询传递#1创建的所有内容.当轮询#2开始时,我想要实例化所有对象的新实例.但是,在单个轮询传递的范围内,我只希望创建每个对象的一个实例.
这样做的正确方法是什么?
这是我的程序类
public class Program
{
public static ILoggerFactory LoggerFactory;
public static IConfigurationRoot Configuration;
static void Main(string[] args)
{
var applicationBaseDirectory = AppContext.BaseDirectory;
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (string.IsNullOrWhiteSpace(environment))
throw new ArgumentNullException("Environment not found in ASPNETCORE_ENVIRONMENT");
ConfigureApplication(applicationBaseDirectory, environment);
var serviceCollection = ConfigureServices();
var serviceProvider = ConfigureIoC(serviceCollection);
ConfigureLogging(serviceProvider);
var logger = LoggerFactory.CreateLogger<Program>();
logger.LogInformation("Starting Application");
ServiceRunner<IWindowsService>.Run(config =>
{
var applicationName = typeof(Program).Namespace;
config.SetName($"{applicationName}");
config.SetDisplayName($"{applicationName}");
config.SetDescription($"Service that matches Previous Buyers to Vehicles In …
Run Code Online (Sandbox Code Playgroud) c# structuremap windows-services dependency-injection .net-core
我正在使用EF Core 2.0。我有一个包含4列的表格,其中PK由所有4列组成。列之一(IsDefault)是数据库中的位字段。如果插入IsDefault设置为true的记录,则一切正常。如果插入IsDefault设置为false的记录,则会出现以下异常:
System.NotSupportedException occurred
HResult=0x80131515
Message=The 'IsDefault' on entity type 'ChromeMileageRestrictionTest' does not have a value set and no value generator is available for properties of type 'bool'. Either set a value for the property before adding the entity or configure a value generator for properties of type 'bool'.
Source=<Cannot evaluate the exception source>
StackTrace:
at Microsoft.EntityFrameworkCore.ValueGeneration.ValueGeneratorSelector.Create(IProperty property, IEntityType entityType)
at Microsoft.EntityFrameworkCore.ValueGeneration.RelationalValueGeneratorSelector.Create(IProperty property, IEntityType entityType)
at Microsoft.EntityFrameworkCore.ValueGeneration.Internal.SqlServerValueGeneratorSelector.Create(IProperty property, IEntityType entityType)
at Microsoft.EntityFrameworkCore.ValueGeneration.ValueGeneratorSelector.<>c__DisplayClass6_0.<Select>b__0(IProperty p, IEntityType e)
at Microsoft.EntityFrameworkCore.ValueGeneration.ValueGeneratorCache.<>c.<GetOrAdd>b__3_0(CacheKey ck)
at …
Run Code Online (Sandbox Code Playgroud) c# sql-server entity-framework entity-framework-core .net-core
c# ×8
.net-core ×2
asp.net ×2
angularjs ×1
asp.net-core ×1
asp.net-mvc ×1
base32 ×1
date ×1
generics ×1
iis ×1
iso8601 ×1
proxy ×1
random ×1
sql-server ×1
structuremap ×1