Fluent migrator throws timout exception - c#

I using Fluent migrator 3.3.2, which throws error on one of databases:
The error was Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
I was looking for how to set timeout option and found this post https://github.com/fluentmigrator/fluentmigrator/discussions/1472
But runner.RunnerContext is marked as obsolete and in there is no comment what I have to use instead. If I try to use it like runner.RunnerContext.Timeout = Timeout.Infinite; then have error:
Migration exception: "Object reference not set to an instance of an object."
Nothing found in google

You can set global timeout on dependency injection
var serviceProvider = new ServiceCollection()
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
.AddSqlServer()
//here
.WithGlobalCommandTimeout(TimeSpan.FromSeconds(120))
.WithGlobalConnectionString(tenant.ConnectionString)
.ScanIn(typeof(ApplicationDbContext).Assembly).For.Migrations())
.Configure<RunnerOptions>(opt => { opt.Tags = new[] { tenant.Id }; })
.AddLogging(lb => lb.AddFluentMigratorConsole())
.AddLogging(lb => lb.AddSerilog())
.BuildServiceProvider(validateScopes: false);

Related

.NET ILogger saves Error as Information level in Application Insights instead of Error

Please note that this post is not a duplicate of this one or at least the discussion in that post did not work. I have a .NET 6.0 console app with the following basic Application Insights configuration:
using var channel = new InMemoryChannel();
try
{
IServiceCollection services = new ServiceCollection();
services.Configure<TelemetryConfiguration>(config => config.TelemetryChannel = channel);
services.AddLogging(builder =>
{
builder.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Error);
builder.AddApplicationInsights("000000-0000-0000-0000-1111111");
builder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
});
IServiceProvider serviceProvider = services.BuildServiceProvider();
ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();
var now = DateTime.UtcNow;
logger.LogInformation("Logger is working... {Now}", now);
logger.LogError("Logger is issuing an error {Now}", now);
logger.LogWarning("Logger is issuing a warning {Now}", now);
logger.LogError(new Exception("Some ficititious exception"), "This is an exception messsage {Now}", now);
}
finally
{
// Explicitly call Flush() followed by Delay, as required in console apps.
// This ensures that even if the application terminates, telemetry is sent to the back end.
channel.Flush();
await Task.Delay(TimeSpan.FromMilliseconds(1000));
}
I am able to see the logs in the Application Insights but the errors are logged as "Information" level in there despite calling logger.LogError("Logger is issuing an error {Now}", now);. The other error log message which has an Exception object looks fine but my challenge is that the logger.LogError("Logger is issuing an error {Now}", now); is not working! Any idea or guidance on getting this problem resolved?

.Net Core Api: Invalid session: Microsoft Graph API Workbook Session Paused

I am using AzureAd along with the Microsoft Graph Client to do calculations on Excel spreadsheets. It works most of the time but every now and again the request comes back with a Invalid Session error:
Status Code: BadRequest
Microsoft.Graph.ServiceException: Code: InvalidSession
Message: The target session is invalid.
Inner error:
Code: invalidSessionReCreatable
Message: The session specified in the request does not exist or is invalid due to a transient error.
Inner error:
Code: InvalidOrTimedOutSession
Message: We noticed that you haven't been interacting with this workbook, so we paused your session.
It seems like the session gets paused before the API gets to finish the call (which happens in less than a second).
Startup Code to configure AD and Graph Services:
services.AddMicrosoftIdentityWebApiAuthentication(Configuration, "AzureAd")
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph(Configuration.GetSection("GraphBeta"))
.AddInMemoryTokenCaches();
Creating the session:
WorkbookSessionInfo res = await _graphServiceClient.Groups[_groupId].Drive.Items[productStructureCalculator.CalculatorId].Workbook
.CreateSession(persistChanges)
.Request()
.Header("Prefer", "respond-async")
.PostAsync();
Doing a batch request:
var batchRequestInfo = CreateBatchRequestAsync(sessionId, structure, productStructureCalculator, productMatrixInputs);
var resultRequest = _graphServiceClient.Groups[_groupId].Drive.Items[productStructureCalculator.CalculatorId].Workbook.Worksheets[productStructureCalculator.SheetName]
.Range("D6:J6")
.Request()
.Header("workbook-session-id", "{" + sessionId + "}")
.Header("Prefer", "return=minimal")
.GetHttpRequestMessage();
resultRequest.Method = HttpMethod.Get;
var resultRequestID = Guid.NewGuid().ToString();
var batchResultRequestStep = new BatchRequestStep(
resultRequestID,
resultRequest,
new List<string> { batchRequestInfo.LastEventRequestId }
);
batchRequestInfo.BatchRequestContent.AddBatchRequestStep(batchResultRequestStep);
var returnedResponse = await _graphServiceClient.Batch.Request().PostAsync(batchRequestInfo.BatchRequestContent);
As per | Microsoft Docs ,
InvalidSessionReCreatable :The session specified in the request does not exist or is invalid due to a transient error.
Error Handling : The Microsoft Graph client can try to recreate a session and resume the work. Further access to the session specified in the failed request is not expected.
Please Check if the below work arounds help in your case:
1)
use the CreateSession method > workbook: createSession | Microsoft Docs to get the workbook info, and set the persistChanges setting.
=>(Set var persistChanges = true; )
Code:
var persistChanges = true;
try
{
WorkbookSessionInfo res = await _graphServiceClient.Groups[_groupId].Drive.Items[productStructureCalculator.CalculatorId].Workbook
.CreateSession(persistChanges)
.Request()
.Header("Prefer", "respond-async")
.PostAsync();
var result = res;
return result;
}
catch (Exception ex)
{
Console.WriteLine($"Error getting items: {ex.Message}");
return null;
}
Reference-SO
Or
2)
You can set the Timeout to a higher value .Lets say one hour:
graphServiceClient.HttpProvider.OverallTimeout = TimeSpan.FromHours(1);
Or
3)
Add session in start up class
Ex:
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
At least chance, take a look at activityBasedTimeoutPolicy which Represents a policy that can control the idle timeout for web sessions for applications that support activity-based timeout functionality.

UseExceptionHandler is not working with validation errors

In Asp.Net Core 5 I am using UseExceptionHandler to handle exceptions globally and it works fine unless I send an invalid object. For example I send an object with null value for the required property "Name" I receive the following error in client but the Run function does not hit in debugger.
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1" ,"title":"One
or more validation errors
occurred.","status":400,"traceId":"00-2428f0fb9c415843bca2aaef08eda6f6-11ea476efb792849-00","errors":{"Name":["The
field Name is required"]}}
(as the first middleware in the pipeline :)
app.UseExceptionHandler(a => a.Run(async context =>
{
//this does not being exceuted for validation errors
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
var exception = exceptionHandlerPathFeature.Error;
var exceptionManager = a.ApplicationServices.GetRequiredService<IExceptionManager>();
await context.Response.WriteAsJsonAsync(exceptionManager.Manage(exception, context));
}));
This may help you.
services.AddControllers().ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context =>
{
var errors = context.ModelState.Keys
.SelectMany(key => context.ModelState[key].Errors.Select(x => $"{key}: {x.ErrorMessage}"))
.ToArray();
var apiError = new CustomErrorClass()
{
Message = "Validation Error",
Errors = errors
};
var result = new ObjectResult(apiError);
result.ContentTypes.Add(MediaTypeNames.Application.Json);
return result;
};
});

Azure CosmosDB: Getting Error during bulk deletion

I am deleting bulk items and getting the below error on my local machine. I am using CosmosDB SDK 3.0.
An error occured trying to Initiailize CosmosBb. One or more errors
occurred. (Response status code does not indicate success:
ServiceUnavailable (503); Substatus: 0; ActivityId:
befe13e5-172f-4930-953f-e24bf9b0a14a; Reason: (Service is currently
unavailable. ActivityId: befe13e5-172f-4930-953f-e24bf9b0a14a,
RequestStartTime: 2020-08-06T07:04:58.1807989Z, RequestEndTime:
2020-08-06T07:05:28.2986219Z, Number of regions attempted: 1
ResponseTime: 2020-08-06T07:04:59.1993537Z, StoreResult:
StorePhysicalAddress:
rntbd://127.0.0.1:10253/apps/DocDbApp/services/DocDbServer18/partitions/a4cb495e-38c8-11e6-8106-8cdcd42c33be/replicas/1p/,
LSN: -1, GlobalCommittedLsn: -1, PartitionKeyRangeId: , IsValid:
False, StatusCode: 410, SubStatusCode: 0, RequestCharge: 0, ItemLSN:
-1, SessionToken: , UsingLocalLSN: True, TransportException: A client transport error occurred: Failed to connect to the remote endpoint.
(Time: 2020-08-06T07:04:59.1993537Z, activity ID:
befe13e5-172f-4930-953f-e24bf9b0a14a, error code: ConnectFailed
[0x0005], base error: socket error
Here is my delete method.
private async Task DeleteAllExistingSubscriptions(string userUUId)
{
var subscriptions = await _repository
.GetItemsAsync(x => x.DistributionUserIds.Contains(userUUId), o => o.PayerNumber);
if (subscriptions.Any())
{
List<Task> bulkOperations = new List<Task>();
foreach (var subscription in subscriptions)
{
bulkOperations.Add(_repository
.DeleteItemAsync(subscription.Id.ToString(), subscription.PayerNumber));
}
await Task.WhenAll(bulkOperations);
}
}
Cosmos Client:
private static void RegisterCosmosClient(IServiceCollection serviceCollection, IConfiguration configuration)
{
string cosmosDbEndpoint = configuration["CosmoDbEndpoint"];
Ensure.ConditionIsMet(cosmosDbEndpoint.IsNotNullOrEmpty(),
() => new InvalidOperationException("Unable to locate configured CosmosDB endpoint"));
var cosmosDbAuthKey = configuration["CosmoDbAuthkey"];
Ensure.ConditionIsMet(cosmosDbAuthKey.IsNotNullOrEmpty(),
() => new InvalidOperationException("Unable to locate configured CosmosDB auth key"));
serviceCollection.AddSingleton(s => new CosmosClient(cosmosDbEndpoint, cosmosDbAuthKey,
new CosmosClientOptions { AllowBulkExecution = true }));
}
The error is a client-side connectivity issue:
TransportException: A client transport error occurred: Failed to connect to the remote endpoint.
(Time: 2020-08-06T07:04:59.1993537Z,
activity ID: befe13e5-172f-4930-953f-e24bf9b0a14a,
error code: ConnectFailed [0x0005], base error: socket error
Common cause is resource starvation:
The volume of data that you are sending through needs CPU to be processed, if the environment's CPU is not enough to process the volume of data, you will see CPU spikes (>70 - 100%) which will cause TCP connections to be delayed and would cause these issues. In this case, either reduce the volume of data or increase the available resources.
If running on the cloud, it could also be a [SNAT limitation](https://learn.microsoft.com/azure/cosmos-db/troubleshoot-dot-net-sdk#snat] or connection limit on the instance (depending on the service, connection limits vary).
Reference: https://learn.microsoft.com/azure/cosmos-db/troubleshoot-dot-net-sdk-request-timeout#troubleshooting-steps

How to fix "An unhandled exception occurred while processing the request."?

As we're developing a webapp which we want to authorize against a Shibboleth IDP we're getting the following error, after what seems like a successfull login at the IDPP:
An unhandled exception occurred while processing the request.
UnexpectedInResponseToException: Received message contains unexpected InResponseTo "idd95739d3bc9e44efa1154b3e62a2e121". No cookie preserving state from the request was found so the message was not expected to have an InResponseTo attribute. This error typically occurs if the cookie set when doing SP-initiated sign on have been lost.
Sustainsys.Saml2.Saml2P.Saml2Response.ReadAndValidateInResponseTo(XmlElement xml, Saml2Id expectedInResponseTo, IOptions options) in Saml2Response.cs, line 153
Our startup.cs looks like this:
services.AddAuthentication()
.AddSaml2(options =>
{
options.SPOptions.EntityId = new EntityId("https://adress.to.the.SP.net");
options.SPOptions.Compatibility.UnpackEntitiesDescriptorInIdentityProviderMetadata = true;
options.SPOptions.ReturnUrl = new Uri(#"https://adress.to.a.site.of.our.site.net.net/Saml/Session");
options.SPOptions.ServiceCertificates.Add(new X509Certificate2(#"wwwroot/mycert.pfx")); // "Sustainsys.Saml2.Tests.pfx"
options.IdentityProviders.Add(
new IdentityProvider(
new EntityId("adress.to.the.IDP.net"), options.SPOptions)
{
LoadMetadata = true,
MetadataLocation = ("https://adress.to.the.MetadataLocation.xml")
});
IdentityProvider idp;
var x = options.IdentityProviders.TryGetValue(new EntityId(Prov.Idp), out idp);
X509Certificate2 ssoTest = new X509Certificate2(#"wwwroot/sso-test.pem");
idp.SigningKeys.AddConfiguredKey(ssoTest);
});^^^
This is what I see when I check the browser's dev-tools cokkies option:
Gett 200 BIGipServer~idm~ipv4-shib-test: ! experimentation_subject_id: JSESSIONID: shib_idp_session:
Post 500 .AspNetCore.Antiforgery.w5W7x28NAIs: ARRAffinity: Saml2.t8NpWx0u6S6zBFc97nzgN_IL:
Gett 200 .AspNetCore.Antiforgery.w5W7x28NAIs: ARRAffinity: Saml2.5AYF3RXdiYbN3iolD0HCRu9P:

Categories