Search code examples
c#workflow-foundation-4

Activity throws exception when C# expression uses reference types


I have a primitive "Assignment" activity. It takes values from an input variable for the parent sequence and puts them into an output variable for the parent sequence. If I simply perform this copy operation, everything works and the workflow completes successfully. If I introduce a system type "Random" or a project enum "ReportStatusType" the workflow throws an exception "A System.NotSupportedException was thrown: "Expression Activity type 'CSharpValue`1' requires compilation in order to run."

I created this workflow from an example for wf 4. I'm using wf 4.5, I think. I'm working in VS2013 and targeting .NET Framework 4.5. I used "WCF Workflow Service Application" template which uses an IIS platform. I'm using "WCF Test Client" to invoke the service and view the response.

THIS VALUE EXPRESSION WORKS:

new ExpenseReportConfirmation() {
       Amount = report.Amount,
       City = report.Amount,
       Client = report.Client,
       Employee = report.Employee,
       EndDate = report.EndDate,
       StartDate = report.StartDate,
       ReportID = 5
    };

THIS VALUE EXPRESSION FAILS:

new ExpenseReportConfirmation() {
       Amount = report.Amount,
       City = report.Amount,
       Client = report.Client,
       Employee = report.Employee,
       EndDate = report.EndDate,
       StartDate = report.StartDate,
       ReportID = new Random().Next(0,5),
    };

My Imported Namespaces look like this:

enter image description here

This also fails if I try to create the ReportID in another assignment activity and then refer to it in the value expression shown above. It fails wherever Random() is expressed.

This may be a rookie mistake on my part, but I'm out of ideas. Anyone else have any?


Solution

  • This issue had nothing to do with compiling the C# expressions since I was deploying the service under IIS and it handles the compilation. It was resolved after reading this Visual Studio Feedback article. My assignment activity value was written like this, and always threw an exception when the Random() function was included:

    new ExpenseReportConfirmation() {
      Amount = report.Amount,
      City = report.City,
      Client = report.Client,
      Employee = report.Employee,
      EndDate = report.EndDate,
      StartDate = report.StartDate,
      ReportID = new Random().Next(0, 50)
    };
    

    I re-wrote the expression to this and everything works:

    new ExpenseReportConfirmation() { Amount = report.Amount, City = report.City, Client = report.Client, Employee = report.Employee, EndDate = report.EndDate, StartDate = report.StartDate, ReportID = new Random().Next(0, 50) };
    

    PS: I was using the WCF Test Client to interact with my workflow service. When it catches an error, a large amount of text is dumped to the user, including the first 1024 bytes of the service response. It took me a while to learn to dig down past all that and find the internal exception. Here is an example of what to look for near the bottom of the text dump:

    [ConfigurationErrorsException: The extension '.xalmx' is not registered with WCF/WF handler.