WCF Rest Application_End - wcf

I have a WCF Rest service and I have added this attributes to service:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[ServiceContract]
I am not sure which one of the 2 of this statements are correct :
Application_End from Global.asax will be called everytime a request is completed
or
Application_End from Global.asax will be called once when the IIS will fire recycle
Can you help me out? I am having trouble understanding so many Application_End in my logs

InstanceContextMode.PerCall -- spawns an instance of your service per call. Unlike the standard first in first out approach. If you hit your service with 10 requests at once you should see 10 Application_End events.
Application_End from Global.asax will be called everytime a request is completed

Related

Consume wcf service in .net core which to use services.AddSingleton or services.AddTransient

I am consuming a WCF service in my .net core API project and I used the following steps:
The binding in WCF service (for testing) <add binding="basicHttpsBinding" scheme="https" />
Created a proxy class using https://learn.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-svcutil-guide?tabs=dotnetsvcutil2x (This created a proxy which has an interface IService1 and calls Service1Client).
In the startup class I have bound the interface with the class services.AddSingleton<IService1, Service1Client>();
The method endpoint in the WCF service gets the data form the database according to the parameter passed to consume the WCF service.
I am not sure which one I should use, services.AddSingleton or services.AddTransient, because I am not sure what the proxy class is using to call the method.
If I create a single instance, will it be locked?
I have done a Jmeter test with 1000 rows in the database and 1000 rows from csv as a parameter to consume the API, but did not find any lock and all were successful under 3 minutes.
You can view the servicereference.cs file to see which method in the wcf service is called by the proxy class.
Then we need to instantiate the proxy class and call the WCF service through the instantiated proxy class:
ServiceReference.Service1Client service1 = new Service1Client();
By this documentation in a web api a single httpclient should be instantiated:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client#create-and-initialize-httpclient
Of course if you have multiple target services, with different endpoints, an HttpClient per service is needed (you set the base address in the HttpClient)
In your case your generated client should use an HttpClient under the covers. If you can determine if the whole genrated client is thread safe then you can inject the client as a Singleton, if not you should add it as scoped

Multiple dependent WCF service

Is there a way where I can create a set of wcf services where one service depends upon the request of the previous service?
That is, I create Service1, Service2, Service3.
Service2 should be invoked when the request of Service1 arrives.
Similarly, Service3 should be invoked when the request of Service2 arrives.
The response would then travel back from the Serice3 to Service2 then to Service1.
If this is not feasible, what is the nearest thing that is possible?
Kindly provide links to tutorials if you can.
Thanks

WebServiceHostFactory and WebServiceHost instance lifetime

I am trying to figure out the lifetime of a WebServiceHost. I thought that it would initially be per-call/per-request (i.e. Similar to an ASP.Net page being created for each request). I've created a Custom WebServiceHostFactory and WebServiceHost, but noticed that the Factory and Host are only created once.
The CustomWebServiceHostFactory overrides CreateServiceHost and the CustomWebServiceHost overrides OnOpening to spit out some diagnostics to track lifetimes.
My Service File
[ServiceContract]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public partial class ProductService
{
... // code
}
My Global.asax File
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("products", new CustomServiceHostFactory(), typeof(ProductService)));
}
Is anyone able to explain why two requests are hitting the same instance of CustomWebServiceHost, and how I would create a new Host for each service request?
Thanks
Creating the host for each new request doesn't make sense - the service host is the WCF runtime on the server side - the piece of code that will
listen for incoming requests
dispatch those requests to the service classes to be handled
Creating the service host is a pretty expensive and extensive operation - in the case of hosting in IIS, this will be done "on demand" when the first request comes in, but the service host will stay around for a given time and be reused.
When self-hosting, you typically create a single service host instance in your managed application, and then leave that host open and active until you want to shut down your services.
What will be created on a per-call basis for each request is the service class instance that actually handles the request coming in (but not the service host).

WCF Service - Asynch Operation or Queued Messaging

I have a WCF service hosted as Windows Service with most of its methods currently defined as:
[OperationContract(IsOneWay = true)]
But, now I need to send response back to the calling Web application for these service methods.
Now, because service methods are bit heavy (FYI, they are reporting methods that needs to do mail merge for a no. of records), I am thinking to either queue them or to process asynchronously, so essentially when the request is sent to the service it should save the request to database/queue, returning Request-Id to calling Web application.
In the mean-time, WCF service can just process incompleted requests from the queue or database.
Then either calling Web application can ping WCF service for status of request because it has Request-Id or
WCF service can ping back to calling app when the process corresponding to a Request-Id is completed.
To achieve above, can anyone please guide what changes I need to make to my WCF service (which currently has all one way operation)?
Also, please guide me whether I need to go for Asynch operation or message queuing?
Thank you!
Of course, going Async is simple:
remove the OneWay on the OperationContract in question and regenerate your Service WITH Async methods. There's a reason why Silverlight forces you to use Async operations. They do force you to rethink your UI.

How to make IIS wait for WCF service gets ready?

I have a WCF service hosted in IIS 7. It takes some minutes until this service finish to load its data and ready for external calls. Data loads in internal thread. The problem is that from IIS point the service is ready just after it was activated (by some call), and it process a request without waiting for data to be loaded.
Is it possible to tell IIS that the service is still loading and make this service unavailable for requests? No problem if such request will throw an exception.
You could invoke the initialization logic synchronously in the service's default constructor. The service operations won't be invoked until the service instance has been created, which will only happen after the initialization has completed. In the meantime clients simply won't receive a response.
Here's a quick example:
public class MyService : IMyService
{
public MyService()
{
// Blocking call that initializes
// the service instance
this.Initialize();
}
public void GetData()
{
// The service operation will be invoked
// after the service instance has been created
// at which point the initialization is complete
}
private void Initialize()
{
// Initialization logic
}
}
If the initialization logic is expensive, you should consider making your service run as a singleton, so that the price is paid only at the first request. Alternatively you could store the data loaded during initialization in a centralized cache. This way it can be made available to multiple service instances while still having to load it only once.
If the initialization logic is shared across multiple services, you should consider implementing it once in a custom ServiceHost by overriding the OnOpening method. Since you're hosting your services in IIS, you would then also need to implement a custom ServiceHostFactory to create instances of your ServiceHost.You can read more about this approach in this MSDN article.
Found this new feature described in "ASP.NET 4 and Visual Studio 2010 Web Development Overview": http://www.asp.net/LEARN/whitepapers/aspnet4/#0.2__Toc253429241
The problem that it's requires IIS 7.5 running on Windows Server 2008 R2, but works with ASP.NET 2.0+