none
How to Initialize a WCF Service (load values) hosted by IIS similar to using global.asax RRS feed

  • Question

  • I have a WCF Library that is hosted with an IIS Site.  I want to be able to load in several objects when the class is initialized so that they are available for each call to the service without reloading them for every call.  With ASMX, I would have done this with a Global.asax, but with a WCF Library that is Hosted by an IIS Website, I can't do that. 

    I've seen some forum entries that talk about ServiceHost and ServiceHostFactory, and I think that could be the direction, and I've also seen references to using some sort of Static Class, but I can't find any clear instructions on implementing with VB code.

    Any assistance would be appreciated.


    mp

    Friday, May 3, 2013 5:44 PM

Answers

  • I am pleased to report that I figured this out.  Part of my problem was the differences between C# (the examples I found) and VB.NET (my target language).  As it turns out, All I needed to do was create add the Initialization Code below, which uses very different syntax than the C# version.  But it works.

    I also added a reference to System.Web and Import of System.Web.Hosting in order to find the physical path.  I wanted to write an entry whenever the Initialization code executed, so I could confirm that it only executed on the first call, and was shared as read only from thereafter.  I hope this helps someone else.  It was a Huge help to me.

    Imports System.Web.Hosting

    NotInheritable Class InitializedObjectsHolder
            Private Sub New()
            End Sub
            Shared Sub New()
                ' and other initialization
                m_theObject = New BTOrdering.OrderHeader
                m_theObject.BuyerSAN = "m_theObject'sBuyerSAN"

                Dim AppPath As String = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath & "Logs\"
                Dim lf As System.IO.StreamWriter
                Try
                    lf = My.Computer.FileSystem.OpenTextFileWriter(AppPath & "InitializationLog.txt", True)
                    lf.WriteLine("Initialized at " & Now.ToString)
                    lf.Close()

                Catch ex As Exception
                    m_theObject.BuyerSAN = ex.Message
                End Try
            End Sub
            Shared m_theObject As BTOrdering.OrderHeader
            Public Shared ReadOnly Property AppData() As BTOrdering.OrderHeader
                Get
                    Return m_theObject
                End Get
            End Property
        End Class
     


    mp


    Friday, May 3, 2013 9:37 PM

All replies

  • On 5/3/2013 1:44 PM, MarvinPerkins wrote:

    I have a WCF Library that is hosted with an IIS Site.  I want to be able to load in several objects when the class is initialized so that they are available for each call to the service without reloading them for every call.  With ASMX, I would have done this with a Global.asax, but with a WCF Library that is Hosted by an IIS Website, I can't do that.

    I've seen some forum entries that talk about ServiceHost and ServiceHostFactory, and I think that could be the direction, and I've also seen references to using some sort of Static Class, but I can't find any clear instructions on implementing with VB code.

    Any assistance would be appreciated.

    What are you talking about? Is it about holding objects in session between calls to the ASMX service on the service-side?

    Friday, May 3, 2013 6:36 PM
  • I am pleased to report that I figured this out.  Part of my problem was the differences between C# (the examples I found) and VB.NET (my target language).  As it turns out, All I needed to do was create add the Initialization Code below, which uses very different syntax than the C# version.  But it works.

    I also added a reference to System.Web and Import of System.Web.Hosting in order to find the physical path.  I wanted to write an entry whenever the Initialization code executed, so I could confirm that it only executed on the first call, and was shared as read only from thereafter.  I hope this helps someone else.  It was a Huge help to me.

    Imports System.Web.Hosting

    NotInheritable Class InitializedObjectsHolder
            Private Sub New()
            End Sub
            Shared Sub New()
                ' and other initialization
                m_theObject = New BTOrdering.OrderHeader
                m_theObject.BuyerSAN = "m_theObject'sBuyerSAN"

                Dim AppPath As String = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath & "Logs\"
                Dim lf As System.IO.StreamWriter
                Try
                    lf = My.Computer.FileSystem.OpenTextFileWriter(AppPath & "InitializationLog.txt", True)
                    lf.WriteLine("Initialized at " & Now.ToString)
                    lf.Close()

                Catch ex As Exception
                    m_theObject.BuyerSAN = ex.Message
                End Try
            End Sub
            Shared m_theObject As BTOrdering.OrderHeader
            Public Shared ReadOnly Property AppData() As BTOrdering.OrderHeader
                Get
                    Return m_theObject
                End Get
            End Property
        End Class
     


    mp


    Friday, May 3, 2013 9:37 PM
  • Hi,

    Thanks for sharing the solution here, it will be very beneficial for other community members who have similar questions.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 6, 2013 8:47 AM
    Moderator