te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>c# - ASP.NET Core Web API calling .NET console aspp - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - ASP.NET Core Web API calling .NET console aspp - Stack Overflow

programmeradmin2浏览0评论

I have an ASP.NET Core 8.0 Web API with a project reference to .NET 8.0 console app. An endpoint in the API controller calls the underlying console app (by direct method invocation and not by ProcessStart) to run some services. All the logging in the console app is done using Console.WriteLine.

I am worried if there will be concurrency issues if the same end point from controller is invoked multiple times in parallel calling the console app.

Controller call:

[HttpPost("SomeService")]
public string SomeService([FromBody]  JsonObject jobParams)
{
    return someService.InvokeSomeService(jobParams.ToString());
}

Console app service:

 public class SomeService
 {
     public string InvokeSomeService(string json)
     {
         string originalOutput = Console.Out;

         using (StringWriter stringWriter = new StringWriter())
         {
             try
             {
                 string version = "1.0";
                 string logData = "";

                 Console.SetOut(stringWriter);

                 ServiceLogger.LogInformation($"***Start invoking the Some service {version}***");
                 Feeds feeds = new Feeds();
                 feeds.Start(json);
                 ServiceLogger.LogInformation($"***Completed running the Some service ***");

                 stringWriter.Flush();
                 logData = stringWriter.ToString();
                 feeds.SaveFeedsServiceLogs(logData);
                 return logData;
             }
             finally
             {
                 Console.SetOut(originalOutput);
             }
         }
     }
 }

I have an ASP.NET Core 8.0 Web API with a project reference to .NET 8.0 console app. An endpoint in the API controller calls the underlying console app (by direct method invocation and not by ProcessStart) to run some services. All the logging in the console app is done using Console.WriteLine.

I am worried if there will be concurrency issues if the same end point from controller is invoked multiple times in parallel calling the console app.

Controller call:

[HttpPost("SomeService")]
public string SomeService([FromBody]  JsonObject jobParams)
{
    return someService.InvokeSomeService(jobParams.ToString());
}

Console app service:

 public class SomeService
 {
     public string InvokeSomeService(string json)
     {
         string originalOutput = Console.Out;

         using (StringWriter stringWriter = new StringWriter())
         {
             try
             {
                 string version = "1.0";
                 string logData = "";

                 Console.SetOut(stringWriter);

                 ServiceLogger.LogInformation($"***Start invoking the Some service {version}***");
                 Feeds feeds = new Feeds();
                 feeds.Start(json);
                 ServiceLogger.LogInformation($"***Completed running the Some service ***");

                 stringWriter.Flush();
                 logData = stringWriter.ToString();
                 feeds.SaveFeedsServiceLogs(logData);
                 return logData;
             }
             finally
             {
                 Console.SetOut(originalOutput);
             }
         }
     }
 }
Share Improve this question edited Feb 17 at 11:35 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 17 at 11:24 Prasanth SarathPrasanth Sarath 295 bronze badges 8
  • 3 What is the question? Yes, this is a problem, you really shouldn't use a Console App like that. At all. Extract the wanted functionality into a library project and use that. (And drop the Console.WriteLine()s) – Fildor Commented Feb 17 at 11:29
  • 1 What are you trying to do here - seems nonsensical for a WebApi to redirecting logging to console ? Even ignoring that, what is your writer configured to output to ? Use the built in asp ILogger – auburg Commented Feb 17 at 11:32
  • @fildor sorry if I was not clear, the app is actually a library project. The we api references this library to invoke some services – Prasanth Sarath Commented Feb 17 at 12:06
  • @Auburg the web api is not redirecting the logs. Web api having a c# library project invokes the project to run some services. The underlying library project is a legacy one and it has logs written using console.writeline. Earlier this exe was used to be triggered by another exe and that calling exe used to read all the console output logs. Now we have replaced the calling exe with a web api. – Prasanth Sarath Commented Feb 17 at 12:12
  • 1 I'd strongly suggest a rewrite, then. With proper input/output, proper logging using the canonical Logging APIs and testing and all you'd want in a modern library. – Fildor Commented Feb 17 at 13:13
 |  Show 3 more comments

1 Answer 1

Reset to default 0

You can use built-in logger instead creating logger yourself: https://learn.microsoft/en-us/dotnet/core/extensions/logging?tabs=command-line#get-started (Logging in C# and .NET microsoft article)

Add logger in your startup file (pay attention to the .AddConsole() call):

services.AddLogging(builder => builder.AddConsole());

And then use it:

public class YourClass(ILogger<YourClass> logger) {
    [HttpPost("SomeService")]
    public string SomeService([FromBody] JsonObject jobParams)
    {
        logger.LogInformation(...);
        return someService.InvokeSomeService(jobParams.ToString());
    }

}
发布评论

评论列表(0)

  1. 暂无评论