我有一个简单的同步方法,看起来像这样:
public IEnumerable<Foo> MyMethod(Source src)
{
// returns a List of Oof objects from a web service
var oofs = src.LoadOofsAsync().Result;
foreach(var oof in oofs)
{
// transforms an Oof object to a Foo object
yield return Transform(oof);
}
}
Run Code Online (Sandbox Code Playgroud)
由于该方法是Web应用程序的一部分,因此尽可能有效地使用所有资源是很好的.因此,我想将方法更改为异步方法.最简单的选择是做这样的事情:
public async Task<IEnumerable<Foo>> MyMethodAsync(Source src)
{
var oofs = await src.LoadOofsAsync();
return oofs.Select(oof => Transform(oof));
}
Run Code Online (Sandbox Code Playgroud)
我不是专家要么async/ await或IEnumerable.但是,从我的理解,使用这种方法"杀死"的好处IEnumerable,因为等待任务,直到整个集合被加载,从而省略了IEnumerable集合的"懒惰" .
在其他StackOverflow帖子上,我已经阅读了几个使用Rx.NET(或System.Reactive)的建议.快速浏览我读过的文档,这IObservable<T>是他们的异步替代品IEnumerable<T>.但是,使用天真的方法并尝试键入以下内容不起作用:
public async …Run Code Online (Sandbox Code Playgroud) 最近我们将项目迁移.NET Core 2.0到了.NET Core 2.1.结果我们的Swagger文档站点停止了工作.我们仍然可以访问它.我们可以看到自定义标题和版本,但没有API文档,只是一条消息说No operations defined in spec!.
我尝试过.NET Core 2.0的旧解决方案,但它没有帮助.基于以下两篇文章1 2我尝试从控制器方法中删除Swagger属性并[ApiController]在控制器类之上添加一个属性,但这也没有帮助.任何人都可以帮忙解决这个问题吗?
的.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>Company.Administration.Api</RootNamespace>
<AssemblyName>Company.Administration.Api</AssemblyName>
<PackageId>Company.Administration.Api</PackageId>
<Authors></Authors>
<Company>Company, Inc</Company>
<Product>Foo</Product>
<ApplicationInsightsResourceId>/subscriptions/dfa7ef88-f5b4-45a8-9b6c-2fb145290eb4/resourcegroups/Foo/providers/microsoft.insights/components/foo</ApplicationInsightsResourceId>
<ApplicationInsightsAnnotationResourceId>/subscriptions/dfa7ef88-f5b4-45a8-9b6c-2fb145290eb4/resourceGroups/Foo/providers/microsoft.insights/components/foo</ApplicationInsightsAnnotationResourceId>
<UserSecretsId>bf821b77-3f23-47e8-834e-7f72e2ab00c5</UserSecretsId>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>bin\Debug\netcoreapp2.1\Administration.Api.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<!-- Try to set version using environment variables set by GitVersion. -->
<Version Condition=" '$(Version)' == '' And '$(GitVersion_AssemblySemVer)' != '' ">$(GitVersion_AssemblySemVer)</Version>
<InformationalVersion Condition=" '$(InformationalVersion)' == '' And '$(GitVersion_InformationalVersion)' != '' ">$(GitVersion_InformationalVersion)</InformationalVersion>
<!-- If …Run Code Online (Sandbox Code Playgroud) 标题是不言自明的。是否存在内置机制来支持Entity Framework Core中代码优先数据库中记录的创建和编辑时间戳记?
喜欢的东西:created_at,并:updated_at在Ruby on Rails的迁移。我找不到与此有关的任何文档。如果没有现成的机制,是否有最佳实践来实现这些列?
我正在尝试使用 VS Code 来调试 Ruby on Rails 应用程序。我已经在 VS Code 中安装了 Ruby 和 Rubocop 扩展。然后,我已经安装ruby-debug-ide -v 0.6.0和debase -v 0.2.1双方通过sudo gem install和rvmsudo gem install。但是,当尝试使用 Rails 服务器调试应用程序时,出现以下异常:
/usr/lib/ruby/2.3.0/rubygems/dependency.rb:319:in `to_specs'
:
Could not find 'ruby-debug-ide' (>= 0.a) among 48 total gem(s)
(
Gem::LoadError
)
Checked in 'GEM_PATH=/home/myname/.rvm/gems/ruby-2.3.1:/home/myname/.rvm/gems/ruby-2.3.1@global', execute `gem env` for more information
from /usr/lib/ruby/2.3.0/rubygems/dependency.rb:328:in `to_spec'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_gem.rb:65:in `gem'
from /usr/local/bin/rdebug-ide:22:in `<main>'
Run Code Online (Sandbox Code Playgroud)
但是,当我用 列出所有已安装的 gem 时rvm all do gem list,安装的 gem 都不存在:
*** …Run Code Online (Sandbox Code Playgroud) 将UTC日期时间转换为本地时间(CET)的正确方法是什么?我应该使用System.DateTime.ToLocalTime()还是TimeZoneInfo.ConvertTime()?有什么区别吗?还是它们只是内部互相调用的两个方法?
我正在为我现有的.NET Core项目尝试JetBrains Rider.在项目中,我有几个appsettings.json覆盖:
appsettings.Development.json - 用于发展appsettings.Test.json - 用于检测appsettings.Staging.json - 用于临时环境在Visual Studio中,覆盖工作完美,在调试模式下运行应用程序时,我总是得到我的开发覆盖.但是,在Rider中我得到的默认appsettings.json值不适合调试和开发.如何更改设置以从正确的覆盖中获取值?
在Java中,可以使用AspectJ在执行方法之前和之后使用方法注释添加行为。由于C#属性似乎非常相似,所以我想知道是否有可能实现类似的行为。我一直在寻找的几个教程和其他来源(1,2,3),但没有人帮助我。
我认为也许可以通过将代码插入Attribute构造函数并使其成为可处理的方式来模仿行为,如下所示:
[AttributeUsage(AttributeTargets.Method)]
public class MyWritingAttribute : Attribute, IDisposable
{
public MyWritingAttribute()
{
Console.WriteLine("Attribute created");
}
public void Dispose()
{
Console.WriteLine("Attribute disposed");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当使用这样的属性时,只有Hello world!在控制台中显示:
class Program
{
static void Main(string[] args)
{
SayHelloWorld();
Console.ReadLine();
}
[MyWriting]
private static void SayHelloWorld()
{
Console.WriteLine("Hello World!");
}
}
Run Code Online (Sandbox Code Playgroud)
我当时在想控制台可能无法在该属性中访问,但是即使将其替换为throw new Exception()表达式,也不会引发异常。StringLengthAttributeEF 如何运作,但我的属性甚至没有实例化?以及如何使属性在修饰方法之前和之后运行?
我有一个在Azure上运行的ASP.NET Core应用程序。该应用程序以明文和HTML格式发送各种电子邮件。我在文件夹中创建了一个子Resources文件夹,名为EmailTemplates,用于存储所有HTML和TXT模板。示例路径可能是:
\src\WebProject\Resources\EmailTemplates\Welcome.html
我通过EmailService.cs以下方式访问班级中的文件:
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "EmailTemplates", "Welcome.html");
var htmlTemplate = await File.ReadAllTextAsync(filePath);
Run Code Online (Sandbox Code Playgroud)
当在IIS和Kestrel上的本地环境上运行时,这非常有用。但是,当部署到Azure时,我收到一条IO错误消息:
System.IO.DirectoryNotFoundException
An unhandled exception has occurred: Could not find a part of the path 'D:\home\site\wwwroot\Resources\EmailTemplates\Welcome.html'.
Run Code Online (Sandbox Code Playgroud)
我在StackOverflow上发现了类似的问题,但答案已经接近6年了。我不确定,自从Azure在过去几年中发生重大变化以来,它是否仍然适用。我也在看官方文档(1) (2),但没有发现关于Azure的任何特别之处。
在ASP.NET Core和Azure中存储和检索电子邮件模板(和其他静态文件)的正确方法是什么?
在本教程之后,我将Swagger和Swashbuckle生成器添加到了我的站点。现在,在导航至时,https://localhost:port/swagger/我可以看到生成的API文档。请注意,我还没有创建任何SwaggerController类-所有这些都由NuGet包处理。
问题是,我的整个站点,甚至是API,都使用自定义LDAP进行了身份验证。我也想保护/swagger/页面。但是,我没有找到一种方法来做到这一点。关于StackOverflow的唯一相关问题描述了向INTO摇摇欲坠的请求中添加身份验证 -不对整个API文档页面进行身份验证。
有没有一种特定的方法来保护生成的/swagger/页面?或者,是否存在将身份验证验证器添加到ASP.NET Core 2.0 MVC路由的一般方法?
我们有一个带有Entity Framework Core的ASP.NET Core MVC应用程序.我们增加了一个自定义的Testing环境,现有的Development,Staging和Production环境.现在,我们希望每次应用程序在此新的测试环境中启动时,应用程序都会删除并重新创建(或至少截断)数据库.
是否有一种自动化的方式来实现这种行为?或者我是否必须在数据库上下文中手动调用RemoveAll每个DbSet?
我有一个以下形式的字符串:
'123string89continues...'
Run Code Online (Sandbox Code Playgroud)
更换或修剪前导数字最优雅的方法是什么?注意,可以有多于或少于3位数,但至少有一位数字始终存在.
'1string89continues...' # This can happen
'0123456789string89continues...' # This can happen
'string89continues...' # This cannot happen
Run Code Online (Sandbox Code Playgroud) c# ×8
.net-core ×4
asp.net-core ×3
ruby ×2
swagger ×2
appsettings ×1
asynchronous ×1
azure ×1
rider ×1
routing ×1