Comments (4)
No, just create one SqlConnection in an action.
from cap.
@yuleyule66 But When I use one SqlConnection , I got a "NullReferenceException", code like below:
[Route("publish")]
public async Task<IActionResult> PublishMessage()
{
const string cstr = "Data Source=192.168.0.250;Initial Catalog=CapDemo;User ID=sa;Password=123123;";
var sql1 = new System.Data.SqlClient.SqlConnection(cstr);
await _publisher.PublishAsync("CapDemo", new Person { Name = "老张", Age = 30 }, sql1);
// var sql2 = new System.Data.SqlClient.SqlConnection(cstr);
await _publisher.PublishAsync("CapDemo.Service", new Person { Name = "老李", Age = 40 }, sql1);
return Ok();
}
Then I got:
NullReferenceException: Object reference not set to an instance of an object.
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource<DbConnectionInternal> retry)
System.Data.SqlClient.SqlConnection.Open()
DotNetCore.CAP.Abstractions.CapPublisherBase.PrepareConnectionForAdo(IDbConnection dbConnection, IDbTransaction dbTransaction) in CapPublisherBase.cs
-
private void PrepareConnectionForAdo(IDbConnection dbConnection, IDbTransaction dbTransaction)
{
DbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection));
if (DbConnection.State != ConnectionState.Open)
{
IsCapOpenedConn = true;
DbConnection.Open();
}
DbTransaction = dbTransaction;
if (DbTransaction == null)
{
IsCapOpenedTrans = true;
DbTransaction = dbConnection.BeginTransaction(IsolationLevel.ReadCommitted);
DotNetCore.CAP.Abstractions.CapPublisherBase.PublishAsync<T>(string name, T contentObj, IDbConnection dbConnection, string callbackName, IDbTransaction dbTransaction) in CapPublisherBase.cs
-
}
public Task PublishAsync<T>(string name, T contentObj, IDbConnection dbConnection,
string callbackName = null, IDbTransaction dbTransaction = null)
{
CheckIsAdoNet(name);
PrepareConnectionForAdo(dbConnection, dbTransaction);
var content = Serialize(contentObj, callbackName);
return PublishWithTransAsync(name, content);
}
DemoA.Controllers.ValuesController+<PublishMessage>d__2.MoveNext() in ValuesController.cs
-
const string cstr = "Data Source=192.168.0.250;Initial Catalog=CapDemo;User ID=sa;Password=123123;";
var sql1 = new System.Data.SqlClient.SqlConnection(cstr);
await _publisher.PublishAsync("CapDemo", new Person { Name = "老张", Age = 30 }, sql1);
// var sql2 = new System.Data.SqlClient.SqlConnection(cstr);
await _publisher.PublishAsync("CapDemo.Service", new Person { Name = "老李", Age = 40 }, sql1);
return Ok();
}
[NonAction]
[CapSubscribe("CapDemo")]
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeActionMethodAsync>d__12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeNextActionFilterAsync>d__10.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeInnerFilterAsync>d__14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResourceFilter>d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeFilterPipelineAsync>d__17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeAsync>d__15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+<Invoke>d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__7.MoveNext()
from cap.
Upgrading CAP version to 2.1.0-preview-26231671
, I modified part of the ICapPublish
interface about publishing message with ADO.NET , and here is a sample code :
[Route("~/publishWithTransactionUsingAdonet")]
public async Task<IActionResult> PublishMessageWithTransactionUsingAdonet([FromServices]ICapPublisher publisher)
{
var connectionString = "";
using (var sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
using (var sqlTransaction = sqlConnection.BeginTransaction())
{
// 此处填写你的业务代码,通常情况下,你可以将业务代码使用一个委托传递进来进行封装该区域代码。
publisher.Publish("xxx.services.account.check", new Person { Name = "Foo", Age = 11 }, sqlTransaction);
sqlTransaction.Commit();
}
}
return Ok();
}
from cap.
En, Thank you for your time, It's very helpful!
from cap.
Related Issues (20)
- How to implement transaction Pub/Sub using CAP & FreeSql using demo? HOT 2
- CAP has not been started! HOT 4
- Question about parallel execution HOT 6
- Error in Nats transport HOT 4
- 能不能加个配置,可以单独启用消费功能或发送功能 ? HOT 2
- How to Introduce Multiple Database Providers? HOT 1
- .NET Core 8 使用RabbitMQ 报No ip address could be resolved for【hostName】的问题 HOT 1
- Optimizing RabbitMQ Performance and Managing Multiple Instances in CAP HOT 2
- Feature Request: Per-Queue Configuration for BasicQos and ConsumerThreadCount HOT 3
- 消息持久化支持达梦数据库吗 HOT 1
- Is the message data structure compatible with older versions of 3.0? HOT 1
- Add new option for parallel publish send. HOT 2
- Add option for controlling reponse headers from OnSubscribeExecuted HOT 2
- NATS Connection Error after Restarting nats-server: Unable to Publish Messages HOT 5
- SQL Server replication readpast error! "You can only specify the READPAST lock in the READ COMMITTED or REPEATABLE READ isolation levels" HOT 3
- 一个进程内可以创建多个CAP吗? HOT 1
- NATS feature: add the possibility to disable dynamic consumer subject/topic creation through NatsCapOtpions HOT 2
- 一个程序进程会操作多个不同的业务数据库,参考#998创建多个发布者进行对应处理,消费者应该如何处理消息? HOT 1
- Feature Request: Add New Column in table Cap.published for relation to table every single data publish HOT 2
- Callback on IIS Idle state HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cap.