Azure Service Bus trigger tutorials - Usage of the C# Library

About Azure Service Bus trigger C# library

You can send message and fire trigger for Azure Service Bus trigger from Microsoft Azure applications (Web Worker roles or roles) by calling C# library belonging to Azure Service Bus trigger.
For more information about C# Library API Reference, refer to "C# Library API Reference".

To use C# Library

Environment Settings

The following environment is required for using C# Library using to develop applications for Microsoft Azure.

Required information for using

The following information is required to send and receive messages with Azure Service Bus trigger.

Development Flow

Microsoft Azure application development using C# library is performed in the following flow.
  1. Add reference to necessary modules with Microsoft Azure C# application on Microsoft Visual Studio(using Web role or worker role)
  2. Which implement ignite the trigger ( See "Using the C# program"Please for more information)
  3. Deploy the developed application as a service on Microsoft Azure

Usage from C# program


C# library using C# to implement the program, you must add a reference to the following two modules.
Both libraries are in the References property "Local Copy" to "True" should be set.

Program flow

  1. Specify the connection information, create an instance of AppFabricTriggerService.
  2. Using AppFabricTriggerService.PutParam method, and parameters to pass values to script input variable.
  3. The Execute method of AppFabricTriggerService is called, message is send and trigger fires.
  4. Execute method's return value, get an instance of ExecutionResponse and gets the script execution result.
  5. If successfully executed, gets the value of script output variable from ExecutionResponse.
  6. If you fail to execute, get error information form ExecutionFailure.

Sample Program

In the following sample program, specifies 3 parameters to pass to script input variables with ExecuteDataSpiderService method and fire Azure Service Bus trigger.
If successful, the process gets the valuer of three script output variables.
If the operation fails, it get the error information.
The processing and values obtained using the error information, and C# where not directly related to the use of the library have been omitted.
using System;
// Specify the C# Library namespace to using directive 
using Appresso.DataSpider.Service;
public class AppFabricTriggerTest 

	public void ExecuteDataSpiserService ()
		/ / AppFabricTriggerService instantiation
		AppFabricTriggerService service = new AppFabricTriggerService (
			"ServiceNamespace", / / Service Namespace (Service Namespace) specification
			"ServicePath", / / Service path (Service Path) specification
			"defaultIssuer", / / Default Issuer (Default Issuer) specification
			"defaultKey"); / / Default Key (Default Key) specification

		// Set parameters to pass to string script input variables "string_in"
		string string_in = "Input string";
		service.PutParam("string_in", string_in);

		// Set parameters to pass to integer script input variables "int_in"
		int int_in = 200;
		service.PutParam("int_in", int_in);
		// Set parameters to pass to Boolean script input variables "boolean_in"
		bool boolean_in = True;
		service.PutParam("boolean_in", boolean_in);

		// Call Execute method, fire the trigger and get ExecutionResponse
		ExecutionResponse response = service.Execute();
		// Get the exit status from ExecutionResponse
		int exitStatus = response.ExitStatus;
		//Get whether the process was successful from ExecutionResponse
		bool succeeded = response.Succeeded;
		if (succeeded)
			// If the operation was successful
			// Get Dictionary type instance with information of script output variables by Results property of ExecutionResponse .
			Dictionary<string, object&gt results = response.Results;
			//Get value from String type script output variable "string_out"
			string string_out = (string )results["string_out"];
			//Get value from integer type script output variable "int_out"
			int int_out = (int)results["int_out"];
			//Get value from Boolean type script output variable "boolean_out"
			bool boolean_out = (bool)results["boolean_out"];
			\\\( Omit the process of using the retrieved values) \\\
			// If the operation fails
			//Get the ExecutionFailure instance with error information from Failure property of ExecutionResponse
			ExecutionFailure failure = response.Failure;
			/  Get the error type
			string failureType = faillure.Type;
			// Get the error message
			string failureMessage = faillure.Message;
			// Get the error details
			string failureMessage = faillure.Message;
			\\\( Omit the process of using the retrieved error info) \\\

To pass a value to a script input variable

Using AppFabricTriggerService.PutParam method, and parameters to pass values to script input variable.

Pass the script input variable name of the target to the first argument of PutParam.
PutParam second parameter of the method, C# and pass the value type.
This C# type of script must be a corresponding type of the input variables of the target script.

The type of input variables and corresponding C# type of script is as follows.

Script input variable type Corresponding C# type Remarks
String string  
Integer int  
Decimal decimal  
Date/Time System.DateTime The value less than Ms digit is truncated.
Boolean bool  
Binary byte[]  
XML System.Xml.XmlDocument  

To get the script output variable

AppFabricTriggerService.Results from the property value to a variable that holds the script output you can get an instance of Dictionary type.

Value can be retrieved from this Dictionary using script output variable name as key.
Upon acquisition, the output variables corresponding to the type of C# script must be cast to type.

Type of the variable type and the corresponding C# script output is as follows.

Type of script output variable Corresponding C# type Remarks
String string  
Integer int  
Decimal decimal  
Date/Time System.DateTime  
Boolean bool  
Binary byte[]  
XML System.Xml.XmlDocument  

Main exceptions

Exception may occur when AppFabricTriggerService.Execute method is called.
The main exception and causes, and solution are as follows.

Exception Name Causes Solution
The token provider was unable to provide a security token.
Azure Service Bus failed to connect with the service. Check Service Namespace specified at AppFabricTriggerService.
Message could not be created: NotFound, No service is hosted at the specified address.
Azure Service Bus service could not be found. Check the following.
  • AppFabricTriggerService Service Path is correct
  • Whether the target Azure Service Bus trigger exists or DataSpiderServer is started
  • Whether the target Azure Service Bus trigger is enabled
The token provider was unable to provide a security token. Error:Code:401:SubCode:T2001:Detail:The issuer does not exist, or the secret or signature is invalid.
Azure Service Bus Service authentication failed. Check Default Issuer and the Default Key specified at AppFabricTriggerService.
Message could not be retrieved: NoContent, No message available within the specified timeout.
Azure Service Bus failed to receive messages from the trigger. Check the following.
  • Whether the script execution takes time
    If the script is time-consuming, set RetrieveTimeoutSec and RetrieveRetryTimes property of AppFabricTriggerService accordingly.
  • Whether error occurred in DataSpiderServer
    Please check whether error information is recorded in Server logs.