tl; dr
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
my ($inputfilename, $outtextfilename, $outbinfilename) = @ARGV;
open(my $in, '<:encoding(UTF-8)', $inputfilename)
or die "Could not open file '$inputfilename' $!";
open my $outtext, '>', $outtextfilename or die;
my $outbin;
open $outbin, '>', $outbinfilename or die;
binmode $outbin;
while (my $aline = <$in>) {
chomp $aline;
if($aline =~ /\<\/FileSystem\>/) { # a match indicates the end of the text portion - the rest is binary
print $outtext "$aline\n"; # last line of the text portion
print "$aline\n"; # last line of the text portion
close ($outtext);
binmode $in; # change input file to binary?
# what do I do here to copy all remaining bytes in file as binary to $outbin??
die;
} else {
print $outtext "$aline\n"; # a line of the text portion
print "$aline\n"; # a line of the text portion
}
}
close ($in);
close ($outbin);
在经典SignalR Chat应用的一个变体中,我监视对SQL Server表的更改,并通过SignalR实时报告它们。观看this video 10秒钟以了解主意。我的代码改编自this project。聊天部分工作正常。
对SQL Server表的更改应该由类services.AddSingleton<ISomeService, SomeService>();
// SomeService not instantiated nor initialized?
监视。但是,它的构造函数永远不会被调用,因此应该在这里获取对SignalR集线器的引用。
这是我的Startup.ConfigureServices:
SqlDependencyService
这是我的SqlDependencyService类的一部分:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddSignalR();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// No instantiation ?!?
services.AddSingleton<IDatabaseChangeNotificationService, SqlDependencyService>();
// My lame attempt at forcing instantiation:
SqlDependencyService sqlDS = serviceProvider.GetService<SqlDependencyService>();
//sqlDS.Config(); // Fails, sqlDS is null.
}
这是服务的界面:
public class SqlDependencyService : IDatabaseChangeNotificationService
{
private readonly IConfiguration configuration;
private readonly IHubContext<ChatHub> chatHub;
// Never gets called!
public SqlDependencyService(IConfiguration configuration, IHubContext<ChatHub> chatHub)
{
this.configuration = configuration;
this.chatHub = chatHub;
}
// Never gets called!
public void Config()
{
SubscribeToPersonTableChanges();
}
private void SubscribeToPersonTableChanges()
{
// ...
}
// ...
}
如前所述,从不调用构造函数。另外,我看不到如何调用Config方法-一些ASP.NET Core约定魔术?
HomeController没有这个问题,它的构造函数被调用并传递了public interface IDatabaseChangeNotificationService
{
void Config();
}
:
IHubContext<ChatHub>
答案 0 :(得分:3)
仅添加服务不会导致实例化,实际上您需要将其注入到容器中以调用其构造函数。看来您希望该服务在启动时可用,所以我建议在启动时使用Configure
方法:
public void Configure(IApplicationBuilder app, IDatabaseChangeNotificationService dbService)
{
dbService.Config();
//snip
}