WFBlogger.Net

Workflow Foundation Blog

Starting WF in ASP.NET Global ASAX File

clock July 14, 2008 09:26 by author Administrator

The most uncomplicated method of starting WF and keeping it running is to start WF when the website application starts up.

This can lead to the application start up time to increase depending on the number of running workflows.

To do this, you need to setup a global.asax file and on application start up start the necessary WF components.

 

 

Sample:

 

<%@ Application Language="VB" %>

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        Dim workflowRuntime As System.Workflow.Runtime.WorkflowRuntime = New System.Workflow.Runtime.WorkflowRuntime("WorkflowRuntime")
        Application("WorkflowRuntime") = workflowRuntime

        Dim typeProvider As System.Workflow.ComponentModel.Compiler.TypeProvider = New System.Workflow.ComponentModel.Compiler.TypeProvider(workflowRuntime)
        typeProvider.AddAssembly(GetType(ASM.Web.WF.WIM.AuthorizationsWF).Assembly)
        workflowRuntime.AddService(typeProvider)
       
        ' Add the external data service

        ' Add custom manual charge service
        'requestService = New RequestService()
        'dataService.AddService(requestService)
       
        '  Should only be run when changes are made to the tracking profile.
        CreateAndInsertTrackingProfile()

        workflowRuntime.StartRuntime()
    End Sub
   
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        Dim workflowRuntime As System.Workflow.Runtime.WorkflowRuntime = Application("WorkflowRuntime")
        workflowRuntime.StopRuntime()
    End Sub
   
    Sub CreateAndInsertTrackingProfile()
        Dim trackingProfile As Workflow.Runtime.Tracking.TrackingProfile = New Workflow.Runtime.Tracking.TrackingProfile()

        Dim activityTrackPoint As Workflow.Runtime.Tracking.ActivityTrackPoint = New Workflow.Runtime.Tracking.ActivityTrackPoint()
        Dim activityTrackingLocation As Workflow.Runtime.Tracking.ActivityTrackingLocation = New Workflow.Runtime.Tracking.ActivityTrackingLocation(GetType(Workflow.ComponentModel.Activity))
        activityTrackingLocation.MatchDerivedTypes = True
       
        Dim activityExecutionStatuses As Collections.Generic.IEnumerable(Of Workflow.ComponentModel.ActivityExecutionStatus) = CType(System.Enum.GetValues(GetType(Workflow.ComponentModel.ActivityExecutionStatus)), Collections.Generic.IEnumerable(Of Workflow.ComponentModel.ActivityExecutionStatus))
        For Each activityExecutionStatus As Workflow.ComponentModel.ActivityExecutionStatus In activityExecutionStatuses
            activityTrackingLocation.ExecutionStatusEvents.Add(activityExecutionStatus)
        Next

        activityTrackPoint.MatchingLocations.Add(activityTrackingLocation)
       
        Dim workflowDataTrackingExtract As System.Workflow.Runtime.Tracking.WorkflowDataTrackingExtract = New System.Workflow.Runtime.Tracking.WorkflowDataTrackingExtract()
        workflowDataTrackingExtract.Member = "FileEventArgs"

        activityTrackPoint.Extracts.Add(workflowDataTrackingExtract)
       
        trackingProfile.ActivityTrackPoints.Add(activityTrackPoint)
        trackingProfile.Version = New Version("1.0.0.1")

        Dim workflowTrackPoint As Workflow.Runtime.Tracking.WorkflowTrackPoint = New Workflow.Runtime.Tracking.WorkflowTrackPoint()
        Dim workflowTrackingLocation As Workflow.Runtime.Tracking.WorkflowTrackingLocation = New Workflow.Runtime.Tracking.WorkflowTrackingLocation()
       
        Dim trackingWorkflowEvents As Collections.Generic.IEnumerable(Of Workflow.Runtime.Tracking.TrackingWorkflowEvent) = CType(System.Enum.GetValues(GetType(Workflow.Runtime.Tracking.TrackingWorkflowEvent)), Collections.Generic.IEnumerable(Of Workflow.Runtime.Tracking.TrackingWorkflowEvent))
        For Each trackingWorkflowEvent As Workflow.Runtime.Tracking.TrackingWorkflowEvent In trackingWorkflowEvents
            workflowTrackingLocation.Events.Add(trackingWorkflowEvent)
        Next

        workflowTrackPoint.MatchingLocation = workflowTrackingLocation
        trackingProfile.WorkflowTrackPoints.Add(workflowTrackPoint)
       
        InsertTrackingProfile(trackingProfile)
    End Sub

    Sub InsertTrackingProfile(ByVal trackingProfile As Workflow.Runtime.Tracking.TrackingProfile)
        Dim profile As String = SerializeTrackingProfile(trackingProfile)
       
        Using sqlCommand As New Data.SqlClient.SqlCommand()
            sqlCommand.CommandType = System.Data.CommandType.StoredProcedure
            sqlCommand.CommandText = "dbo.UpdateTrackingProfile"
            sqlCommand.Connection = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings.Item("SharedConnectionString").ConnectionString)

            Dim typeFullName As Data.SqlClient.SqlParameter = New Data.SqlClient.SqlParameter()
            typeFullName.ParameterName = "@TypeFullName"
            typeFullName.SqlDbType = System.Data.SqlDbType.NVarChar
            typeFullName.SqlValue = GetType(ASM.Web.WF.WIM.AuthorizationsWF).ToString()
            sqlCommand.Parameters.Add(typeFullName)

            Dim assemblyFullName As Data.SqlClient.SqlParameter = New Data.SqlClient.SqlParameter()
            assemblyFullName.ParameterName = "@AssemblyFullName"
            assemblyFullName.SqlDbType = System.Data.SqlDbType.NVarChar
            assemblyFullName.SqlValue = GetType(ASM.Web.WF.WIM.AuthorizationsWF).Assembly.FullName
            sqlCommand.Parameters.Add(assemblyFullName)

            Dim versionId As Data.SqlClient.SqlParameter = New Data.SqlClient.SqlParameter()
            versionId.ParameterName = "@Version"
            versionId.SqlDbType = System.Data.SqlDbType.VarChar
            versionId.SqlValue = trackingProfile.Version.ToString()
            sqlCommand.Parameters.Add(versionId)

            Dim trackingProfileXml As Data.SqlClient.SqlParameter = New Data.SqlClient.SqlParameter()
            trackingProfileXml.ParameterName = "@TrackingProfileXml"
            trackingProfileXml.SqlDbType = System.Data.SqlDbType.NVarChar
            trackingProfileXml.SqlValue = profile
            sqlCommand.Parameters.Add(trackingProfileXml)

            sqlCommand.Connection.Open()
            Try
                sqlCommand.ExecuteNonQuery()
            Catch e As Data.SqlClient.SqlException
                Dim s As String
                s = e.Message
            End Try
        End Using
    End Sub

    Function SerializeTrackingProfile(ByVal trackingProfile As Workflow.Runtime.Tracking.TrackingProfile) As String
        Dim trackingProfileSerializer As Workflow.Runtime.Tracking.TrackingProfileSerializer = New Workflow.Runtime.Tracking.TrackingProfileSerializer()
        Dim stringWriter As IO.StringWriter = New IO.StringWriter(New StringBuilder(), System.Globalization.CultureInfo.InvariantCulture)
        trackingProfileSerializer.Serialize(stringWriter, trackingProfile)
        Return stringWriter.ToString()
    End Function
           
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
       
    End Sub

      
</script>

kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


How to setup your environment to use WF Foundation for ASP.NET in Visual Studio 2005

clock July 11, 2008 12:19 by author Administrator

Getting setup and understanding WF Foundation is confusing and annoying. Knowing where to go and what to download what to install what order it's annoying to say the least, if you have a new computer and visual studio 2008 then you won't have any problems, but if your like me then you have visual studio 2005 and windows XP.

 

--- Installing WF Foundation ---

-- End Installing WF Foundation ---

Once you have the above installed you'll need to setup your SQL database.

 

Install the workflow persistence for SQL persistence tracking

 

C:\WINDOWS\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\EN

 

Tracking_Schema.sql

Tracking_Logic.sql

 

Install the aspnet persistence provider

 

C:\WINDOWS\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\EN

 

SqlPersistenceService_Schema.sql

SqlPersistenceService_Logic.sql

 

Install the aspnet membership provider (You don't need this but it's a good pattern to have if you don't have your own).

 

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Welcome to WFblogger.NET

clock July 10, 2008 17:38 by author Administrator

Thanks for taking a look at WFBlogger.NET

Here we will discover together all the cool things we can do with windows workflow foundation.

Feel free to comment and discuss any posts on this website.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


RecentComments

None

Tag cloud


Sign in