Register instance into builder from NServiceBus behaviour - unity-container

I have a web api which sends commands to endpoints. I've created message a mutator which adds a custom header with a serialized IAuthenticationInformation instance.
What I need to do in the receiving endpoints is intercept this header (presumably in a Behavior), deserialize it into an IAuthenticationInformation and register it into the child container which will build the handlers to return it when requested.
I've got everything to the point of registering the instance into the container. The problem is that the context.Builder doesn't provide any Register operation.
How can I achieve this?
Notes:
I'm using v6 (I found a way of doing this in v5 using the PipelineExecutor, but this doesn't seem to work any more).
I'm using Unity, but I'd prefer a container independent solution
This issue seems very common to me, as I need this for a couple of other scenarios (i.e registering a unit of work with a given db connection), so I don't understand why is so difficult to find a solution. Should I be looking at another approach?

What you are doing is a variation of the Appending username using headers sample.
Add the UnityContainer as a constructor dependency in the IncomingMessageBehavior.
This will give you a child container to register your instance with.

Related

Symfony service factory with many children, all with different dependencies

I am working on a very large game project in Symfony, and keep running into variations of the same problem.
I have a service that I need to separate into multiple "sub-services" because there is too much code to be contained within one class. For instance, a custom JSON serializer handler that needs a separate handler service for each method of serialization.
I'm having trouble working out the best practice for passing dependencies between "families of services." I would preferably like to keep all the definitions within services.yml just so things don't break in the future, but am assuming that may not be achievable.
Here's a better example - I have an "ActionQueueService" which takes a rather lengthy queue of actions from the user. What I would like to do is create a separate class to handle each type of action, so -
ActionQueueService // processes action queue JSON and delegates to sub-services
AbstractAction
PurchaseAction extends AbstractAction
SellAction extends AbstractAction
HarvestAction extends AbstractAction
Now if these three "actions" are defined as services they can have their own dependencies. However, they all have to be injected into the ActionQueueService - what happens if there are 38 of them (which there will be one day).
The next logical step for me was to create ActionFactory. Now I'm only passing in one dependency to ActionQueueService, and it can invoke any action service simply by calling -
$this->actionFactory->get('Harvest');
The problem I have is that each child has it's own separate list of dependencies. A Purchase or Sell action would need the Character service and the ShopStock service, a HarvestAction would need the HarvestService. Because I've decided to use a Factory method, I have to instantiate the sub-service within the factory class. I don't want to pass every dependency into the Factory only to have that either a) inject every dependency into every child or b) handle some crazy child constructor logic.
One solution would be to pass the service container into the factory and come up with a naming convention that allows me to create the service on the fly. I've heard that this is fairly bad practice though. Maybe a wrapper around the service container that limits the amount of bad stuff that could be done with it.
If anybody has any ideas on how this could be solved with Symfony I'd appreciate it. There are similar answers out there but unfortunately I don't know any languages other than PHP/Symfony.

Selective Explicit Loading in WCF Data Service

I'm about to implement a web service for my database, perhaps using WCF Data Services. Some of the objects I need to make available have child objects that need to be present for the objects to be useful. But because of lazy loading in the Entity Framework, those child objects are not going to be automatically loaded.
I'm going to be calling this service using JSON, and I don't want to have to specify the $expand option in each call. And it's not clear to me where I would use the LoadProperty method (same link), since I'm just writing the InitializeService method and letting the framework do the rest.
Is there a way to configure it to explicitly load some child objects and not others?
WCF Data Services currently doesn't support auto-expand on the server. The client always has to ask for expansions.
You could implement some kind of a workaround around the WCF DS, by modifying the incoming request. So for example if the client sends request for ~/Products you could modify it before it gets to WCF DS and let it process ~/Products&$expand=Category and that way effectively achieve auto-expand. But for such a service to be robust, you would have to parse the query URL and only add the expand if there's not already one in there and so on.
The other way is if its always necessary for the child object to be present, can we make the child object complex types instead of entities, so that they always come along with the parent. Is there a strong reason for the child objects to be individual entities?
Hope this helps.
Thanks
Pratik

Using Web Services in the Flex Mate Framework

I am currently trying to use the "Invocation tags" of Mate to call my web services and delegate the WS-responses to my fault/result handlers.
I want to use the generated proxies, provided by the Flex Builder, and not the plain <WebService> or <WebServiceInvoker> tags.
I actually failed using several techniques:
<WebServiceInvoker> does not work with the generated proxies.
<AsyncMethodInvoker> needs some complicated successType that I could not get to work with the WS-calls. And defining the events seems redundant to me. I want it simple and easy to read, the code will be touched by other people than me!
<MethodInvoker> can't use instances, and it also can't handle the proxies' AsyncToken
<DelegateInvoker> Looked fine at first. It calls the service but doesn't fire valid result events (infinite busy cursor). Even though i can successfully bind to the XYZ_lastResult of the WS-proxies, and a WS-call results in getting valid data from the WS-backend, the <faultHandlers> and <resulthandlers> are not executed. There is some solution for the DelegateInvoker that changes code in the generated proxies, which i definately do not want to do!
So here is my question: Is there a simple(!) way of using default Flexbuilder generated proxies with the Mate Invocation tags?
It appears that your request is not that uncommon to Mate. Check out this couple of threads in their forum:
http://mate.asfusion.com/forums/topic.php?id=424
http://mate.asfusion.com/forums/topic.php?id=421
The solution is to modify some bits of the auto-generated code... which in a way ruins the whole point of using code generation.

How do I set BTS.Operation in a custom pipeline?

Up until now I've only been using orchestrations in my BizTalk application and it's been working fine so far. But now I want to convert some of the unnecessary orchestrations to pure message routing instead in order to get better performance.
I've got a WCF service with only one method and that works fine because I can set the BtsActionMapping to only that single method. But the second WCF service I've got has two methods and now BizTalk doesn't know how to route my message. I've read everywhere that you need to set BTS.Operation in a custom pipeline to get it to work. But I've searched all over the place for a tutorial or example on how to do this.
I've been trying to implement the IBaseComponent, IComponentUI, IComponent and IPersistPropertyBag interfaces to do this. Am I going in the right direction or I'm I way off? Can anyone point me to an example or better yet show me how to do this?
The easiest way to get started writing a custom pipeline component is to use the BizTalk Server Pipeline Component Wizard; it will generate all the boilerplate for you. I've also got several custom pipeline components you can use as an example, a few that are very close to what you need (i.e. a component that just sets a custom message property) can be seen here.
As for setting the property, all you need to do is call message.Context.Write/Promote and pass in the namespace and name of the property, in this case those would be "http://schemas.microsoft.com/BizTalk/2003/system-properties" and "Operation" respectively.

How to catch all Exceptions from a WebService?

I have a Web Reference to a remote Web Service, which is called in several different classes. I'd like to catch all exceptions coming from this Web Service only.
How can I do that (for example extending it's base, auto generated class) without interfering and modifying all upper level code?
You can't, and why do you want to? It's almost always a mistake to "catch all exceptions" or "handle all exceptions". What are you trying to accomplish?
I'd suggest you log it on the server, not the client.
What will you be doing when you hide the exception from the upper layers? Pretending it didn't happen?
It might make sense to wrap the SoapException in one of your own. Maybe. That's only because SoapException is so specific to web services. In this case, yes, you need to wrap every call in a try/catch. The best way to do this would be in a facade class, which could also have the common code for wrapping the SoapException or translating it into one of your own.
Still, unless you're ready to take control of the code generation, you'll wind up doing this by hand.

Resources