ServiceHostFactory.CreateServiceHost is called multiple WCF

I have a wcf service that is hosted in IIS

<%@ ServiceHost Language="C#" Debug="true" Service="MyService" Factory="InitializableServiceHostFactory" %> 

my problem is that CreateServiceHost is called with every request to the MyService method that does my initialization code, which is written in CreateServiceHost, called multiply.

+6
source share
2 answers

After I checked this problem many times, I saw that it is related to ASP.NET dynamic compilation, it will recompile the whole site when some things inside the Bin folder are changed, but my WCF service writes to a temporary folder inside Bin. which force the site to recompile and then restart Application_Start, then InitializableServiceHostFactory will re-create the WCF service

I know this is a dummy problem :(

+2
source

I suspected the host service was not an instance of the service class, and Carlos Figueira confirmed that in his answer to [Do I need Dispose () when using the custom ServiceHostFactory?] .

CreateServiceHost() in the factory was called several times for my application, throwing an ArgumentException the second time it was initialized, with the message:

 The value could not be added to the collection, as the collection already contains an item of the same type: .... 

WCF sometimes reuses a service host. I worked on the problem by checking if any of my configured actions were already on the list of servicehost Description.Endpoints.Behaviors , so I did not initialize my service host a second time. In practice, my case was simpler because I installed the ServiceAuthorizationManager ; if I find that it is already installed, I no longer avoided initializing anything.

0
source

Source: https://habr.com/ru/post/945071/


All Articles