tags:

views:

10

answers:

1

I have code as following:

Public Class xxxSvcHostFactory
    Inherits ServiceHostFactory

    Protected Overrides Function CreateServiceHost(ByVal serviceType As Type, ByVal baseAddresses As Uri()) As ServiceHost
        Dim result As New WebServiceHost2(serviceType, True, baseAddresses)
        Return result
    End Function

End Class

Service contract is defined as below:

<ServiceContract()>
Public Interface IxxxSvc

    <Description("")>
    <OperationContract()>
    <WebGet(ResponseFormat:=WebMessageFormat.Json,
            UriTemplate:="CustomerDetails?id={CustomerId}")>
    Function GetCustomerDetails(ByVal CustomerId As String) As Customer


End Interface

Public Class MySvc
    Implements IxxxSvc

    Public Function GetCustomerDetails(ByVal CustomerId As String) As Customer Implements IxxxSvc.GetCustomerDetails
.
.
.
    End Function

End Class

When would CreateServiceHost gets executed?

Is it for every call, or for every transport session, or when the application startsup?

When does the ServiceHost expire?

If I implement static variable it is available through multiple sessions (say from IE and Firefox). How can I maintain static variable for a particular session (say if I access from IE, the same session should not be shared when I access from FF).

I am using WCF REST functionality in my application (core REST and not REST Starter kit). thanks

A: 

It depends! :-) As always.....

If you host this service in IIS by using a MyService.svc file, then IIS will instantiate the WebServiceHost for each incoming request and spin up a service class instance to handle the request (ok, it's probably doing some caching on this - however, not quite clear how and how long the host will live etc.). IIS is said to have "message-based activation" - e.g. potentially each incoming message/request will activate the WebServiceHost.

When you self-host in a Windows NT Service, console app etc., it's obviously totally up to you - you create the WebServiceHost at your discretion, and then it's up and running until you explicitly tear it down (or an unhandled exception brings it down). Self-hosting gives you a bit more control over lifetime of your WebServiceHost.

Check out Hosting And Consuming WCF Services on MSDN - has lots of interesting info on hosting and lifetime of your service host and so forth.

marc_s