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