{"action":"created","issue":{"url":"https://api.github.com/repos/Particular/NServiceBus/issues/3417","repository_url":"https://api.github.com/repos/Particular/NServiceBus","labels_url":"https://api.github.com/repos/Particular/NServiceBus/issues/3417/labels{/name}","comments_url":"https://api.github.com/repos/Particular/NServiceBus/issues/3417/comments","events_url":"https://api.github.com/repos/Particular/NServiceBus/issues/3417/events","html_url":"https://github.com/Particular/NServiceBus/issues/3417","id":131393734,"number":3417,"title":"Container registrations of message session and message context","user":{"login":"danielmarbach","id":174258,"avatar_url":"https://avatars.githubusercontent.com/u/174258?v=3","gravatar_id":"","url":"https://api.github.com/users/danielmarbach","html_url":"https://github.com/danielmarbach","followers_url":"https://api.github.com/users/danielmarbach/followers","following_url":"https://api.github.com/users/danielmarbach/following{/other_user}","gists_url":"https://api.github.com/users/danielmarbach/gists{/gist_id}","starred_url":"https://api.github.com/users/danielmarbach/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danielmarbach/subscriptions","organizations_url":"https://api.github.com/users/danielmarbach/orgs","repos_url":"https://api.github.com/users/danielmarbach/repos","events_url":"https://api.github.com/users/danielmarbach/events{/privacy}","received_events_url":"https://api.github.com/users/danielmarbach/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-02-04T16:04:59Z","updated_at":"2016-02-04T16:50:51Z","closed_at":null,"body":"### Situation in v5\r\n\r\n`IBus` is registered over our container abstraction in the user container. Automagically the user gets the right instance of `IBus` depending where his instances are resolved. Let me give you an example\r\n\r\n```\r\nclass MyHandler : IHandleMessages<MyMessage>\r\n{\r\n   public MyHandler(MyDependency dependency) { }\r\n\r\n   public IBus Bus { get; set; }\r\n\r\n   public void Handle(MyMessage message)\r\n   {\r\n   \r\n   }\r\n}\r\n\r\nclass MyDependency\r\n{\r\n    public MyDependency(IBus bus)\r\n}\r\n```\r\n\r\nIn the above snippets the `IBus` implementation gets automatically resolved from the child container which will resolve the contextual bus. So everything resolved in a scope of a handler will get the contextual bus.\r\n\r\n\r\nif `MyDependency` is resolved outside the scope of a handler. For example in an implemenation of an `IWantToRunWhenTheBusStartsAndStops` the `IBus` instance resolved will be the singleton non-contextual bus implementation.\r\n\r\n## Situation in v6\r\n\r\nIf a user blindly applies the following refactoring: `IBus` --> `IMessageSession` like\r\n\r\n```\r\nclass MyDependency\r\n{\r\n    public MyDependency(IMessageSession session)\r\n}\r\n```\r\n\r\nthen they are in trouble. This code compiles and runs. Possibly for days, weeks until exceptions occur and a handle message is rollbacked. Why? Because the message session injected into `MyDependency` is no context of the incoming pipeline. So all operations done on `IMessageSession` will not rollback when the surrounding handler rolls back. The users would need to redesign their code to something like this:\r\n\r\n```\r\nclass MyDependency\r\n{\r\n    public Task DoWork(IMessageHandlerContext context) {}\r\n}\r\n```\r\n\r\nSo this is a potential message loss scenario that we need to address with a proper design. Here are a few proposals\r\n\r\n## Solution approach\r\n1. Somehow check on the message session for the availability of `TransportReceiveContext` or `IncomingMessage` and throw a runtime exception if found.\r\n1. Implement proper scoping of the message session outside the handler and therefore make it almost impossible to directly register the session on the container like describe in https://github.com/Particular/PlatformDevelopment/issues/591 \r\n\r\nIn my opinion we should try to approach https://github.com/Particular/PlatformDevelopment/issues/591 and come up with a design which doesn't require us to throw during runtime.\r\n\r\n@Particular/nservicebus-maintainers @Particular/tf-handlercontext Thoughts?"},"comment":{"url":"https://api.github.com/repos/Particular/NServiceBus/issues/comments/179941709","html_url":"https://github.com/Particular/NServiceBus/issues/3417#issuecomment-179941709","issue_url":"https://api.github.com/repos/Particular/NServiceBus/issues/3417","id":179941709,"user":{"login":"timbussmann","id":3524870,"avatar_url":"https://avatars.githubusercontent.com/u/3524870?v=3","gravatar_id":"","url":"https://api.github.com/users/timbussmann","html_url":"https://github.com/timbussmann","followers_url":"https://api.github.com/users/timbussmann/followers","following_url":"https://api.github.com/users/timbussmann/following{/other_user}","gists_url":"https://api.github.com/users/timbussmann/gists{/gist_id}","starred_url":"https://api.github.com/users/timbussmann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timbussmann/subscriptions","organizations_url":"https://api.github.com/users/timbussmann/orgs","repos_url":"https://api.github.com/users/timbussmann/repos","events_url":"https://api.github.com/users/timbussmann/events{/privacy}","received_events_url":"https://api.github.com/users/timbussmann/received_events","type":"User","site_admin":false},"created_at":"2016-02-04T16:50:51Z","updated_at":"2016-02-04T16:50:51Z","body":"should we also discuss that we currently do not automatically register `IMessageSession`/`IEndpointInstance` and users need to do register it manually with the container?\r\n\r\n> Somehow check on the message session for the availability of TransportReceiveContext or IncomingMessage and throw a runtime exception if found.\r\n\r\nI don't think this works. Sends from `IMessageSession` always use the `RootContext`. I don't see how you want that detection to work.\r\n\r\n> Implement proper scoping of the message session outside the handler and therefore make it almost impossible to directly register the session on the container like describe in Particular/PlatformDevelopment#591\r\n\r\nTo be clear: I love the \"session scoping\" idea and completely support it. But I start to think this would be better suited for v7."}}