How does WCF take care of multiple client calls to the server? - multithreading

Want to get a clear understanding of how WCF works.
Lets say a WCF service has exposed a function A.
Now the client creates 5 threads, and in each one of them calls the function A with different parameters.
I assume this should happen - a new instance of function A would get created for every thread call to that function. Could some one confirm this.
I have written a POC which is not doing this, its giving inconsistent results.

This depends on your service configuration via the ServiceBehavior attribute on the class implementing your service contract:
[ServiceBehavior(
InstanceContextMode = InstanceContextMode.PerSession,
ConcurrencyMode = ConcurrencyMode.Multiple)]
With the parameter InstanceContextMode you tell WCF how you want to host your service:
Single: One instance of your service class will be created which receives all service calls
PerSession: For each connecting client a new instance will be created
PerCall (default): For each call of every client a new instance will be created
The next thing is synchronisation, when one host object receives parallel operation calls. You can control the behavior with the ConcurrencyMode parameter:
Single (Default): WCF serialises all operations, so your service instance is executing exactly one or no operation call at a time.
Reentrant: WCF delegates all operation calls to your service instance directly, but synchronises calls to another WCF service inside a service operation (rarely used i think).
Multiple: WCF delegates all operation calls to your service instances directly without synchronization. You have to worry about synchronisation yourself.

The default is for a new instance of your wcf service to be created per call, this is documented e.g. here

There is no such thing like a new instance of function. It'd be rather instance of class.
You can configure how your Service should behave by changing proper meta attribute.
You can make your service implementation instantiated per call. It can also work as singleton (one and only service instance for all calls).
Here you can find information on creating singleton WCF service
And here there is a lot more about WCF services

Related

In Service Fabric, Are reliable queues only available to the same service type?

I created a pair of services in service fabric, one goes and reads from the source database and if it finds any new items, adds to a reliable queue; the other one tries to dequeue from the reliable queue and creates in the other database where I need the records.
If both of this processes are in the same service, everything works, but if I separate this functionality in two different services, the second service queue is always empty, which tells me the queues are not the same.
Hence my question: is a reliable queue only available to instances of the same service type? Is there any way to make a reliable queue available to two or more service types? If I want to share the same queue across service types, do I have to use Service Bus instead?
I hope my question makes sense, I have been trying to find this in the documentation, but I do not see anything helpful there, maybe I am looking in the wrong place.
A reliable collection is indeed only available to one particular stateful service type. The whole idea behind it is that the data (reliable collection) lives where the code (service) lives.
If you want to access the queue from another service you could expose methods that manipulate the queue to do that on the service interface and have other services call this service. See this repo for some inspiration. Or use another messaging service like the Azure Service Bus or Azure Storage Queues.

Sharing Azure Service Fabric Reliable Queues Between Services

I'm diving into Service Fabric (from the Cloud Services world) and am hitting a few speed bumps with how ReliableQueues work.
Let's say I have 2 stateful services StatefulService1 and StatefulService2.
If I need to have StatefulService1 send a message in a queue that StatefulService2 will pick up and read am I able to use ReliableQueues or are ReliableQueues isolated within the service they are created in?
If that is the case then what is the purpose of having ReliableQueues? The usual pattern behind them is for another process to act on the messages. I understand why isolating a Dictionary to a service would make sense, but not a queue...
Is my best option to rely on a traditional approach to send this message such as a Storage Queue or does ServiceFabric offer a solution for passing message queues between services?
UPDATE
Just want to clarify that I did attempt to dequeue a message created in StatefulService1 from within StatefulService2 and it came up empty. Dequeing from within StatefulService1 worked fine as expected.
Reliable Collections are in memory data structures that are not intended for inter-service communications. If you would like to establish a communication channel between StatefulService1 and StatefulService2, you have the following options:
Use Communication Listeners. You can have custom listeners for the protocol of your choice, including HTTP, WCF or your custom protocol. You can read more about it in this section. For example, StatefulService2 can open up an HTTP endpoint that StatefulService1 can POST/GET to.
Use an external queuing system, like Servicebus, EventHub or Kafka, where StatefulService1 can post events to. StatefulService2 can be consumer service that consumes events from the queue and processes it.
Reliable Collections (queue and dictionary) are not intended for communication. With queues, it's a 2PC, so only one process can access it at any point in time. Note that when you use stateful services with partitions, to access the data both service instances have to be on the same partition. Different partitions cannot access the same data.
Relying on either traditional methods or implementing your own communication listener is the way to go. With the traditional way - keep in mind that you'll need to decide if you want to partition your queues just like your services are or not.
I don't see why a service can't host a reliable collection/queue, and other services can access it via one of three transports: Remoting, WCF and HTTP.
Obviously, the reliable service will have to expose the collection/queue via an API or implement an IService interface
https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-connect-and-communicate-with-services
You have to add a fault-handling retry pattern to your calling code, see https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication, in this case you don't need a queue to hold data for your in between service calls.

When to use ResolveService

I have a dashboard service that relies on other services to retrieve data. So in order to reuse existing services I'm calling ResolveService for each service I'm reusing.
My question is if it would be better for me to extract the logic from these services rather than resolving the service? In particular are there performance impacts if I were keep calling ResolveService?
It can be cleaner to extract the logic in shared dependencies, which allows for more finer-grained usage (i.e you call just what you need instead of the entire Service). But if you need to use the entire Service response than calling ResolveService<Service> is fine.
The performance impact is no different since it's essentially just resolving an a Service class from the IOC and executing it.

Notification to Cloud Service Instance when another instance goes down

I have a cloud service with multiple instances. I'm trying to find a way to notify instance A if instance B goes down. Is there a way to hook into the azure notifications to do this rather than have each instance poll the others?
Polling is simplest approach. Do be aware that it takes some time for Azure to detect if an instance goes down (depends on how outage occurs and how it is manifested). In fact, if your app crashes, Azure may not even detect an outage at all.
I'm involved with a product called CloudMonix # http://cloudmonix.com , it provides sophisticated monitoring/automation for Azure and has a set of features where-by it can call your APIs when it detects issues in an environment. Perhaps it could be used to detect an issue with your instance and post to an API method that you implement?
HTH

How to use a Service Locator in a WCF service

I'm using a service locator to hold concrete instances of classes, which is used in a WCF service. Rather than passing the ServiceLocator around I decided to make it static so I could access it from every class. The service locator is populated when the WCF service kicks off and depending on some parameters, it can get populated with different concrete implementations.
My problem is that if a call is made to the WCF service before another call has finished, then (I think) the ServiceLocator will be populated with the incorrect implementations.
Is there any way to make the service locator available to classes and not have it re-used by different threads or calls to the WCF service?
You provide few details about your Service Locator and the undelying implementation. From what I read I understand that it's your own.
My personnal advice is that you should not use a Service Locator in a case of WCF service because WCF exposes some interface that enable dependency injection.
You could read more about the anti pattern Service Locator here : http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx
You should not implement your service locator because there are many OSS project around dependency injection you can use and that do the job the right way.
Here is the correct implementation of Dependency Injection using StructureMap with WCF services : http://lostechies.com/jimmybogard/2008/07/30/integrating-structuremap-with-wcf/.

Resources