Manufacturing Execution System (MES) : Middleware Extensibility Hooks
Manufacturing Execution System (MES) : Middleware Extensibility Hooks
Reference
Version 6.0
June, 2018
© 2018 AVEVA Group plc or its subsidiaries. All rights reserved.
The Schneider Electric industrial soft ware business and AVEVA have merged to trade as AVEVA Group
plc, a UK listed company. The Schneider Electric and Life is On trademarks are owned by Schneider
Electric and are being licensed to AVEVA by Schneider Electric.
No part of this documentation shall be reproduced, stored in a ret rieval system, or transmitted by any
means, electronic, mechanical, photocopying, rec ording, or otherwise, without the prior written
permission of AVEVA. No liability is assumed with respect to the use of the information contained herein.
Although precaution has been taken in the preparation of this docum entation, AVEVA assumes no
responsibility for errors or omissions. The information in this documentation is subject to change without
notice and does not represent a commitment on the part of AVEVA. The soft ware described in this
documentation is furnished under a license agreement. This soft ware may be used or copied only in
accordance with the terms of such license agreement.
ArchestrA, Aquis, Avantis, DYNS IM, eDNA, EYESIM, InBatch, InduSoft, InStep, IntelaTrac, InTouch,
PIPEPHASE, PRiSM, PRO/II, PROV IS IO N, ROMeo, SIM4ME, SimCentral, SimSci, Skelta,
SmartGlance, Spiral Software, Termis, WindowMaker, WindowViewer, and Wonderware are trademarks
of AVEVA and/or its subsidiaries. An extensive listing of AVEVA trademarks can be found at:
https://sw.aveva.com/legal. All other brands may be trademarks of their respective owners.
Publication date: 6/29/2018
Contact Information
AVEVA Group plc
High Cross
Madingley Road
Cambridge
CB3 OHB. UK
https://sw.aveva.com/
For information on how to cont act sales, customer training, and technical support, see
https://sw.aveva.com/contact.
MES Middleware Extensibility Hooks Reference
Contents
Chapter 1 Getting Started..................................................................................................... 5
Using This Guide ...................................................................................................................... 5
Definitions ................................................................................................................................ 5
Introduction to MES Middleware Extensibility Hooks .................................................................... 5
Custom Action Scenarios Using Hooks ................................................................................. 6
Version 6.0 3
MES Middleware Extensibility Hooks Reference Contents
4 Version 6.0
MES Middleware Extensibility Hooks Reference
C HAPTER 1
Getting Started
In This Chapter
Using This Guide ............................................................................................................................ 5
Definitions ...................................................................................................................................... 5
Introduction to MES Middleware Extensibility Hooks ......................................................................... 5
Definitions
A middleware event is a method expos ed by the MES middleware and that method can be invoked
by the client layer (e.g., stateless API, etc.) to perform some action in the MES dat abas e.
A hook represents a custom functionality implemented by the end us er in the form of a stored
procedure, assembly, workflow, etc. A pre-hook represents a custom functionality implemented by
the end user to execute that functionality before the middleware event is executed. A post -hook
represents a custom functionality implemented by the end user to execute that functionality after the
middleware event is processed.
Version 6.0 5
MES Middleware Extensibility Hooks Reference Getting Started
6 Version 6.0
MES Middleware Extensibility Hooks Reference
C HAPTER 2
Functional Overview
In This Chapter
Pre-Hook and Post-Hook Configuration ............................................................................................ 7
Execution Flow Diagram ................................................................................................................. 8
Trans action Control ......................................................................................................................... 9
Version 6.0 7
MES Middleware Extensibility Hooks Reference Functional Overview
8 Version 6.0
Functional Overview MES Middleware Extensibility Hooks Reference
Transaction Control
If pre- and/or post-hooks are c onfigured, then t he flow of the middleware event (i.e., whether t o commit or
rollback, stop or continue with the flow) is controlled by the Middleware Flow parameter [see Middleware
Flow (P re-Hook /Post-Hook ) on page 16 for more information]. If an exception is raised from the pre- or
post-hooks or from the middleware event itself, and Middleware Flow is set to Stop on Error, then the
current transaction is rolled back, including the transactions from pre - and post-hooks. The end user is
responsible for determining when to raise an error from the stored procedure/assembly and for
controlling the flow of the middleware event.
The stored procedures/assemblies involved for the pre- and post-hooks are execut ed as a part of the
middleware transaction that corresponds to the middleware event. In general, the stored
procedures/assemblies involved for the pre- and post-hooks are executed as a part of the middleware
transaction that corresponds to the middleware event. The total transaction time allotted for the
middleware event is split among the pre-hook, middleware event, and the post-hook. However, if the
Middleware Flow parameter is set to Continue E ven on Error, then each event (i.e. pre-hook, normal
middleware event, post-hook) is executed in a s eparate t rans action. In this case, the transaction time -out
that is configured applies to each transaction.
Stored Procedure
The transaction that corresponds to the middleware event needs to complete executing the custom
stored proc edure configured for t he pre-hook, complete executing the middleware stored procedure, and
complete executing the custom stored procedure configured for the post -hook. All three of these stored
procedures must be executed within the allotted transaction time; otherwise, the middleware raises a
timeout exception to the caller.
It is not recommended to have an autonomous transaction or nested transactions inside custom stored
procedures, becaus e that might affect the transaction flow built inside the MES product.
Assembly
A transaction started by the middleware event needs to be completed within the allotted time. The
amount of time allotted for the middleware transaction time includes the time the middleware takes to
execute custom pre-hook assembly, custom post-hook assembly, and the middleware call itself.
Therefore, end users must be cautious when writing custom assembly hooks such that they do not cause
any performance bottlenecks when executing the actual middleware call. If the code in the assembly
involves processing the files or executing long running transactions, then it is recommended to execut e
those processes on a background thread, so that the pre - or the post-hook processes do not block the
main thread. However, the transactions started on a separate thread (e.g., background thread) are not
part of t he transaction started on the main thread (i.e., Middleware E vent thread). Therefore, the success
or failure on the secondary thread does not affect the trans action on the main thread as long as they do
not create deadlocks.
Version 6.0 9
MES Middleware Extensibility Hooks Reference
C HAPTER 3
Configuring Pre-Hooks and Post-Hooks
In This Chapter
Installation Location ...................................................................................................................... 11
Opening the Middleware Configuration Editor ................................................................................. 11
Middleware SP Name ................................................................................................................... 12
Pre-Hook/Post-Hook Name ........................................................................................................... 15
Pre-Hook and Post-Hook Type ...................................................................................................... 16
Middleware Flow (P re-Hook/Post-Hook) ......................................................................................... 16
Loading the Configuration and Hooks............................................................................................. 18
Examples of Middleware Hook Configurations and Executions ........................................................ 19
Installation Location
The Middleware Extensibility Hooks mechanism is installed as a part of standard MES installation on
machines where the MES middleware is installed. If default location was used for the MES installation,
this application/editor (MWDBMappingEditor.exe) can be found in the following folder on the machine
that is hosting the MES host (i.e., the machine that is hosting the Middleware Server):
C:\Program Files\Wonderware\MES\Middleware\
However, the installation path can vary if the MES middleware (MES Host) is installed on a different drive
or in a different folder. Therefore, look for this application in your corresponding installation path, which
typically ends with \MES\Middleware\.
Version 6.0 11
MES Middleware Extensibility Hooks Reference Configuring Pre-Hooks and Post-Hooks
The configuration information entered in the Middleware Configuration Editor is stored in the file
MWDBMappingsCustom.xml file specified in Installation Location on page 11.
Middleware SP Name
This column in the Middleware Configuration Editor represents a unique stored procedure name for
which a post-hook or a pre-hook can be configured.
The stored procedure name is the name of a valid stored procedure that exists in the MES database to
which the MES middleware connects. The end us er is responsible for finding the correct stored
procedure name by manually looking at the stored procedure names in the MES database.
Whenever this stored procedure (c onfigured in this column) is invoked, by virtue of calling an equivalent
middleware method from the client, the middleware determines whether there are any pre- or post-hooks
configured for this stored procedure and execute them according to the Extensibility Hook configuration.
The hooks configured for a stored proc edure can be removed or deleted by highlighting the row in the
grid.
12 Version 6.0
Configuring Pre-Hooks and Post-Hooks MES Middleware Extensibility Hooks Reference
If any of these pre-6.0 stored procedures were configured for extensibility hooks, the hooks should be
reconfigured wit h the new stored procedures so that they will work properly with the current schema.
Version 6.0 13
MES Middleware Extensibility Hooks Reference Configuring Pre-Hooks and Post-Hooks
14 Version 6.0
Configuring Pre-Hooks and Post-Hooks MES Middleware Extensibility Hooks Reference
Pre-Hook/Post-Hook Name
The name of the Extensibility Hook is configured in this column.
Note: If the name is left blank (or empty), the middleware assumes that the Extensibility Hook is not
configured.
Stored Procedure
A valid stored procedure name from the MES dat abas e, if the type of Extensibility Hook is stored
procedure.
The parameter names, dat a types, and presence of default values in the custom (hook) stored procedure
must match exactly those of the parameter names, data types, and pres ence of default values in the
middleware stored procedure (stored procedures configured in the Middleware SP Name column).
However, there can be additional parameters in the custom (hook) stored procedure that might not be
matching the parameters in the middleware stored procedure, but those additional paramet ers must be
defaulted to NULL in the custom stored procedure. If they are not defaulted to NULL, an exception might
be raised by the dat abas e layer for not supplying enough values for those additional parameters. It is
recommended that the list of stored procedure parameters, data types, and default values (if any) be
copied from the middleware stored procedure to the custom stored procedure to ensure all these
properties are identical bet ween them. However, the default values for the custom (hook) stored
procedure do not have to ex actly match the default values in the middleware stored procedure.
The values supplied for the middleware stored procedure (configured in the Middleware SP Name
column) are the same values supplied to the custom (hook) stored procedure.
Assembly
The hook method in the assembly has a single ‘string’ parameter that accepts the XML message from the
middleware event. The XML message is the exact XML generated for the middleware event to process.
The end user is responsible for parsing the XML message and extracting the relevant data from the XML
message. See the Stateless API help for more details about the XML structure. The middleware calls
impersonate the ArchestrA Net work user; therefore, the custom (hook) assembly is invoked as that user.
The assembly name can be configured in two ways:
If the assembly is added to the GA C, then the detailed information about the assembly version,
public key token, etc. must be specified.
However, if the assembly is not added to the GAC, then the physical location of the assembly must
be specified.
In both of these cases, the class name and the method name must be specified by separating them by
semi-colon (;).
Note: The configured Extensibility Hook method can accept only one string parameter.
Version 6.0 15
MES Middleware Extensibility Hooks Reference Configuring Pre-Hooks and Post-Hooks
For example,
TestHooks, version= 1.0.0.0,culture=neutral,processorarchitecture=MS IL,PublicKeyToken= 23106a8
6e706d0ae; TestHooksNamespace. TestHooksClass;TestHooksMethod
represents the assembly name as TestHooks.dll, version as 1.0.0.0, public key token as
23106a86e706d0ae, and that has the namespace TestHooksNamespace, class name TestHooksClass,
and the method name TestHooksMethod.
Stop
This option indicates that the flow of the middleware exec ution will stop after completing the current
execution, regardless of whet her the current execution succeeds or not. In other words, when the pre- or
the post-hook is configured with this option, the execution stops with the current call and returns the
result to the caller, regardless of whether the current transaction was successful or not (i. e., committed or
rolled back). If an error occurs, the error message is returned to the caller.
The table below describes the flow of the middleware call after the pre-hook or post-hook execution is
completed with the Middleware Flow parameter set to Stop.
16 Version 6.0
Configuring Pre-Hooks and Post-Hooks MES Middleware Extensibility Hooks Reference
Pre-Hook Stop. The pre-hook call is completed Stop. The pre-hook call resulted in an error.
successfully. The error message is returned to the end
user or the error message is logged in the
Note: The data modified on tables for the logger.
pre-hook call is committed. The normal
middleware call will never be executed. Note: The data modified on table for the
pre-hook call is rolled back. The normal
middleware call will never be executed.
Post-Hook Not applicable; this option cannot be Not applicable; this option cannot be
configured for post-hooks. configured for post-hooks.
Stop on Error
This option indicat es that the flow of the middleware execution will stop only if it encounters an error while
processing the current execution. As long as there is no error while executing the pre -hook, post-hook, or
the middleware event, the execution continues.
The table below describes the flow of the middleware call after the pre-hook or post-hook execution is
completed with the Middleware Flow parameter set to Stop on Error.
Middleware Execution
Hook Type Status Hook Succe ss Hook Error
Pre-Hook Not applicable. The pre-hook call Stop. The pre-hook call
completed successfully. resulted in an error. The
error message is returned
Note: The data modified to the caller.
on tables for the pre-hook
call is committed. The Note: The data modified
middleware continues to on tables for the pre-hook
execute the normal call is rolled back. The
middleware call. normal middleware call will
never be executed.
Post-Hook The normal middleware The data modified on The data modified for the
call/transaction has tables for the normal normal middleware call
succeeded. middleware call and for the and for the post-hook are
post-hook are committed. both rolled back. The error
message is returned to the
caller.
Version 6.0 17
MES Middleware Extensibility Hooks Reference Configuring Pre-Hooks and Post-Hooks
Middleware Execution
Hook Type Status Hook Succe ss Hook Error
Post-Hook The normal middleware The databas e transactions The data modified on
call/transaction has started for the normal tables for the normal
succeeded. middleware call and for the middleware call is
post-hook call are committed, but the data
committed. modified on tables for the
post-hook is rolled back.
The error message
generated by the
post-hook transaction is
ignored by the middleware.
Post-Hook The normal middleware Stop. The error message is Not applicable.
call/transaction has failed returned to the caller.
or rolled back. Note: The post-hook will
Note: The post-hook will never be executed,
never be executed, because the normal
because the normal middleware
middleware call/event/transaction has
call/event/transaction has failed or rolled back.
failed or rolled back.
18 Version 6.0
Configuring Pre-Hooks and Post-Hooks MES Middleware Extensibility Hooks Reference
Pre-Hook Name sp_I_Uom_Log The custom stored proc edure created by the
end user in the MES dat abase after installing
the MES database.
Pre-Hook Type SP Indicates that the type of this hook is a stored
procedure
Middleware Flow STOP ON ERROR Indicates to the middleware to stop the
execution if there was an error while executing
the sp_I_Uom_Log stored proc edure.
END
Version 6.0 19
MES Middleware Extensibility Hooks Reference Configuring Pre-Hooks and Post-Hooks
As can be seen from its content, the stored procedure sp_I_Uom_Log contains all the parameters and
their properties that sp_I_Uom has, and an additional paramet er containing more contextual information.
The custom stored procedure can c omplet ely ignore the input parameters and the code inside the "begin
and end" block can be customized for individual needs. The statements in bold indicate that one or more
custom stored procedures can be called from this custom stored procedure. The concept described
above is also applicable to configuring the post-hook of type stored procedure.
Action
When a new unit of measurement (UOM) is added from the MES Client application or by using an
ArchestrA script to add the UOM, the middleware typically uses the sp_I_Uom stored proc edure to add
the new unit of meas urement. As explained in Functional Overview on page 7, the middleware first
verifies whet her any pre-hook is configured for the sp_I_Uom stored procedure. Since there is a
pre-hook sp_I_Uom_Log configured for this case, the middleware first executes the stored procedure
sp_I_Uom_Log and then evaluates the result before processing the standard middleware call sp_I_Uom
to create a new unit of measurement in the MES database.
Middleware SP
Name Pre-Hook Name Pre-Hook Type Middleware Flow
20 Version 6.0
Configuring Pre-Hooks and Post-Hooks MES Middleware Extensibility Hooks Reference
Action
In this case, when a new UOM is added from the MES Client application or by using an ArchestrA script
to add the UOM, the middleware executes the stored procedure sp_I_Uom_Log, and evaluates the
result before actually processing the standard middleware call sp_I_Uom to create a new unit of
measurement in the MES database. Since the Middleware Flow paramet er is set to Stop after executing
the custom (pre-hook ) stored proc edure, the sequence of execution stops and returns the following error
to the client:
Error message from the custom hook stored procedure - sp_I_Uom_Log
Note that even if there are no errors in t he custom hook stored procedure, the normal middleware call will
never be processed in this case, because the the Middleware Flow parameter is set to Stop after
executing the pre-hook call. That is, in this case, the middleware never adds the unit of measurement.
Middleware SP Middleware
Name Pre-Hook Name Pre-Hook Type Flow
Version 6.0 21
MES Middleware Extensibility Hooks Reference Configuring Pre-Hooks and Post-Hooks
DateTime.Now.ToString(), xmlSource);
// Write the string to a file.
using (System.IO.StreamWriter file = new
System.IO.StreamWriter("c:\\Temp\\UomLog.txt", true))
{
file.WriteLine(lines);
}
}
}
}
The value for the xmlSource parameter contains the XML message for the normal middleware event.
The structure of the XML message used for this case is listed below:
<?xml version='1.0'?>
<request>
<object>uom</object>
<msgtype>exec</msgtype>
<cmd>add</cmd>
<uom>
<session_id>119</session_id>
<description>Unit_001</description>
<abbreviation>ssss</abbreviation>
<last_edit_comment></last_edit_comment>
</uom>
<validate>1</validate>
</request>
The text output is written to the text file UomLog.txt in the format below:
DateTime: 1/2/2013 11:34:44 AM, XMLSource:
<?xmlversion='1.0'?><request><object>uom</object>
<msgtype>exec</msgtype><cmd>add</cmd><uom><session_id>119
</session_id><description>Unit_001</description>
<abbreviation>ssss</abbreviation><last_edit_comment>
</last_edit_comment></uom><validate>1</validate></request>
The concept described above is also applic able to configuring the post -hook of type assembly.
Action
When a new UOM is added from the MES Client application or by using an ArchestrA script to add the
UOM, the middleware typically uses the sp_I_Uom stored procedure to add the new unit of
measurement. As explained in Functional Overview on page 7, the middleware first verifies whether any
pre-hook is configured for the sp_I_Uom stored procedure. Since there is a pre-hook TestHooks.dll
configured for this case, the middleware first executes the method TestMethod in the TestHook s.dll
assembly and then evaluates the result before actually processing the standard middleware call
sp_I_Uom to create a new unit of meas urement in the MES databas e.
22 Version 6.0
Configuring Pre-Hooks and Post-Hooks MES Middleware Extensibility Hooks Reference
Middleware SP Middleware
Name Pre-Hook Name Pre-Hook Type Flow
Action
When a new UOM is added from the MES Client application or by using an ArchestrA script to add the
UOM, the middleware typically uses the sp_I_Uom store d procedure to add the new unit of
measurement. As explained in Functional Overview on page 7, the middleware first verifies whether any
pre-hook is configured for the sp_I_Uom stored procedure. Since there is a pre-hook TestHooks.dll
configured for this case, the middleware first executes the method TestMethod in the TestHooks.dll
assembly and then evaluates the result before actually processing the standard middleware call
sp_I_Uom to create a new unit of meas urement in the MES databas e.
Version 6.0 23
MES Middleware Extensibility Hooks Reference
C HAPTER 4
Configuring Extensibility Hooks on Multiple
Middleware Servers
The Middleware Extensibility Hooks are configured in the Extensibility Hooks tab in the Middleware
Configuration Editor, and the hooks configurations are stored in the same folder in the file
MWDBMappingsCustom.xml. The MES Middleware S erver loads the Extensibility Hooks only if the
hook parameters are configured in the MWDBMappingsCustom.xml file in its folder. When there is
more than one Middleware Server configured to connect to the same MES database, then the end user
is responsible for managing the hook configurations and keeping them in sync across all the Middleware
Servers.
For example, let’s say Middleware Server 1 has a pre -hook configured for the sp_I_Uom (Uom.Add) to
perform a pre-action before the actual sp_I_Uom is executed. Middleware Server 2 has a pre-hook
configured for the sp_U_Job_Exec_StartBatchJobs (Job_Exec.StartJob) to perform a pre-action before
the actual sp_U_Job_Exec_StartBatchJobs is executed. These two Middleware Servers are configured
to connect to the same MES database, and these two are the only two hooks configured in their
respective middleware directories.
The table below explains how the pre -hook commands are processed by both the Middleware Servers
using the example above:
The Middleware Server 1 processes the pre-hook for sp_I_UOM before calling the sp_I_Uom stored
procedure on the MES database, but does not process the pre-hook for
sp_U_Job_Exec _StartBatchJobs before calling this stored procedure, because Middleware Server 1
does not know anything about the pre-hook for sp_U_Job_Exec_StartBatchJobs, since the XML file in its
folder does not have any information about the hook for sp_U_Job_Exec_StartbatchJobs.
Similarly, the Middleware Server 2 processes the pre -hook for sp_U_Job_Exec_StartBatchJobs before
calling the sp_U_Job_Exec_StartBatchJobs stored procedure on the MES database, but does not
process the pre-hook for sp_I_UOM before calling this stored procedure.
Note: If the desire is to have an Extensibility Hook processed for a middleware stored procedure,
regardless of the Middleware Server that invok es the stored procedure, then the user must copy the
configuration file or manually configure t he Extensibility Hook configurations into all t he MES Middleware
Servers, thereby keeping the configuration information in sync across the Middleware Servers.
Version 6.0 25
MES Middleware Extensibility Hooks Reference
C HAPTER 5
Frequently Asked Questions
What are hooks? What is the purpose of having hooks configured in the MES
middleware?
A hook represents a custom functionality implemented by the end user in the form of a stored procedure,
assembly, work flow, and so on. The purpose of having a hook is to allow a custom functionality to be
integrated as a part of standard MES event and ex ecute that functionality as a part of standard MES
event.
How do you determine the name of a stored procedure that corresponds to a specific
middleware event?
In most cases, the name of the MES middleware event matches the name of the MES stored procedure.
If not, they are configured in the MWDBMappings.xml file under the db_objects section. This XML file
resides in the same folder where the MES server is installed on the host machine. It is recommended to
first look in this file to find the name of the stored procedure that corresponds to the name of the
middleware event. If such a configuration is not found in this file, then the stored procedure name is
resolved automatically on-the-fly by the Middleware Server. For example, a middleware event Item.Add
corresponds to the stored procedure sp_I_Item, Attr.Update corresponds to sp_U_Attr, Ent.GetAll
corresponds to sp_SA_Ent, JobExec.DownloadS pecs corres ponds to
sp_U_Job_Exec _DownloadSpecs, and so on. See the "Middleware Configuration Editor" chapter in the
MES Installation Guide or online help for more det ails about how a middleware method is mapped to a
stored procedure.
Can stored procedure names configured in the custom database mappings tab in the
Middleware Configuration Editor be used as part of hooks? How are those stored
procedures related to hooks?
Yes, the stored procedures (names) configured in the custom dat abase mappings can be used to
configure the stored procedure hooks. The custom database mappings is a separate mechanism that
allows to map a custom stored procedure created by an end user, and t hat allows an end user to call that
custom stored procedure from the client applications using the standard XML structure used by the
middleware.
Does the Middleware Server need to be restarted if a hook configuration is modified?
Yes, whenever a hook configuration is modified, the Middleware Server needs to be restarted to reload
the changes in its memory. Otherwise, the changes are not effective until the Middleware Server is
restarted.
If there is more than one Middleware Server, does the hook information need to be
copied to more than one Middleware Server?
Yes. It is the responsibility of the end user to copy and synchronize the hooks configurations on all the
Middleware Servers.
Does the pre-hook or the post-hook mechanism support the UpdateSpecific() method in
the middleware to perform pre- or post-processing?
No. The UpdateSpecific() method in the middleware uses dynamic SQL (a SQL Updat e statement
generated when needed and not pre -compiled) to update the data in the MES database. Since there are
no stored procedures involved with the UpdateSpecific() method, pre- and post-hooks cannot be
configured and exec uted against this method.
Version 6.0 27
MES Middleware Extensibility Hooks Reference Frequently Asked Questions
28 Version 6.0