首页 \ 问答 \ 如何使用Async Await(How to use Async Await)

如何使用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
更新时间:2024-02-12 19:02

最满意答案

我要从你自己的答案中重新混合rakefile / tasks。 您应遵循一些Ruby / Rake惯例以吸引更广泛的受众。 我对如何编写令人敬畏的rakefiles有一些看法。 尤其是...

1.不要直接调用/执行Rake任务

Rake::Task[:unitTestWithCoverage].execute( testAssembly )

有很多原因可以解释为什么你不想直接使用Rake invokeexecute 。 其中一个不调用依赖任务,一个只运行一次依赖任务......它变得愚蠢。 应始终有一种方法来构建正确定义和依赖的任务。

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是懒惰的。 在您对其进行枚举之前,它不会进行评估(例如,通过使用eachmapzip )。

但是,我们立即在它上面生成测试任务......所以这不起作用。 它在列表中没有任何内容,也不会生成任何任务。 或者,更糟糕的是,它会拾取先前构建的输出并可能做坏事(如果你没有完全清理输出目录)。

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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。