我有一个使用Entity Framework 6处理数据库的MVC站点,我一直在尝试更改它,以便所有内容都作为异步控制器运行,并且对数据库的调用作为异步对应项运行(例如ToListAsync())而不是ToList())
我遇到的问题是,简单地将我的查询更改为异步会导致它们非常慢.
以下代码从我的数据上下文中获取"Album"对象的集合,并转换为相当简单的数据库连接:
// Get the albums
var albums = await this.context.Albums
.Where(x => x.Artist.ID == artist.ID)
.ToListAsync();
Run Code Online (Sandbox Code Playgroud)
这是创建的SQL:
exec sp_executesql N'SELECT
[Extent1].[ID] AS [ID],
[Extent1].[URL] AS [URL],
[Extent1].[ASIN] AS [ASIN],
[Extent1].[Title] AS [Title],
[Extent1].[ReleaseDate] AS [ReleaseDate],
[Extent1].[AccurateDay] AS [AccurateDay],
[Extent1].[AccurateMonth] AS [AccurateMonth],
[Extent1].[Type] AS [Type],
[Extent1].[Tracks] AS [Tracks],
[Extent1].[MainCredits] AS [MainCredits],
[Extent1].[SupportingCredits] AS [SupportingCredits],
[Extent1].[Description] AS [Description],
[Extent1].[Image] AS [Image],
[Extent1].[HasImage] AS [HasImage],
[Extent1].[Created] AS [Created],
[Extent1].[Artist_ID] AS [Artist_ID]
FROM [dbo].[Albums] AS [Extent1]
WHERE [Extent1].[Artist_ID] = @p__linq__0',N'@p__linq__0 …Run Code Online (Sandbox Code Playgroud) 我已经包含了一个模型,并创建了一个视图文件以及一个控制器来指导所有这些文件
public class CreateNewUserModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required(ErrorMessage = "Email required")]
[EmailAddress(ErrorMessage = "Not a valid email")]
public string UserEmail { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword …Run Code Online (Sandbox Code Playgroud) 经过两个问题和很多混乱 - 我想知道我是否最终做对了.这是我的理解:
async/await仅用于一个目的 - 允许在已完成异步任务后执行代码.例如
async Task CallerMethod()
{
await AsyncMethod();
AnotherMethod();
}
Run Code Online (Sandbox Code Playgroud)
允许AnotherMethod异步后执行AsyncMethod的结束,而不是后立即AsyncMethod被启动.
async/await NEVER 使任何异步.它不会启动一个单独的线程(当然,除非等待的方法是这样做的),等等.
我的理解(最后)是否正确?
我有一个 Spring 启动应用程序,它使用 Feign 通过 Eureka 调用外部 Web 服务。我希望能够使用 Feign 接口的模拟实现来运行应用程序,这样我就可以在本地运行应用程序,而不必运行 Eureka 或外部 Web 服务。我曾想过定义一个允许我这样做的运行配置,但我正在努力让它工作。问题是,无论我尝试什么,Spring 的“魔法”都会为 Feign 接口定义一个 bean。
Feign 接口
@FeignClient(name = "http://foo-service")
public interface FooResource {
@RequestMapping(value = "/doSomething", method = GET)
String getResponse();
}
Run Code Online (Sandbox Code Playgroud)
服务
public class MyService {
private FooResource fooResource;
...
public void getFoo() {
String response = this.fooResource.getResponse();
...
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试添加一个配置类,如果 Spring 配置文件是“本地”,则该配置类有条件地注册了一个 bean,但是当我使用该 Spring 配置文件运行应用程序时,它从未被调用过:
@Configuration
public class AppConfig {
@Bean
@ConditionalOnProperty(prefix = "spring.profile", name = "active", havingValue="local")
public FooResource fooResource() { …Run Code Online (Sandbox Code Playgroud) spring-boot netflix-feign spring-cloud-feign spring-cloud-netflix
我正在尝试使用以下代码创建一个新的注册表项并收到此错误:
Cannot write to the registry key.
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
var rs = new RegistrySecurity();
string user = Environment.UserDomainName + "\\" + Environment.UserName;
rs.AddAccessRule(new RegistryAccessRule(user,
RegistryRights.WriteKey | RegistryRights.SetValue,
InheritanceFlags.None,
PropagationFlags.None,
AccessControlType.Allow));
RegistryKey key;
key = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", RegistryKeyPermissionCheck.ReadSubTree, rs);
key.SetValue("kashif", 1, RegistryValueKind.DWord);
key.Close();
Run Code Online (Sandbox Code Playgroud) 我试图了解.NET 4.5中的变化,主要是异步功能.为了理解它,我想我会创建一个小应用程序来存档我的大量照片集.我通过这样做来学习最好的应用程序有双重目的.
我已经阅读了很多关于使用异步的MSDN文章,但我认为我对它没有足够的了解(因为它不起作用).我的目的是将源文件夹中的每张照片根据其拍摄日期复制到目标文件夹(或者如果缺少所拍摄的元数据则创建).同时将其重命名为标准命名约定,并在图像框中存档时显示图像.我希望应用程序在工作期间保持响应,这是异步进入的地方.现在应用程序的目的并不重要,整个过程让我的头脑异步.
实际发生的是应用程序没有响应,按预期存档所有图像,但图像框仅显示最终图片.异步开始文件传输,然后继续下一个图像,开始传输然后继续等等,所以我最终得到数百个打开的文件流,而不是等待每个关闭.
任何我错误的地方的指针将不胜感激.我对使用任务的理解很简单,返回任务服务的目的是什么?
imgMain是XAML文件中的图像框.async/await在归档方法中,但显示所有可能相关的代码.
using System;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
namespace PhotoArchive
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string Source
{
get { return txtSource.Text; }
set { txtSource.Text = value; }
}
private string Destination
{
get { return txtDestination.Text; }
set { txtDestination.Text = value; }
}
public MainWindow()
{
InitializeComponent();
} …Run Code Online (Sandbox Code Playgroud) 我需要做什么? 我需要针对XSD文件验证XML文件(传递文件路径/位置)(传递文件路径/位置).我需要检查它是否良好没有非法字符,并且它具有在XSD中定义的所有标签,即没有标签丢失.它匹配xsd中定义的数据类型.完成之后,我需要解析xml文件以获取数据并将其存储在数据库中.
有问题吗?1)使用带有XmlDocument的XmlReaderSetttings和带有Validate方法的XmlReader将帮助我实现我需要的东西吗?有没有人用sampel代码帮我?
2)解析xml文件以获取特定标记的最佳方法是什么?
我是VB.net的新手,所以任何示例代码帮助将不胜感激.谢谢!
我在异步方法中抛出异常时遇到了一些我无法理解的行为.
以下代码将在调用ThrowNow方法时立即抛出异常.如果我将该行注释掉并直接抛出异常,那么吞没异常并且不会在Unobserved事件处理程序中引发异常.
public static async void ThrowNow(Exception ex){
throw ex;
}
public static async Task TestExAsync()
{
ThrowNow(new System.Exception("Testing")); // Throws exception immediately
//throw new System.Exception("Testing"); // Exception is swallowed, not raised in unobserved event
await Task.Delay(1000);
}
void Main()
{
var task = TestExAsync();
}
Run Code Online (Sandbox Code Playgroud)
更令人困惑的是,如果我async从ThrowNow方法中删除关键字,则会再次吞下异常.
我认为异步方法同步运行,直到达到阻塞方法.在这种情况下,似乎删除async关键字使其行为异步.
我在我的Word插件中使用Microsoft.Bcl.Async,我的插件编译为exe(test_addin.exe)文件,从Microsoft Word作为程序集加载,当我直接启动可执行文件时,一切正常,但是当我从Word运行它时,我收到一个错误,说它无法加载Systems.Threading.Tasks程序集.
Could not load file or assembly System.Threading.Tasks...
Run Code Online (Sandbox Code Playgroud)
看起来它与绑定重定向有关,当我尝试从Word运行应用程序时,它期望配置文件位于'C:\Program Files (x86)\Microsoft Office\Office15'文件夹中并被命名WINWORD.exe.config,遗憾的是这是不可能的,因为我可能无法访问该文件夹.
我的test_addin.exe.config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我试过设置AppDomain.CurrentDomain.SetupInformation.ConfigurationFile指向正确的路径,但它似乎没有帮助,是否有其他方法使其适用于Office加载项?
我试图根据每个http请求标头设置我的DbContext的连接字符串.是否有可能在.NET Core中这样做?我在MVC5中做到了,但我无法在.NET核心中实现它.
在
public void ConfigureServices(IServiceCollection services)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
我不知道http标头,所以我在哪里可以做到?
c# ×8
async-await ×4
asynchronous ×4
.net ×3
asp.net-mvc ×2
.net-4.5 ×1
.net-core ×1
asp.net ×1
registrykey ×1
spring-boot ×1
vb.net ×1
xml ×1
xsd ×1