是否有可能通过某种方式获取对 IServiceProvider 或可以解决依赖关系的某个类的引用来动态获取依赖关系?例如,在处理异常以UseExceptionHandler
向客户端输出一些有意义的内容时,我还想做一些自定义日志记录以记录有关抛出的异常的一些内容。
例如,假设我在ASP.net Core 项目Configure
的Startup
类中的方法中有此代码:
app.UseExceptionHandler(
builder =>
{
builder.Run(
async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "text/html";
var error = context.Features.Get<IExceptionHandlerFeature>();
if (error != null)
{
// TODO Log Exception. Would like to do something like this:
// var logger = ServiceProvider.Resolve<ILogger>();
// logger.LogCritical("Unhandled Error :"error.Error.ToString());
await context.Response.WriteAsync($"<h1>Error: {error.Error.Message}</h1>").ConfigureAwait(false);
}
});
});
Run Code Online (Sandbox Code Playgroud)
当我没有传入 ILogger 的构造函数时,如何获取 ILogger 的实例?
我试图使用一个单独的Update语句来更新具有不同值的多个记录(我不是要尝试更新许多行以具有相同的值,这非常简单).这就是我现在正在尝试的:
using (var cn = GetOpenConnection()) {
// get items where we need to set calculated fields that will now be persisted in the DB
var items = cn.Query<MaintenanceItem>("select TOP 500 * from [Maintenance] where Tolerance IS NOT NULL");
foreach (var mi in maintItems)
{
// Set calculated fields on multiple recrods
logic.CalculateToleranceFields(mi, true);
}
var updateInput = items.Select(a => new {a.ToleranceMonths, a.ToleranceDays, a.ToleranceHours, a.ToleranceLandings, a.ToleranceCycles, a.ToleranceRIN }).ToList();
// THIS DOESN'T WORK - attempting to update multiple rows with different …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个Android应用程序(使用mono/Xamarin实现),允许进行非消费类应用内购买(用户只需购买一次该功能,然后他们就可以永久访问所有设备).我正在尝试使用Xamarin.InAppBilling组件来完成此任务.
根据Xamarin.InAppBilling组件(http://components.xamarin.com/view/xamarin.inappbilling)的文档,这些事件存在供我使用:
Xamarin.InAppBilling定义了您可以监视和响应的以下事件:
OnConnected - 当组件附加到Google Play时触发.
OnDisconnected - 当组件与Google Play分离时触发.
OnInAppBillingError - 在组件内发生错误时引发.
OnProductPurchasedError - 购买产品或订阅时出错.>
OnProductPurchase - 成功购买产品时启动.
OnPurchaseConsumedError - 在消费购买时出错.
OnPurchaseConsumed - 在成功消费时获得.
我看到OnConnected,OnDisconnected和OnInAppBillingError事件被定义为Xamarin.InAppBilling.InAppBillingServiceConnection类的一部分.
在程序集浏览器中,我发现其他事件被定义为Xamarin.InAppBilling.InAppBillingHandler类的一部分,但我不确定访问这些事件的最佳方法,因为它们不能通过IInAppBillingHandler接口获得.通过属性Xamarin.InAppBilling.InAppBillingServiceConnection.BillingHandler访问它们是有意义的,但该属性返回一个实例强制转换为IInAppBillingHandler而不是InAppBillingHandler类.
我的问题:
我是否应该期望此代码能够作为内联评论注意到它应该如何工作?
// When activity starts...
_serviceConnection = new InAppBillingServiceConnection (CurrentContext, publicKey);
_serviceConnection.OnConnected += () =>
{
var bh = _serviceConnection.BillingHandler as InAppBillingHandler;
bh.OnProductPurchased += (sku) => {
// This code should run when call to BuyProduct is successful
var purchasedProductId = sku;
};
bh.OnProductPurchasedError += (int responseCode, string sku) …
Run Code Online (Sandbox Code Playgroud)