How to renew subscriptions after IISRESET? - nservicebus

This is my scenario:
Web application with a self-hosted bus (publisher)
Windows service with NServiceBus.Host.exe (subscriber)
Imagine that trigger an IISRESET (never mind on the why I have to do that) that makes the bus re-initialize and lose the subscriptions that already had. I'm using MsmqSubscriptionStorage and the publisher (web app) implements the IAuthorizeSubscriptions interface.
How can I renew the subscriptions and also call the AuthorizeSubscribe()? Is that possible? What's your suggestion to this scenario?

If NServiceBus is properly using MsmqSubscriptionStorage, then after iisreset, the bus should initialize and load the existing subscriptions from the configured storage queue. This queue name can either be set by configuration or, if omitted, should default to a name based on an assembly plus "_storage". However, having never used this from a web application, I don't know what that would be in that scenario, since there is no class that implements IConfigureThisEndpoint.
That said, trying to publish an event from a web application is a very icky business, and it's generally advised that you avoid just that.

Related

Simulating a transient error for Service Bus

I'm writing an application which will use the Azure Service Bus. For local development I'm using Windows Server Service Bus to provide the same services (the code to use either is identical).
I want to write the application to be tolerant of transient errors when sending or receiving messages. To that end, I want to be able to test the fault-handling code can deal with the local Service Bus instance suddenly being unavailable during execution of various operations.
Ideally, I'd want to write some automated integration tests around these scenarios, but I appreciate that may not be practically achieved.
What can I do to simulate transient errors on my local Service Bus?
One easy thing would be to call the stop-sbservice (affects one node) or stop-sbfarm (affects the entire farm) cmdlets. This would let you simulate a servicebus outage locally. You can then call start-sbservice or start-sbfarm to bring the service back and validate that your code recovers properly. This approach also has the added benefit that you control when the service returns (compare to just crashing the process). This page has information on the available cmdlets.
If that's not enough, another approach that I've used in the past is to shut down the network interface, or, if the server is in another machine, put up a firewall on the ports used to communicate to service bus.

How to connect to ActiveMQ on startup with WCF and IIS

What is the best way to combine a single instance WCF service that uses ActiveMQ and runs within IIS/AppFabric?
Our Services need to support both HTTP transports and ActiveMQ (listening and sending messages). We've elected not to use MSMQ, and will use Spring.Net.NMS. The fundamental issue I have now is that ActiveMQ needs to connect to the queue(s) at startup and remain connected, but WAS is getting in the way with it's message-activation feature. If the service is not activated until a message arrives (HTTP/MSMQ, etc) then there is no trigger to have the connection to AMQ occur.
I know I can disable the recycling behavior, and I know I can do self-hosting with a Windows Service. But I want to take advantage of the monitoring and other features in AppFabric. I've already been down the route with IServiceBehavior and will use that for other nice things. But that interface is not called until a (non-AMQ) message arrives. So it won't work for this. What I was hoping for was something along the line of how ServletContextListeners work in Java, where you get both the start up and shutdown events. But it seems no such thing exists in WAS... it is driven only by messages arriving.
I've scoured every inch of web info for 3 days and the only thing I came across was to use a static class construction (C#) trick as the trigger. That's a hack, but i can live with it. It still leaves the issue of cleanly shutting down, which I can figure out later.
Anyone have a solid solution to this?
The direct WCF support for ActiveMQ that Ladislav mentions is still being supported. There just hasn't been an official release for the module in a while. However, you can still get the latest version of it from the 1.5.x branch or trunk and compile it yourself.
1.5.x branch for use with Apache.NMS 1.5.0:
https://svn.apache.org/repos/asf/activemq/activemq-dotnet/Apache.NMS.WCF/branches/1.5.x/
Check out instructions:
http://activemq.apache.org/nms/source.html
There was direct WCF support for ActiveMQ but I guess it is not developed anymore. Your problem actually is the IIS / WAS (provides hosting for non-http protocols) hosting architecture. Services in WAS are always activated when message arrives - there is no global startup. The reason for this is that WAS hosting expects that there is separate process (windows service) running the listener all the time and this process has adapter which calls WAS and uses message level activation. I guess you don't have such process for ActiveMQ and because of that you will have trouble to use ActiveMQ endpoint hosted in WAS. Developing such listener can be challenging task (example for UDP).
Creating custom listener can be probably avoided by using IIS 7.5 / AppFabric auto start feature. There is also not very well documented way to run the code when the application starts.

Hosting an NServiceBus subscriber in the same application as the producer

Is it possible to use NServiceBus to publish and consume messages in the same application, specifically a web application?
In the future we will almost certainly need to maintain a separate long running service to process messages generated by this application, and this is why we are hoping to use NServiceBus from the start, but right now it would be nice to just start up the consumer and the publisher when the web application starts. This will make testing and deployment far easier for us.
I presume I will need to reference the NServiceBus.Host.exe and start up the process in the global.asax, but need help on what exactly I need to call to do this.
This is not a mode of deployment that is supported out of the box. While you could make this work by manually creating an additional appdomain for the second NServiceBus endpoint, you'd also likely need to give it a custom configuration source, and of course its own queue.
All in all, I'd recommend keeping it as a separate process, even if it is on the same box. That being said, you can create a second web app to host it rather than using the generic host if you don't want to manage windows services in addition to web apps.
Hope that helps.

Event Dispatcher for WCF call-backs

I have a server that needs to keep a small number of clients in sync. Whenever there is a change of state at the server, all the connected clients must be informed.
I am planning to use a “callback
contract”,
I can get hold of the
callback reference for each client on
the server by using
GetCallbackChanel().
I then need
to manage all these client channel
reference and call all of them when
needed.
So far so good however:
I don’t wish to block the server, so calls to the clients must be none blocking
Errors calling the client must be logged and coped with
Is there a standard WCF component to do this?
No, there is not a standard WCF component for this, at least through .NET 3.5. I can't speak to what may be available in .NET 4.0.
That said, there is a pretty straightforward way to do this. Juval Lowy, author of Programming WCF Services, describes how to do this using his WCF-based Publish-Subscribe Framework.
Basically, the idea is to create a separate WCF event service that resides in the same hosting application as your server (e.g., Windows service, IIS). When the state of your server changes, you publish the state change to the event service. The clients that need to be kept in sync subscribe to this same event via the event service. In effect, the event service becomes a broker for your server to notify clients of whatever events your server publishes.
The article I listed above has a code download, but you can also get the Publish-Subscribe Framework and a working example for free from his website, IDesign.net. Here is the link to the download. You may need to scroll your browser up just a little bit to see it as I believe their internal hyperlink is wrong.

Server architecture question. (WCF+NServiceBus)

First of all i will describe current state:
Server consists of several WCF services, hosted in one or several win services on diffirent machines.
Service responsible for recieving data from diffirent devices. Communication with devices is implemented using sockets. Service instance mode - singleton.
Data broker service - responsible for persisting data and sharing in by request. Instance mode - singleton.
Configuration service - responsible for changing configuration database and working with administration console(WPF app, like SSMS). Handles connections from console, subscriber management, etc. Instance mode - singleton.
Client access service - quite the same as above thith management of clients but also notifyes clients of new data, and acts like facade to service bus. And singleton again.
Identity management service - Checks permissions and returns result. Singleton.
All of those services are connected with NServiceBus and i realy like how it works at this moment.
But:
Too many singletons. Mainly because to use servicebus i must have single instance of it afaik. I dunno maybe i can use nservice bus in session mode, but dont know how to handle issue that all of those services will use one queue.
And what if i will have 300+ clients? singleton can become unresponsive..
And i wanted to ask for some critics about all of this and maybe some one could suggest something.
Thanks in advance.
Alexey
Alexey,
While you should only have one instance of the bus per process, you can put that instance in a globally accessible place (as shown in the AsyncPages sample), and use that from non-singleton objects like web pages and WCF services.
Also, it is probably not appropriate to have all your services using one queue. Without better understanding your situation, I'd give the default recommendation of one queue for each of the services you identified.
Hope that helps.