如何使用Async Await(How to use Async Await)
我的WCF服务中有一个主方法。 请帮我指出我需要放置Aync和Await的确切位置。 我在main方法中有2个方法,一个方法更新一个表,另一个方法从SQl获取值,我将值作为对象返回给客户端。我希望在Parallel中执行2个方法。 我想快速将对象返回给客户端
public object GetSensorConfiguration(string desc) { object oObject = null; UpdateTable(desc); oObject = GetobjectValue(desc); return oObject ; } private void updatetable(string desc) { // no return value.. just update } private object GetobjectValue(string desc) { object objA = null; //Get values for the objA; return objA; }
I have a Main Method inside my WCF service. Please help me to point where exactly i need to put Aync and Await. I have 2 methods inside main method , one method updates a table and another method fetches values from SQl and i return back the values as object to client.I want 2 methods to execute in Parallel. I want to quickly return the object to the client
public object GetSensorConfiguration(string desc) { object oObject = null; UpdateTable(desc); oObject = GetobjectValue(desc); return oObject ; } private void updatetable(string desc) { // no return value.. just update } private object GetobjectValue(string desc) { object objA = null; //Get values for the objA; return objA; }
原文:https://stackoverflow.com/questions/40743539
最满意答案
我要从你自己的答案中重新混合rakefile / tasks。 您应遵循一些Ruby / Rake惯例以吸引更广泛的受众。 我对如何编写令人敬畏的rakefiles有一些看法。 尤其是...
1.不要直接调用/执行Rake任务
Rake::Task[:unitTestWithCoverage].execute( testAssembly )
有很多原因可以解释为什么你不想直接使用Rake
invoke
或execute
。 其中一个不调用依赖任务,一个只运行一次依赖任务......它变得愚蠢。 应始终有一种方法来构建正确定义和依赖的任务。2.不要参数化“内部”任务
exec :unitTestWithCoverage, [:testAssembly] do |cmd, testAssembly|
您可能有一个静态列表或通配符匹配的测试程序集列表。 您应该能够在不使用参数的情况下构建具体任务。 当用户可以使用命令行中的自定义输入调用它们时,我只使用参数化任务。
3.无需在每个任务中创建路径
testAssemblyRealPath = Pathname.new(testAssembly).realpath testAssemblyName = File.basename(testAssemblyRealPath)
我们将探索Rake
FileList
来弄清楚如何创建自定义,懒惰, 映射的文件名/路径/任意字符串列表!混音(更新)
我在第一个答案中犯了一个严重错误(我保留在底部,供参考)。 我会解释那部分你的/我的教育出了什么问题!
以下是我的新建议。 这对我来说应该是显而易见的,因为我在自己的构建中使用mspec测试运行器任务犯了同样的错误。
dotcover_path = 'path/to/dotcover.exe' xunit_runner_path = 'path/to/xunitrunner.exe' test_assemblies = FileList['path/to/output/**/*.test.dll'] coverage_results = "#{test_results_path}/coverage_results.dcvr" task :cover_all => [ :tests_with_coverage, :publish_coverage_results ] exec :tests_with_coverage do |cmd| cmd.comand = dotcover_path cmd.parameters = [ "cover", "/AnalyseTargetArguments=False", "/TargetExecutable=\"#{xunit_runner_path}\"", "/TargetArguments=\"#{test_assemblies.join ','}\"", "/Output=\"#{coverage_results}\"" ] end task :publish_coverage_results => [ :tests_with_coverage ] do import_data 'dotNetCoverage', 'dotCover', coverage_results end def import_data(type, tool, file) puts "##teamcity[importData type='#{type}' tool='#{tool}' path='#{file}']" end
说明
我默认使用绝对路径(通常使用
File.expand_path
和__FILE__
常量)。 有些工具/任务需要相对路径,但您始终可以使用File.basename
方法。dotcover_path = 'path/to/dotcover.exe' xunit_runner_path = 'path/to/xunitrunner.exe'
我们仍然可以使用构建的程序集的
FileList
来定义目标程序集。 在测试任务的主体执行之前,我们不会对其进行评估。test_assemblies = FileList['path/to/output/**/*.test.dll']
coverage运行器支持具有单个结果文件的多个程序集。 这样我们就没有其他复杂的
pathmap
。coverage_results = "#{test_results_path}/coverage_results.dcvr"
从CI服务器调用此命令以运行测试并发布覆盖结果。
task :cover_all => [ :tests_with_coverage, :publish_coverage_results ]
这项任务现在简单明了。 一些注意事项:1。使用
join
将目标列表转换为正确格式的字符串。 2.我倾向于引用具有文件路径的exec任务参数(需要转义,\"
)。exec :tests_with_coverage do |cmd| cmd.command = dotcover_path cmd.parameters = [ "cover", "/AnalyseTargetArguments=False", "/TargetExecutable=\"#{xunit_runner_path}\"", "/TargetArguments=\"#{test_assemblies.join ','}\"", "/Output=\"#{coverage_results}\"" ] end
相同的旧发布任务/方法。
task publish_coverage_results => [ :tests_with_coverage ] do import_data 'dotNetCoverage', 'dotCover', coverage_results end def import_data(type, tool, file) puts "##teamcity[importData type='#{type}' tool='#{tool}' path='#{file}']" end
旧混音
剪切以显示问题区域,假设其余部分无趣或存在于新解决方案中。
直到构建任务之后 ,测试程序集才会存在。 这通常不是问题,因为
FileList
是懒惰的。 在您对其进行枚举之前,它不会进行评估(例如,通过使用each
,map
或zip
)。但是,我们立即在它上面生成测试任务......所以这不起作用。 它在列表中没有任何内容,也不会生成任何任务。 或者,更糟糕的是,它会拾取先前构建的输出并可能做坏事(如果你没有完全清理输出目录)。
test_assemblies = FileList['path/to/output/**/*.test.dll'] coverage_results = test_assemblies.pathmap "#{test_results_path}/%n.dcvr" cover_task_names = test_assemblies.pathmap "cover_%n" test_assemblies.zip(coverage_results, cover_task_names) do |assembly, results, task_name| exec task_name do |cmd| cmd.command = dotcover_path cmd.parameters = [ "cover", "/AnalyseTargetArguments=False", "/TargetExecutable=#{xunit_path}", "/TargetArguments=#{assembly}", "/Output=#{results}" ] end end
For anyone that's interested here's my final rake tasks working
task :unitTestsWithCoverageReport => [ :unitTestsWithCoverage, :coverageServiceMessage ] exec :unitTestsWithCoverage do |cmd| fullPathAssemblies = [] @unitTestAssemblies.each do |testAssembly| testAssemblyRealPath = Pathname.new(testAssembly).realpath fullPathAssemblies << testAssemblyRealPath end cmd.command = @dotCoverRealPath cmd.parameters = [ "cover", "/AnalyseTargetArguments=False", "/TargetExecutable=#{@xUnitRunnerRealPath}", "/TargetArguments=\"#{fullPathAssemblies.join ';'}\"", "/Output=#{@testResultsRealPath}/coverage.dcvr" ] end task :coverageServiceMessage do |t| puts "##teamcity[importData type='dotNetCoverage' tool='dotcover' path='#{@testResultsRealPath}/coverage.dcvr']" end
Many thanks to @AnthonyMastrean as he showed me some really nice little ruby tricks and how I should be structuring my rake file properly.
相关问答
更多-
gulp注入相对文件路径(gulp inject relative file paths)[2021-01-08]
你正在听错事件。 从Stream上的Node.js文档 : 'finish'和'end'事件分别来自父Writable和Readable类。 调用stream.end()并且stream._transform()处理了所有块之后触发'finish'事件,在输出所有数据之后触发'end' ,这是在stream._flush()的回调之后被称为。 这意味着你要么必须听'finish'事件: gulp.task("inject", function (cb) { gulp .src("src/clie ... -
使用raise或fail方法与任何其他Ruby脚本一样( fail是别名为raise )。 该方法将字符串或异常作为参数,用作在脚本终止时显示的错误消息。 这也将导致脚本将值1返回到调用shell。 这里记载了其他地方。 Use the raise or fail method as you would for any other Ruby script (fail is an alias for raise). This method takes a string or exception as an a ...
-
关于NDepend,在.ndproj项目文件中,您需要调整XML以引用dotCover coverage文件,它看起来像:
.\CoverageFiles\CoverageFile.xml -
Rake任务在命名空间之外(Rake task outside of namespace)[2022-01-03]
该行将puts "I'm in setup"不是任何任务的一部分 - 它将在您正在解析文件时执行您指定的任何任务(甚至是不存在的任务)(严格地说,当Ruby解析文件时不会执行该任务,但是正在执行并设置rake任务): $ rake foo I'm in setup rake aborted! Don't know how to build task 'foo' (See full trace by running task with --trace) 只有在文件被读取后才会执行任务查找,这就是引用的内容 ... -
我通过从配置文件中删除过滤器来修复此问题,而是将它们作为参数添加到.exe / Filters = +:module = ; class = ; function = ; - :module = .Test *; - I fixed this issue by removing the filters from the config file and instead adding them as arguments in the .exe /Filters=+:module=;class=;function ...
-
解决这个问题的最简单方法(但是一种霰弹枪的方法)来自你的shell类型 环境| grep PATH 然后获取此输出并将该crontab添加到该用户 所以它看起来像这样 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 42 6 * * * root job1 47 6 * * 7 root job2 52 6 1 * ...
-
您可以在任务中定义帮助程序,使其可用于该任务及所有后续任务: desc 'has access to local helper' task :accessible do def helper return "the helper" end puts "I have access to #{helper}" end desc 'has access too' task 'after-accessible' => ['accessible'] do puts "this ran af ...
-
您是否尝试过指定.testsettings文件的路径? 在“MsTest”页面上,单击“使用此测试运行配置”,然后选择.testsettings文件(dotCover | Options)。 Have you tried to specify path to .testsettings file? On the 'MsTest' page, click 'Use this Test Run Configuration', and then choose a .testsettings file (dotCo ...
-
为什么这个dotCover rake任务使用这些相对文件路径失败?(Why does this dotCover rake task fail using these relative file paths?)[2023-07-01]
我要从你自己的答案中重新混合rakefile / tasks。 您应遵循一些Ruby / Rake惯例以吸引更广泛的受众。 我对如何编写令人敬畏的rakefiles有一些看法。 尤其是... 1.不要直接调用/执行Rake任务 Rake::Task[:unitTestWithCoverage].execute( testAssembly ) 有很多原因可以解释为什么你不想直接使用Rake invoke或execute 。 其中一个不调用依赖任务,一个只运行一次依赖任务......它变得愚蠢。 应始终有一种 ... -
您能否提供有关NUnit步骤的信息,尤其是关于.Net Coverage(过滤器)的信息。 你能否确认测试实际上已经开始了? 完整的构建日志(粘贴在pastebin.com或此类提供商)也将不胜感激。 Stupid me. The DLL that was being ignored was spelled *Test.dll instead of *Tests.dll. I use *Tests.dll in my test path. The issue got fixed once I changed ...