9
A
回答
15
这里是一个不仅说明了如何连接起来的文档存储,又如何设定,让你可以注入您的文档会话的示例的控制台程序:
using System.Threading.Tasks;
using Autofac;
using Raven.Client;
using Raven.Client.Document;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main()
{
var builder = new ContainerBuilder();
// Register the document store as single instance,
// initializing it on first use.
builder.Register(x =>
{
var store = new DocumentStore { Url = "http://localhost:8080" };
store.Initialize();
return store;
})
.As<IDocumentStore>()
.SingleInstance();
// Register the session, opening a new session per lifetime scope.
builder.Register(x => x.Resolve<IDocumentStore>().OpenSession())
.As<IDocumentSession>()
.InstancePerLifetimeScope()
.OnRelease(x =>
{
// When the scope is released, save changes
// before disposing the session.
x.SaveChanges();
x.Dispose();
});
// Register other services as you see fit
builder.RegisterType<OrderService>().As<IOrderService>();
var container = builder.Build();
// Simulate some activity. 5 users are placing orders simultaneously.
Parallel.For(0, 5, i =>
{
// Each user gets their own scope. In the real world this would be
// a new inbound call, such as a web request, and you would let an
// autofac plugin create the scope rather than creating it manually.
using (var scope = container.BeginLifetimeScope())
{
// Let's do it. Again, in the real world you would just inject
// your service to something already wired up, like an MVC
// controller. Here, we will resolve the service manually.
var orderService = scope.Resolve<IOrderService>();
orderService.PlaceOrder();
}
});
}
}
// Define the order service
public interface IOrderService
{
void PlaceOrder();
}
public class OrderService : IOrderService
{
private readonly IDocumentSession _session;
// Note how the session is being constructor injected
public OrderService(IDocumentSession session)
{
_session = session;
}
public void PlaceOrder()
{
_session.Store(new Order { Description = "Stuff", Total = 100.00m });
// we don't have to call .SaveChanges() here because we are doing it
// globally for the lifetime scope of the session.
}
}
// Just a sample of something to save into raven.
public class Order
{
public string Id { get; set; }
public string Description { get; set; }
public decimal Total { get; set; }
}
}
注意DocumentStore是单实例,但DocumentSession是每个生命周期范围的实例。对于此示例,我手动创建的寿命范围和做并行,如何模拟5个不同的用户可能会在同一时间下订单。他们将分别得到他们自己的会议。
的SaveChanges把在OnRelease事件是可选的,但将节省您不必把它在每一个服务。
在现实世界中,这可能是一个Web应用程序或服务总线的应用程序,在这种情况下,您的会话应该作用域到单web请求或消息的分别寿命。
如果您使用的是ASP.Net WebApi,则应该关闭NuGet的Autofac.WebApi包并使用它们的.InstancePerApiRequest()方法,该方法会自动创建适当的生存期范围。
+0
谢谢!奇迹般有效! – trailmax
相关问题
- 1. Autofac注册DLL使用Assembly.Load
- 2. 注册和使用AutoFac
- 3. Autofac通用注册
- 4. 用Autofac注册HttpContext.User
- 5. 使用Autofac注入注册类
- 6. Autofac - DelegatingHandler(HttpMessageHandler)注册
- 7. Autofac - 注册插件
- 8. Autofac注册问题
- 9. Autofac&MassTransit - 注册用户ObjectDisposedException
- 10. 通用注册与Autofac?
- 11. 推迟使用Autofac注册财产
- 12. 注册容器本身使用Autofac
- 13. 使用Autofac注册Generic类型
- 14. AutoFac和Log4Net - 注册和使用
- 15. 我正确使用AutoFac注册ObjectContext吗?
- 16. 使用Autofac注册ISecureDataFormat <AuthenticationTicket>
- 17. 在Autofac中注册事件
- 18. 如何设置Autofac注册
- 19. Autofac注册多个容器
- 20. Autofac - 注册(部分)Open Generic。
- 21. 与Autofac嵌套注册
- 22. Autofac命名注册失败
- 23. Autofac注册大括号
- 24. autofac中的指定注册
- 25. Autofac - 注册多个装饰
- 26. Autofac自我注册TypedNamedAndKeyedServices
- 27. Autofac房产注射型后注册
- 28. 重写AutoFac注册记忆应用
- 29. Autofac注册AuthorizationFilter调用两次
- 30. Autofac注册通用代理工厂
这里有一个相关的问题:http://stackoverflow.com/questions/10940988/how-to-configure-simple-injector-ioc-to-use-ravendb。它谈到简单的喷油器,但它对于Autofac来说几乎是一样的。 – Steven