HangfireIO / HangfireIO/Hangfire
Issue Creating Client With Existing Connection
- Dominant language
- C#
- Stars
- 10.1k
- Forks
- 1.8k
- Avg merge
- 1h 19m
- Merged PRs (30d)
- 1
Description
So I'm trying to integrate Hangfire into my application. I need to be able to manage the transaction that Hangfire uses to enqueue jobs to the database if a failure occurs. If one does occur, I can rollback the transaction so the job is removed from the database.
Essentially what I'm doing is this:
var connectionString = "";
var connection = new SqlConnection(connectionString);
connection.Open();
var transaction = Connection.BeginTransaction();
// Later down the line:
// At this point in time the transaction has been created and other services may have used the
// connection and transaction to write to the database.
// Create the Hangfire client using the previously created connection (which has an open transaction)
var storage = new SqlServerStorage((DbConnection)transaction.Connection); -- Error Line
var client = new BackgroundJobClient(storage);
client.Enqueue(() => Console.WriteLine());
However I'm getting this error when trying to create the SqlServerStorage object with the existing connection:
**System.InvalidOperationException: 'ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.'**
Any ideas? Am I going about this wrong?
Contributor guide
Research direction
Start by examining the SqlServerStorage construction in the failing example, including the existing SqlConnection and pending transaction, then trace how BackgroundJobClient.Enqueue issues database commands. Reproduce the reported InvalidOperationException and determine what behavior or API change would be required for an existing transaction; no target file or test is named in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100