Showing posts with label Web services. Show all posts
Showing posts with label Web services. Show all posts

Thursday, 4 September 2014

How to call a Syspro Business Object from a SQL Trigger

Sometimes one of the worst options for customising Syspro, using a SQL Trigger, is the best option you've got. In that circumstance, how do you call a Syspro Business Object from within the SQL Trigger? Here are some ways:

Create a CLR Stored Procedure

Create a CLR Stored Procedure and call that from your SQL Trigger. In your CLR Stored Procedure, you can call the Business Object via one of Syspro's COM Objects, or via Syspro's web service or Syspro's WCF service. However, you must watch out for the 32/64-bit issue:

Watch out for the 32/64-bit issue

If you are using one of Syspro's COM objects, you may run into the problem of trying to run a 32-bit application in a 64-bit process, which can't be done (unless you want to hack around with the registry as described here - good luck). This problem arises because if your version of SQL Server is 64-bit, then you won't be able to access any 32-bit COM objects from it (such as Encore.dll or Syspro32.dll); you could use Syspro.dll (which is 64-bit), but at the time of writing, September 2014, it had't been released.

You could overcome the 32-64 bit disconnect by using Inter-Process Communication or some other communication method, but you may just find it easier to use another method.

Call a COM Object

You CAN call a COM object directly from a SQL Trigger using the extended stored procedures, sp_OACreate and sp_OAMethod (details here).
Then you could either call a web service, or call Encore.dll or Syspro.dll (being mindful of the 32/64-bit issue as mentioned above). Here's one example.
However, this is not a good solution due to the level of security you have to give to run those extended stored procedures.

Use Document Flow Manager

You could dump out files and feed them into Syspro's Document Flow Manager, a server process which monitors a directory for files and feeds them into Syspro via one of Syspro's e.Net interfaces.

Call a separate process via xp_cmdshell

The quickest and simplest method is: from your SQL Trigger, use the xp_cmdshell function, e.g.

EXEC master..xp_cmdshell @yourParameters
This has the same security issues as calling a COM Object, but it isolates your process from the SQL Server, and so this is probably the best method.

Thursday, 15 May 2014

How to test your Syspro WCF Service

These are some basic tests you can use to check if Syspro's WCF Service is up and running.
  1. Open a web browser and go to a URL such as:

    http://example.com:97/SYSPROWCFService/Rest

    You should see something like this:

  2. Open a web browser and go to a URL such as:

    http://example.com:97/SYSPROWCFService/Rest/GetVersion

    You should receive a text file called GetVersion containing the version number of the WCF Service, e.g. 7.0.0.3.
  3. Can you ping the Syspro server (provided the Syspro server responds to ICMP ping requests)?
     
  4. Telnet to the server and port, then hit ENTER a few times, e.g.

    telnet example.com 99

    If you get this:

    Connecting To example.com...Could not open connection to the host, on port 99: Connection failed

    then you didn't get a connection at all. This could happen if you had the wrong port or IP address.

    However, if you got a clear screen, then when you hit ENTER the telnet session terminated, then you got a connection OK.

    "Actively refused" means that you got at least as far as a TCP Connect packet arriving at the named server.
     
  5. From an elevated command prompt (i.e. run CMD as Administrator), run this command:

    netstat -ano | findstr 9999

    where 9999 is the process number or port number you are interested in.
    You should see your process and port listed.
     
  6. Check the Event Log for any error messages: in the Event Viewer, look under Application and Services Logs / SYSPROWCF.

Monday, 21 April 2014

How to access SOAP services from VBA in Excel or similar

To access a SOAP web service (such as a Syspro web service) from Visual Basic for Applications (such as an Excel 2003 macro), you can download Microsoft’s SOAP toolkit and access that from Visual Basic.
If you tailor the example code from Microsoft’s SOAP toolkit to make it easy to call Syspro functions; here’s a code snippet example:
Dim GUID as String
Dim XmlOut as String
Dim WebServicesBaseURL = "http://example.com/sysprowebservices"

GUID = LogonToSysproViaWebServices()
XmlOut = CreateSalesOrderWebServices(GUID, "SORTOI", XmlParameters, XmlIn)

Private Function LogonToSysproViaWebServices() As String

    Dim XmlIn As String
    Dim XmlOut As String
    Dim GUID As String

    XmlIn = ""

    Dim objSysproWS As New Syspro_Utilities_Web_Service
    objSysproWS.Setup WebServicesBaseURL

    XmlOut = objSysproWS.Logon(Operator, OperatorPassword, CompanyId, CompanyPassword, LanguageCode, LogLevel, EncoreInstance, XmlIn)

    GUID = Trim(XmlOut)

    Set objSysproWS = Nothing

    LogonToSysproViaWebServices = GUID

End Function

Private Function CreateSalesOrderWebServices(ByRef GUID As String, ByRef BusinessObject As String, ByRef XmlParameters As String, ByRef XmlIn As String) As String
    Dim XmlOut As String
    Dim objSysproWS As New Syspro_Transaction_Web_Service
    objSysproWS.Setup WebServicesBaseURL

    XmlOut = objSysproWS.Post(GUID, BusinessObject, XmlParameters, XmlIn)

    Set objSysproWS = Nothing

    CreateSalesOrderWebServices = XmlOut

End Function ' CreateSalesOrderWebServices

Place the following code in a Class Module in Excel; call the moduleSyspro_Utilities_Web_Service, or change the above code to match whatever you call it.
'*****************************************************************
' This is based on Microsoft-generated code from:
' http://msdn.microsoft.com/en-us/magazine/cc163837.aspx
' ... but it has been modified to make the URL configurable.
'
'*****************************************************************
'This class was created by the Microsoft Office 2003 Web Services Toolkit.
'
'Description:
'This class is a Visual Basic for Applications class representation of the 'Web service as defined by http://localhost/sysprowebservice/transaction.asmx?wsdl.
'
'To Use:
'Dimension a variable as new clsws_Service, and then write code to
'use the methods provided by the class.
'Example:
' Dim ExampleVar as New clsws_Service
' debug.print ExampleVar.wsm_Post("Sample Input")
'
'For more information, see Complex Types in Microsoft Office 2003
'Web Services Toolkit Help.
'
'Changes to the code in this class may result in incorrect behavior.
'
'*****************************************************************

Option Explicit

'Dimensioning private class variables.
Private sc_Service As SoapClient30

' e.g. Private Const c_WSDL_URL As String = "http://localhost/sysprowebservices/utilities.asmx?WSDL"
' e.g. Private Const c_WSDL_URL As String = "http://www.example.com/sysprowebservices2/utilities.asmx?WSDL"
' The last part of the line MUST be "/utilities.asmx?WSDL".
'Private Const c_WSDL_URL As String = "http://localhost/sysprowebservices/utilities.asmx?WSDL"

Private c_WSDL_URL As String
Private Const c_WSDL_URL_Extension As String = "/utilities.asmx?WSDL"

Private Const c_SERVICE As String = "Service"
Private Const c_PORT As String = "ServiceSoap"
Private Const c_SERVICE_NAMESPACE As String = "http://www.syspro.com/ns/utilities/"


Private Sub Class_Initialize()

End Sub


Public Sub Setup(ByVal c_WSDL_URL_Base As String)
    '*****************************************************************
    'This subroutine will be called each time the class is instantiated.
    'Creates sc_ComplexTypes as new SoapClient30, and then
    'initializes sc_ComplexTypes.mssoapinit2 with WSDL file found in
    'http://localhost/sysprowebservices/transaction.asmx?wsdl.
    '*****************************************************************
    
    Dim str_WSML As String
    ' WSML: Default value is "".
    ' This string is in Web Services Meta Language (WSML).
    ' This is a required parameter only when using custom type mappers.
    str_WSML = ""
    Set sc_Service = New SoapClient30
    ' Set sc_Service = Server.CreateObject("MSSOAP.SoapClient30")
    
    ' Not needed:
    'sc_Service.ClientProperty("ServerHTTPRequest") = True

    c_WSDL_URL = c_WSDL_URL_Base & c_WSDL_URL_Extension

    sc_Service.MSSoapInit (c_WSDL_URL)
    
    ' Doesn't work; reason unknown:
    'sc_Service.MSSoapInit2 c_WSDL_URL, str_WSML, c_SERVICE, c_PORT, c_SERVICE_NAMESPACE
    
    'Use the proxy server defined in Internet Explorer's LAN settings by
    'setting ProxyServer to 
    sc_Service.ConnectorProperty("ProxyServer") = ""

    'Autodetect proxy settings if Internet Explorer is set to autodetect
    'by setting EnableAutoProxy to True
    sc_Service.ConnectorProperty("EnableAutoProxy") = True

End Sub


Private Sub Class_Terminate()
    '*****************************************************************
    'This subroutine will be called each time the class is destructed.
    'Sets sc_ComplexTypes to Nothing.
    '*****************************************************************
    'Error Trap
    On Error GoTo Class_TerminateTrap
    Set sc_Service = Nothing
    Exit Sub

Class_TerminateTrap:
    ServiceErrorHandler ("Class_Terminate")

End Sub


Private Sub ServiceErrorHandler(str_Function As String)
    '*****************************************************************
    'This subroutine is the class error handler. It can be called from any
    'class subroutine or function when that subroutine or function
    'encounters an error. Then, it will raise the error along with the
    'name of the calling subroutine or function.
    '*****************************************************************

    'SOAP Error
    If sc_Service.FaultCode  "" Then
        Err.Raise vbObjectError, str_Function, sc_Service.FaultString

    'Non SOAP Error
    Else
        Err.Raise Err.Number, str_Function, Err.Description
    End If
End Sub


Public Function Logon(ByVal Operator As String, ByVal OperatorPassword As String, ByVal CompanyId As String, ByVal CompanyPassword As String, ByVal LanguageCode As String, ByVal LogLevel As String, ByVal EncoreInstance As String, ByVal XmlIn As String) As String

    '*****************************************************************
    'Proxy function created from
    'http://localhost/sysprowebservice/utilities.asmx?wsdl.
    '
    '"Logon" is defined as XML. See Complex Types: XML Variables
    'in Microsoft Office 2003 Web Services Toolkit Help for details on
    'implementing XML variables.
    '*****************************************************************
    'Error Trap
    On Error GoTo Logon_ErrorHandler
    Logon = sc_Service.Logon(Operator, OperatorPassword, CompanyId, CompanyPassword, LanguageCode, LogLevel, EncoreInstance, XmlIn)
    Exit Function

Logon_ErrorHandler:
    ServiceErrorHandler "Logon"

End Function


Public Function Logoff(ByVal Operator As String) As String

    '*****************************************************************
    'Proxy function created from
    'http://localhost/sysprowebservice/utilities.asmx?wsdl.
    '
    '"Logoff" is defined as XML. See Complex Types: XML Variables
    'in Microsoft Office 2003 Web Services Toolkit Help for details on
    'implementing XML variables.
    '*****************************************************************
    'Error Trap
    On Error GoTo Logoff_ErrorHandler
    Logoff = sc_Service.Logoff(Operator)
    Exit Function

Logoff_ErrorHandler:
    ServiceErrorHandler "Logoff"

End Function

' TODO: Create other routines: GetLogonProfile, Run.
Place the following code in a Class Module in Excel; call the moduleSyspro_Transaction_Web_Service, or change the above code to match whatever you call it.
'*****************************************************************
' Proxy to post to the a web service.
'
' This is based on Microsoft-generated code from:
' http://msdn.microsoft.com/en-us/magazine/cc163837.aspx
' ... but it has been modified to make the URL configurable.
'
'*****************************************************************
'This class was created by the Microsoft Office 2003 Web Services Toolkit.
'
'Description:
'This class is a Visual Basic for Applications class representation of the 'Web service as defined by http://localhost/sysprowebservice/transaction.asmx?wsdl.
'
'To Use:
'Dimension a variable as new clsws_Service, and then write code to
'use the methods provided by the class.
'Example:
' Dim ExampleVar as New clsws_Service
' debug.print ExampleVar.wsm_Post("Sample Input")
'
'For more information, see Complex Types in Microsoft Office 2003
'Web Services Toolkit Help.
'
'Changes to the code in this class may result in incorrect behavior.
'
'*****************************************************************

Option Explicit

'Dimensioning private class variables.
Private sc_Service As SoapClient30

Dim c_WSDL_URL As String
Private Const c_WSDL_URL_Extension As String = "/transaction.asmx?WSDL"

Private Const c_SERVICE As String = "Service"
Private Const c_PORT As String = "ServiceSoap"
Private Const c_SERVICE_NAMESPACE As String = "http://www.syspro.com/ns/transaction/"


Private Sub Class_Initialize()

End Sub


Public Sub Setup(ByVal c_WSDL_URL_Base As String)
    '*****************************************************************
    'This subroutine will be called each time the class is instantiated.
    'Creates sc_ComplexTypes as new SoapClient30, and then
    'initializes sc_ComplexTypes.mssoapinit2 with WSDL file found in
    'http://localhost/sysprowebservices/transaction.asmx?wsdl.
    '*****************************************************************

    Dim str_WSML As String
    str_WSML = ""
    Set sc_Service = New SoapClient30
    ' Set sc_Service = Server.CreateObject("MSSOAP.SoapClient30")
    
    ' Not needed:
    'sc_Service.ClientProperty("ServerHTTPRequest") = True

 c_WSDL_URL = c_WSDL_URL_Base & c_WSDL_URL_Extension

    sc_Service.MSSoapInit (c_WSDL_URL)
    
    ' Doesn't work; reason unknown:
    'sc_Service.MSSoapInit2 c_WSDL_URL, str_WSML, c_SERVICE, c_PORT, c_SERVICE_NAMESPACE

    'Use the proxy server defined in Internet Explorer's LAN settings by
    'setting ProxyServer to 
    sc_Service.ConnectorProperty("ProxyServer") = ""

    'Autodetect proxy settings if Internet Explorer is set to autodetect
    'by setting EnableAutoProxy to True
    sc_Service.ConnectorProperty("EnableAutoProxy") = True

End Sub


Private Sub Class_Terminate()
    '*****************************************************************
    'This subroutine will be called each time the class is destructed.
    'Sets sc_ComplexTypes to Nothing.
    '*****************************************************************
    'Error Trap
    On Error GoTo Class_TerminateTrap
    Set sc_Service = Nothing
    Exit Sub

Class_TerminateTrap:
    ServiceErrorHandler ("Class_Terminate")

End Sub


Private Sub ServiceErrorHandler(str_Function As String)
    '*****************************************************************
    'This subroutine is the class error handler. It can be called from any
    'class subroutine or function when that subroutine or function
    'encounters an error. Then, it will raise the error along with the
    'name of the calling subroutine or function.
    '*****************************************************************

    'SOAP Error
    If sc_Service.FaultCode  "" Then
        Err.Raise vbObjectError, str_Function, sc_Service.FaultString

    'Non SOAP Error
    Else
        Err.Raise Err.Number, str_Function, Err.Description
    End If
End Sub


Public Function Post(ByVal UserId As String, ByVal BusinessObject As String, ByVal XmlParameters As String, ByVal XmlIn As String) As String

    '*****************************************************************
    'Proxy function created from
    'http://localhost/sysprowebservice/transaction.asmx?wsdl.
    '
    '"Post" is defined as XML. See Complex Types: XML Variables
    'in Microsoft Office 2003 Web Services Toolkit Help for details on
    'implementing XML variables.
    '*****************************************************************
    'Error Trap
    On Error GoTo Post_ErrorHandler
    Post = sc_Service.Post(UserId, BusinessObject, XmlParameters, XmlIn)
    Exit Function

Post_ErrorHandler:
    ServiceErrorHandler "Post"

End Function

In Excel, in the Microsoft Visual Basic for Applications window, you will need to go to Tools / References and add these references, if you not using late binding to the Microsoft SOAP library:
  • Microsoft Soap Type Library v3.0
  • Microsoft Soap WinHttp Connector Type Library (v3.0)
  • Microsoft Soap WinInet Connector Type Library (v3.0)

Microsoft’s SOAP libraries have been deprecated

Microsoft recommends you use VSTO (VIsual Studio Tools for Office) going forward.

The SOAP toolkit doesn’t work on Windows 2008 R2 Server

Microsoft’s SOAP libraries have been deprecated, but if you have applications that still want to use SOAP, there are still ways to do that.
Firstly, I found that the Microsoft SOAP libraries still work on Windows XP, Windows 7 (both 32 and 64-bit), but NOT on Windows Server 2008 R2.
They don’t seem to work on Windows Server 2008 R2 because the system can’t find the SOAP DLL’s in the registry, because the system seems to use new, different, or wrong registry keys to locate the SOAP DLL’s:
On Windows Server 2008 R2, the system looked, unsuccessfully, for this registry key:
HKCR\Wow6432Node\CLSID\{the-GUID}\InprocHandler
but on Windows 7 64-bit, where it was successful, it looks for this registry key:
HKCR\Wow6432Node\CLSID\{the-GUID}\InprocServer32.
(This was discovered using Process Monitor to watch registry activity.

How to access SOAP web services from Windows Server 2008 R2

One solution to accessing SOAP is to create a .Net SOAP client (create a project, add a web service to your SOAP endpoint; add a subroutine to call that SOAP endpoint). Then expose that as a COM object so that you can consume your newly created DLL in VBA or where-ever you want.

NOTE for developers

Visual Studio 2010 must be run As Administrator so that you can test the DLL when you run it from Visual Studio as the SoapClient is exposed as a COM Object and needs to be registered when it is built so that it can be found when it is run.
(You can also manually register it using regasm; again you must run as Administrator.)
On the client’s machine, it doesn’t matter where you put the DLL on the client’s machine but the DLL must be registered using regasm.

See Also

  • The Code Snippet in Syspro’s VBScript editor – there’s a code snippet there for calling SOAP.

Thursday, 22 August 2013

How to connect to a Syspro web service

Syspro has a very powerful and comprehensive set of API's exposed as web services. Here is a guide for how to use Syspro's web services.

Firstly, you need to connect to your Syspro web service’s server and get authenticated; in return, you will get a GUID.
To do this, you need to connect to the Logon method of this SOAP service:

Then you will can call an actual API to do something useful. In this example, we will send Syspro a finished goods receipt using the Transaction web service:

To call the Transaction web service, you need to pass these four parameters:

  1. UserId – use the GUID you obtained when you logged into SYSPRO as described above.
  2. BusinessObject – set it to be this string: INVTMR. There are many business objects that you can call via the Transaction web services; consult the Syspro business object documentation on the Syspro Support Zone for what web services you can use. 
  3. XMLParameters – this will be a fairly large string containing an XML document as described below.
  4. XMLIn – this will be a fairly large string containing an XML document; see below for example.

XMLParameters

Here is an example of the XMLParameters string:
<?xml version="1.0" encoding="Windows-1252"?>
<PostInvReceipts xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="INVTMR.XSD">
  <Parameters>
    <TransactionDate>2013-08-20</TransactionDate>
    <ApplyIfEntireDocumentValid>Y</ApplyIfEntireDocumentValid>
    <ValidateOnly>N</ValidateOnly>
    <ReturnDetailedReceipt>N</ReturnDetailedReceipt>
  </Parameters>
</PostInvReceipts>

XMLIn

Here is an example of the XMLIn string:
<?xml version="1.0" encoding="Windows-1252"?>
<PostInvReceipts xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="INVTMRDOC.XSD">
  <Item>
    <Warehouse>FG</Warehouse>
    <StockCode>A100</StockCode>
    <Quantity>1.000</Quantity>
    <Reference>Receipt</Reference>
  </Item>
</PostInvReceipts>

XMLOut

On successfully calling the web service, you should get a result like this:
<?xml version="1.0" encoding="Windows-1252"?>
<postinvreceipts Language='05' Language2='EN' CssStyle='' DecFormat='1' DateFormat='01' Role='01' Version='6.1.038' OperatorPrimaryRole='   '>
<Item>
<ItemNumber>000001</ItemNumber>
</Item>
<Key>
<JnlYear>2013</JnlYear>
<JnlMonth>05</JnlMonth>
<Journal>00032</Journal>
<EntryNumber>00001</EntryNumber>
<Warehouse>FG</Warehouse>
<GlJournal>
<GlYear>2013</GlYear>
<GlPeriod>05</GlPeriod>
<GlJournal>00230</GlJournal>
</GlJournal>
</Key>
<StatusOfItems>
<ItemsProcessed>1</ItemsProcessed>
<ItemsInvalid>0</ItemsInvalid>
</StatusOfItems>
</postinvreceipts>

XSD files

There are XSD files available for each Business Object. The XSD files are distributed with Syspro and found in the <Syspro-installation-directory>\Base\Schemas directory. These XSD files can be used to generate code objects. See this blog for more information on generating code objects from Syspro's XSD files: http://phildetail.blogspot.com.au/2014/04/how-to-use-xsd2code-for-syspros.html

Friday, 22 March 2013

How to configure IIS for Syspro 6.1 Web Services


Introduction

This document describes how to correctly install and configure IIS for use with Syspro Web Services.
Ideally, this should be done before you install Syspro Web Services, but it can be done after; you may just have to check your Application Pool (see details below).

Install IIS

Use this guide if you don't already have IIS installed:
http://www.iis.net/learn/install/installing-iis-7/installing-iis-7-and-above-on-windows-server-2008-or-windows-server-2008-r2

In Windows 7 Professional, you can find these features by going to Start / Control Panel / Turn Windows features on or off (see screen shot below).


Install IIS 6 Management Compatibility

You also need to install the IIS 6 Management Compatibility for Syspro 6.1 with the options as shown in the screen shot below:


How to install the IIS 6 Management Compatibility features on Windows Server 2008

Run Server Manager; go to Server Manager / Roles and scroll down to Role Services.



If the required roles are not installed, click Add Role Services.
Tick the desired roles, i.e. everything under Web Server and Management Tools, as shown in the screen shot below and then click Next to install them.


Configure your Application Pool

Run IIS and go to the SysproWebServices Site.
Click on Basic Settings. Check that the Application Pool is a .Net Framework version 2.0 pool.



If the Application Pools is not a .Net Framework version 2.0 pool, go to Application Pools and add a new one, as shown in the screen shot below.



Check you are using a 32-bit Application Pool

Click on Advanced Settings. Check that 32-bit mode is enabled.


Set Maximum Worker Processes

Also in the Application Pool's Advanced Settings, set the Maximum Worker Processes to something greater than 1; ideally it should be 1 + the normal maximum number of simultaneous business object calls you will need to service.




Permissions

The IIS_IUSRS user must have both Read and Write access to the Syspro60 or Syspro61 directory (and, obviously all those folders and files within).

You should now be ready to run. Restart your web site and test using Syspro's e.Net Diagnostics Manager.

Errors

SoapException

An error you can get when running Web Services, perhaps from E.Net Diagnostics, is:


Error
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: Unable to open and read operator file (ADMOPR.DAT)
   at SysproServices.utilitiesclass.Logon(String Operator, String OperatorPassword, String CompanyId, String CompanyPassword, Language LanguageCode, LogDetail LogLevel, Instance EncoreInstance, String XmlIn)
   --- End of inner exception stack trace ---




For that error, check that the user, IIS_IUSRS, has full access to the Syspro61 directory by going to Windows Explorer, right-click on the Syspro61 installation directory, and check the security: make sure that IIS_IUSRS has full control, as shown in the screen shot below:


Installation Incomplete

When you are installing Syspro Web Services, you may get this error:
"Installation Incomplete. The installer was interrupted before SYSPRO Web Services could be installed. You need to restart the installer to try again."


This error occurs if you haven't installed IIS 6 Management Compatibility on your server. See instructions at the top of this document for how to install IIS 6 Management Compatibility.