Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service #7

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.Core/DependencyInjectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class DependencyInjectionExtension
{
public static void AddFFmpegServices(this IServiceCollection services)
{
services.AddDbContext<FFmpegDbContext>()
services.AddDbContext<FFmpegDbContext>(ServiceLifetime.Transient)
.AddTransient<LogManager>()
.AddTransient<ConfigManager>()
.AddTransient<PresetManager>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ internal class Options
[Option('p', Required = false, HelpText = "命名管道名称")]
public string PipeName { get; set; }

[Option('s', Default = false, Required = false, HelpText = "注册开机启动")]
public bool RegisterStartup { get; set; }

[Option('u', Default = false, Required = false, HelpText = "取消开机启动")]
public bool UnregistereStartup { get; set; }

[Option('d', Default = false, Required = false, HelpText = "设置工作目录为程序所在目录")]
public bool WorkingDirectoryHere { get; set; }
}
Expand Down
29 changes: 29 additions & 0 deletions SimpleFFmpegGUI.Host.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using CommandLine;
using Microsoft.Extensions.Hosting;
using SimpleFFmpegGUI;
using System.Runtime.InteropServices;

string pipeName = Startup.DefaultPipeName;
Parser.Default.ParseArguments<Options>(args)
.WithParsed(o =>
{
if (o.PipeName != null)
{
Console.WriteLine($"管道名设置为: {o.PipeName}");
pipeName = o.PipeName;
}
else
{
Console.WriteLine($"管道名未设置,默认为: {Startup.DefaultPipeName}");
}
if (o.WorkingDirectoryHere)
{
FzLib.Program.App.SetWorkingDirectoryToAppPath();
Console.WriteLine("工作目录设置为程序目录:" + FzLib.Program.App.ProgramDirectoryPath);
}
});

var builder = Host.CreateDefaultBuilder(args);
ConsoleLogger.StartListen();
Startup.InitializeServices(builder,pipeName);
builder.Build().Run();
32 changes: 32 additions & 0 deletions SimpleFFmpegGUI.Host.Console/SimpleFFmpegGUI.Host.Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\Generation\Debug\Host</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\Generation\Release\Host</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SimpleFFmpegGUI.Host\SimpleFFmpegGUI.Host.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="FzStandardLib">
<HintPath>..\libs\FzStandardLib.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions SimpleFFmpegGUI.Host.WindowsService/CreateWindowsService.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@echo off

net session >nul 2>&1
if %errorlevel% neq 0 (
echo need Administrator

powershell -Command "Start-Process '%~0' -Verb RunAs"
exit /b
)


set "current_dir=%~dp0"
set "service_name=SimpleFFmpegService"
set "exe_path=%current_dir%SimpleFFmpegGUI.Host.WindowsService.exe"

sc create %service_name% binPath= "%exe_path%" start= auto
sc start %service_name%

echo Service has been created and set to start automatically at system boot.
pause


19 changes: 19 additions & 0 deletions SimpleFFmpegGUI.Host.WindowsService/DeleteWindowsService.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@echo off

net session >nul 2>&1
if %errorlevel% neq 0 (
echo need Administrator

powershell -Command "Start-Process '%~0' -Verb RunAs"
exit /b
)


set "service_name=SimpleFFmpegService"
sc stop %service_name%
sc delete %service_name%

echo Service has been deleted.
pause


17 changes: 17 additions & 0 deletions SimpleFFmpegGUI.Host.WindowsService/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using SimpleFFmpegGUI;
using SimpleFFmpegGUI.Host.Service;
using System.Diagnostics;

var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
services.AddWindowsService(options =>
{
options.ServiceName = "SimpleFFmpegHost";
});
});
Directory.SetCurrentDirectory(Path.GetDirectoryName(Environment.ProcessPath));
Startup.InitializeServices(builder);
builder.Build().Run();

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-SimpleFFmpegGUI.Host.Service-8825e1c0-f6ff-4b92-b814-abf06f255eec</UserSecretsId>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\Generation\Debug\Host</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\Generation\Release\Host</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SimpleFFmpegGUI.Host\SimpleFFmpegGUI.Host.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="CreateWindowsService.bat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="DeleteWindowsService.bat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions SimpleFFmpegGUI.Host.WindowsService/Worker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Diagnostics;
using System.IO;

namespace SimpleFFmpegGUI.Host.Service
{
public class Worker : BackgroundService
{
//private readonly ILogger<Worker> _logger;
//private Process _process;

//public Worker(ILogger<Worker> logger)
//{
// _logger = logger;
//}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000, stoppingToken);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
8 changes: 8 additions & 0 deletions SimpleFFmpegGUI.Host.WindowsService/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
Loading