我正在尝试使用Topshelf和Serilog(分别为Serilog.Extras.Topshelf包)为我的Windows服务设置简单的日志配置.
HostLogger.UseLogger(new SerilogHostLoggerConfigurator(new LoggerConfiguration()
.WriteTo.RollingFile(AppDomain.CurrentDomain.BaseDirectory + "\\logs\\app-{Date}.log")
.WriteTo.ColoredConsole()
.MinimumLevel.Debug()
.CreateLogger()));
HostFactory.Run(x => {
x.UseSerilog();
...
Run Code Online (Sandbox Code Playgroud)
该服务运行正常,但没有输出,也没有输出到控制台或指定的日志文件(我可以看到正在创建一个但它仍然是空的).有没有人使用这两个框架?
发生了奇怪的事情:在我的web api中,我在使用Ninject解析时将一个存储库注入控制器.存储库存储在私有只读成员变量中.工作得很好!当调用api方法时,我访问变量 - 只是为了看到它突然变为空!
伪示例:
public class MyController : ApiController {
private readonly IRepo _repo;
public MyController(IRepo repo) {
Guard.AgainstNullArgument("repo", repo); // guarding to
// make sure it's not null
// (would throw ex)
_repo = repo; <--- successfully injected
}
// calling this method
public HttpResponseMessage TestMethod() {
_repo.. <--- suddenly null
}
}
Run Code Online (Sandbox Code Playgroud)
我已经将问题追溯到一个小细节:控制器中的一个方法(不是获取访问的方法)使用自定义属性进行注释,该属性指示ninject使用工作单元拦截方法.如果我把属性拿走,一切都神奇地再次起作用.
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Interface)]
public class UnitOfWorkAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
public class EfUnitOfWorkInterceptor : SimpleInterceptor
{ …Run Code Online (Sandbox Code Playgroud) 所以,我得到了一些我需要序列化/反序列化的类,这些类也恰好是域对象(至少是一些'em),因此我希望它们没有任何属性或者不依赖于某个框架.
我在Json.NET中查看了Custom Converters,但是在使用方面它们看起来非常" 古老 ",而不是,因为我们现在有了泛型,并且实现流畅的界面并不难.所以在我走弱路线之前等等.
...我正在寻找什么(伪):
public class MyModel {
public int Id { get; set; }
public string Name { get; set; }
public SomeObj SomeObj { get; set; }
}
public class MyModelConverter : JsonConverter<MyModel> {
public JsonConverter() {
RuleFor(x => x.Id).Name("Identifier");
RuleFor(x => x.SomeObj).Name("Data")
.WithConverter(new SomeObjConverter());
RuleFor(x => x.Name).Ignore();
}
}
Run Code Online (Sandbox Code Playgroud)
在Json.NET中有类似的东西吗?我只是错过了一些东西吗?(顺便说一句,我不能为我的属性等使用不同的名称,因为模型基于第三方规范).
我想使用模态从表中编辑一些数据.来自definitelyTyped的angular-ui-bootstrap的typescript定义中有各种接口,但它们没有文档,我无法找到有关如何使用它们的任何示例.
我想要实现的是这样的:

我是否正确地假设ItemsListController和ItemDetailModalController都需要一个相同范围的实例才能交换数据?如何使用上面的接口为模态指令定义控制器和模板?
我已经在这里找到了这个非打字稿的例子:https://angular-ui.github.io/bootstrap/#/modal
然而,作为初学者,我很难理解如果样本将所有内容都放在一个文件中而不分离问题的话会发生什么.
javascript angularjs typescript angular-ui angular-ui-bootstrap
在大多数任意应用中,需要在所有可用层中解决许多交叉问题,例如日志记录,消息总线,配置.我注意到的是,在某些类中,如果使用IoC注入模块,它们往往会完全炸毁构造函数.
public class MyService : IService
{
public MyService(ILogger logger, IAppSettings settings, IEventBus eventBus...)
{
}
}
Run Code Online (Sandbox Code Playgroud)
对于构造函数过度注入的常见情况,我倾向于将关注点折射成紧密组合在一起的构建块,因此我在类中获得的依赖项更少.但是,对于交叉切割概念,这是不可能的.
在日志框架中,静态工厂/服务似乎非常流行,例如
// Application root
MyLoggerService.SetFactory(log4NetFactory);
// Somewhere
MyLoggerService.GetLogger("name") // returns Log4NetLogger created by Log4NetFactory.
Run Code Online (Sandbox Code Playgroud)
我的问题是:对于各种交叉切割的东西,这种方法是否很好?如果代码最终看起来像这样,有什么缺点:
public class MyService : IService
{
private readonly IReallyNeedThat _dependency;
public MyService(IReallyNeedThat dependency)
{
_dependency = dependency;
}
private readonly ILogger _logger = LoggerService.GetLogger("MyService");
private readonly IEventBus _eventBus = EventBusService.GetEventBus();
private readonly IConfiguration _configuration = ConfigurationService.GetConfiguration(Level.Roaming)
private readonly IExceptionHandler _exceptionHandler = ExceptionPolicy.GetHandler();
private readonly ITracer _tracer = …Run Code Online (Sandbox Code Playgroud) c# design-patterns factory dependency-injection cross-cutting-concerns
为了进一步澄清我最初的问题,我用更多的'DDD' - 终点,常见模式和讨论论点重写了这个问题.可以在修订版本中找到原始版本.
在正确应用DDD时,在域内生成实体/聚合根的身份的位置和方式是什么?
我需要在创建或持久时为我的实体分配唯一标识.这些身份可以有多种风格
生成和分配身份的任务有很多种方法,从使用工厂创建身份,使用ORM授权到基础设施或数据库生成等.但是,如果正确应用DDD,身份应该在何处以及如何生成,考虑到我们不希望贫血领域模型和服务注入实体?
c# design-patterns domain-driven-design repository-pattern n-tier-architecture
对于我的大多数应用程序,我使用直接的DDD方法,这意味着分离洋葱架构的层,将域与基础架构分离等.两个经常重复的构建块,存储库和事件总线,看起来像这样(简化).
public interface IRepository<TEntity, TKey>
where TEntity : EntityBase<TKey>
{
void Add(TEntity entity);
}
public interface IEventBus {
void Publish<TEvent>(TEvent @event)
where TEvent : IEvent;
}
Run Code Online (Sandbox Code Playgroud)
最近,我开始研究CQRS,并且我认识到许多类似的模式,例如存储库,事件和命令总线.但是,例如CQRS中的存储库不负责存储/检索实体,而是负责管理聚合和构建事件流.
现在我想知道:他们俩一起工作吗?或者他们是完全不同的方法,只是分享一些常见的东西?
因此,今天我使用ILSpy + dotPeek反映了一个使用ILSpy + dotPeek的仲裁.NET程序集,以便在我偶然发现这个奇怪的部分(虚拟示例)时更深入地了解IL代码的工作原理:
public class SomeBaseClass {
public SomeBaseClass(SomeType[] iExpectACollection) {
...
}
}
public class SomeDerivedClass {
public SomeDerivedClass(SomeType onlyOneInstance) {
SomeType[] collection;
if(onlyOneInstance != null)
collection = new SomeType[] { onlyOneInstance };
base.\u002Ector(collection);
}
}
Run Code Online (Sandbox Code Playgroud)
据我所看到的,派生类不调用摆在首位的基础构造,而是做一些事onlyOneInstance,并随后调用是基构造.
我的问题是:在完成一些工作后,是否可以在C#中显式调用基础构造函数?或者这只能在IL中使用?我知道它很容易在Java中使用super(),但是我从未在.NET中看到它.
编辑
我刚和老板谈过,他可以发布一些真实的图书馆代码(这是我们公司的内部代码之一):
**IL PART**
.method public hidebysig specialname rtspecialname
instance void .ctor (
string contextId,
class MyComp.NetStack.BufferManager bufferManager,
class MyComp.NetStack.TcpChannelQuotas quotas,
class [System]System.Security.Cryptography.X509Certificates.X509Certificate2 clientCertificate,
class [System]System.Security.Cryptography.X509Certificates.X509Certificate2[] clientCertificateChain,
class [System]System.Security.Cryptography.X509Certificates.X509Certificate2 serverCertificate,
class MyComp.NetStack.EndpointDescription endpoint, …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有以下日志记录策略/附加程序:
这在整个应用程序中都非常有效,直到我使用IAppBuilder界面启动 OWIN 网络主机的第一行。我一调用WebApp.Start,就注意到以下行为:
经过进一步调查,我发现 OWIN 默默地将System.Diagnostics.DefaultTraceListener和的实例附加System.Diagnostics.TextWriterTraceListener到默认跟踪/调试输出,这可能是问题的根源。但是,DefaultTraceListener在 app.config 中明确声明并没有帮助。
有什么办法可以将 OWIN 配置为不那么......偷偷摸摸?
我有一个通用接口,表示实体的查询选项:
public interface IQueryOptions<TEntity> {
IQueryable<TEntity> Apply(IQueryable<TEntity> query);
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用查询字符串将查询选项传递到 WebApi 路由,并使用自定义模型绑定器构造 IQueryOptions。
(IQueryOptions 可以是 SortingOptions<> 或 PagingOptions<>,具体取决于传递给 api 的查询字符串)
public class DummyController : ApiController {
// optional parameter - query options are not required
public void Test([ModelBinder]IQueryOptions<MyEntity> queryOptions = null)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
public class QueryOptionsModelBinder : IModelBinder {
public bool BindModel(HttpActionContext actionContext...
}
Run Code Online (Sandbox Code Playgroud)
var queryProvider = new SimpleModelBinderProvider(typeof(IQueryOptions<>), new QueryOptionsModelBinder());
config.Services.Insert(typeof(ModelBinderProvider), 0, queryProvider);
Run Code Online (Sandbox Code Playgroud)
我目前无法让它工作。API 抛出以下错误:
Cannot create an instance of an interface. …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc model-binding asp.net-web-api asp.net-web-api2
c# ×8
.net ×1
angular-ui ×1
angularjs ×1
aop ×1
architecture ×1
asp.net-mvc ×1
cqrs ×1
debugging ×1
factory ×1
fluent ×1
il ×1
inheritance ×1
javascript ×1
json ×1
json.net ×1
log4net ×1
logging ×1
ninject ×1
owin ×1
reflection ×1
serilog ×1
topshelf ×1
tracesource ×1
typescript ×1