在Angular CLI中生成服务时,它会添加带有'provided in'属性的额外元数据,其默认值为'root',用于Injectable装饰器.
@Injectable({
providedIn: 'root',
})
Run Code Online (Sandbox Code Playgroud)
究竟做了什么?我假设这使得服务可用作整个应用程序的"全局"类型单例服务,但是,在AppModule的提供者数组中声明此类服务会不会更清晰?
更新:
对于其他任何人,以下段落也提供了另一个很好的解释,特别是如果您想要仅为功能模块提供服务.
"现在有一种新的,推荐的方法,使用新的provideIn属性直接在@Injectable()装饰器中注册提供者.它接受'root'作为值或应用程序的任何模块.当你使用'root ',您的注入将在应用程序中注册为单例,并且您不需要将其添加到根模块的提供者.同样,如果您使用providedIn:UsersModule,注入可注册为UsersModule的提供者无需将其添加到模块的提供者." - https://blog.ninja-squad.com/2018/05/04/what-is-new-angular-6/
更新2:
经过进一步调查,我认为它只是有用 @Injectable()
如果要在除根模块之外的任何模块中提供服务,那么最好在功能模块的装饰器中使用providers数组,否则您将受到循环依赖性的困扰.这里有趣的讨论 - https://github.com/angular/angular-cli/issues/10170
只是想知道从服务总线主题的订阅中清除消息的最佳方法(即使通过 Portal、Powershell 或 C#)。
假设我们有一个有 4 个订阅的主题,我们只想从其中一个订阅中清除消息。
我有一种感觉,唯一的方法可能是在 while 循环中阅读消息,但希望有更好的结果。
更新:
除了使用代码之外,您还可以按照答案中的建议使用服务器资源管理器 - 右键单击订阅并清除消息:
我正在尝试创建类似于此处描述的GeoJSON格式的JSON输出:http: //geojson.org/geojson-spec.html
特别是,我从文本格式的数据源返回文本,并希望以下面的评论显示的格式将我的DTO转换为JSON.我遇到的主要问题是尝试创建没有属性名称的坐标数组[[...]].
码:
/*
Geometry Text Format from database: POLYGON ((319686.3666000003 7363726.7955, 319747.05190000031 7363778.9233, 319700.78849999979 7363832.7814, 319640.10329999961 7363780.6536, 319686.3666000003 7363726.7955))
And we want format:
"geometry": {
"type": "Polygon",
"coordinates": [[
[319686.3666000003, 7363726.795],
[319747.0519000003, 7363778.9233],
[319700.78849999979, 7363832.7814],
[319640.10329999961, 7363780.6536],
[319686.3666000003, 7363726.795]
]]
}
*/
// Strip out everything except the coordinates
var coordRawText = myWkt.Replace("POLYGON ((", "");
coordRawText = coordRawText.Replace("))", "");
coordRawText = coordRawText.Replace(", ", ",");
// Seperate coordinates to iterate through
var coordsArray = coordRawText.Split(',');
var coordsEnumerable …Run Code Online (Sandbox Code Playgroud) 这被证明有点难以追查,但是当使用Effort测试Entity Framework 6时,我似乎在尝试访问其中一个DBSet时得到KeyNotFoundException("给定的密钥在字典中不存在")错误库.
我注意到它在DbContext中使用了一个或两个DbSet,但是一旦我开始向DbContext添加多个DbSets,我收到上述错误.
示例代码(这是我整个代码的简化,当我从DbContext中注释掉一些DbSets然后将它们放回来时,错误似乎是随机的.我也有模型的部分类,但它有时也成功,所以似乎奇怪的):
测试
[Fact]
public void MyTest()
{
var connection = Effort.DbConnectionFactory.CreateTransient();
var context = new StubDbContext(connection);
var count = context.Models1.Count();
Assert.Equal(count, 0);
}
Run Code Online (Sandbox Code Playgroud)
DBContext和DbSets模型
public class StubEntityModelA
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class StubEntityModelB
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class StubEntityModelC
{
[Key]
public int Id { get; set; } …Run Code Online (Sandbox Code Playgroud) 当在另一个方法中创建参数时,如何设置Moq以更改内部方法参数的值.例如(下面的简化代码):
public class Response
{
public bool Success { get; set; }
public string[] Messages {get; set;}
}
public class MyBusinessLogic
{
public void Apply(Response response);
}
public class MyService
{
private readonly MyBusinessLogic _businessLogic;
....
public Response Insert()
{
var response = new Response(); // Creates the response inside here
_businessLogic.Apply(response);
if (response.Success)
{
// Do more stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
假设我想对Insert()方法进行单元测试.如何设置Business Logic的Apply()方法的模拟以接收任何Response类,并让它填充返回的Response对象,并将Success设置为true,以便其余代码可以运行.
顺便说一句,我已经改变了应用()方法的返回类型为布尔(而不是空隙),以有订货数量简单地返回true,类似于下面:
mockMyBusinessLogic.Setup(x => x.Apply(It.IsAny<Response>()).Returns(true);
Run Code Online (Sandbox Code Playgroud)
但是,如果有一个方法可以做某事,并返回一些东西(我更喜欢让方法只做一个或另一个),这感觉很尴尬.
希望有一种方式可能看起来像下面的东西(使用void时):
mockMyBusinessLogic.Setup(
x => x.Apply(It.IsAny<Response>()).Callback(()
=> new Response { Success = …Run Code Online (Sandbox Code Playgroud) 在Azure门户中复制数据库永远不会结束.
通常,当我复制250GB数据库时,它会在不到一个小时内完成.
今天,当我复制时,它似乎永远不会完成,现在已经超过两到三个小时了.
在服务器活动日志中,最后一个条目只是说发生了更新
有关如何查看更多进度,完成百分比或任何其他方式来查看可能锁定它的任何想法?在activty log json中看不到任何用途.
当使用GetTypes执行GetExecutingAssembly时,它正在检索带有奇怪的"+ <> c"后缀的额外类型.我假设这可能是一些特殊的角色或什么?如下图所示.预期结果应该只是列表中的3个项目,但它是检索一个额外的第4个项目,它是其中一个项目的副本,但最后是"+ <> c"?
在处理返回的类型之前,我必须做一些黑客检查,如下所示:
if (@class.ToString().Contains("<")) // Ignore funny types returned by reflection
continue;
Run Code Online (Sandbox Code Playgroud)
如果感兴趣的话,这些类的一般结构是相同的,如下所示:
public class WebJobLogsForErrorsCheck : MonitoringJob
{
public WebJobLogsForErrorsCheck(IKernel kernel) : base(kernel)
{
}
}
Run Code Online (Sandbox Code Playgroud) 是否可以在运行时更改数据库连接超时?据我所知唯一的方法是修改连接字符串,然后用修改后的连接字符串创建一个新的连接实例(替换字符串的超时属性)。
<add name="MyConnection" connectionString="Data Source=.\SQL2016;Initial Catalog=MyDatabase;Persist Security Info=False;User ID=DbUser;Password=DbPassword;Connection Timeout=6" providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)
当尝试通过 Internet 连接到数据库时,服务上的某些方法可能需要超快才能失败,而其他方法可能有更多的宽限期。
是否有更简洁的方法来修改连接时间(因为数据库位于 Internet 资源上)?
注意:只对在运行时改变数据库连接时间感兴趣,我知道但对更改命令执行时间不感兴趣。
亲切的问候,
斯特凡
更新
styx 和 Zohar Peled 提供的答案更符合我的要求,特别是在运行时创建连接实例之前,使用SqlConnectiongStringBuilder类来修改连接超时。
c# ×5
.net ×1
ado.net ×1
angular ×1
angular6 ×1
azure ×1
azureportal ×1
dto ×1
effort ×1
geojson ×1
json ×1
linq ×1
mocking ×1
moq ×1
powershell ×1
sql-server ×1
unit-testing ×1