Accessing data via ECP with eXTreme for .NET
Tags: intersystems, net, extreme, ecp
Published: 2020-11-04 18:37:33

Some readers of my previous article, [Caché eXTreme for .NET - direct access to globals from C#](https://community.intersystems.com/post/cach%C3%A9-extreme-net-direct-access-globals-c), wondered if you could access information not just from the same instance in which you’re working, but also from another instance on the same computer, or from an instance located on another computer in the same local network. Some theorized, correctly, that this would be possible using the Enterprise Cache Protocol (ECP). In this article I’m going to show how it can be done.

The last I heard, eXTreme for .NET still uses an in-memory connection and runs in the same process as the IRIS instance, though at least [one comment](https://community.intersystems.com/post/does-intersystemsdatacacheclientdll-use-tcpip-net-managed-provider-or-not#comment-20996) suggested the possibility of moving away from in-memory to TCP/IP.

But, even if you can’t use TCP/IP to connect to an instance on another computer, you can use ECP to connect your local database instance to another instance on a different computer in a local network and work with this remote database as if it’s the local one. ECP is a distributed data caching architecture that manages the distribution of data and locks among a heterogeneous network of server systems. In this article, I’m going to take advantage of this architecture to store my data to another instance.

The general idea is that I have a database with my data in one instance of IRIS and this will be my data server. I’m going to call this instance **IRIS**. I’ll also need another instance to which I’ll connect from my eXTreme application, and this will be my application server. I’ll call this instance **IRIST**. In this case, both instances are located on the same computer. (I have only one computer with 64-bit anything, in my case Windows). But this doesn’t change the result—I still need to connect the two different instances in order to access the database of one from the other.

Before doing anything in Visual Studio, however, we need to set up the ECP on both IRIS instances.

First, we’ll enable the ECP service on both the data (IRIS) and application (IRIST) servers. To do this, go to Management Portal > System Administration > Security > Services. In the list of services, select %Service.ECP and on the Edit Service page, click the Service Enabled checkbox, as shown below. Don’t forget to click the Save button.

![](https://lh5.googleusercontent.com/YJI09B0o1buLErmVQUVoRtCi1y-LONGxgOOVaT4v2v_6TV2gGdPTMyrmcoRgsd-AIPJk0tN8r3gIu0OO3xQHZcfz_670UV9wRVkFhtbYlDu0RAyZYWhlZKWBJ1tINTn39gPbdsF4) 

Next, we’ll configure the data server, incrementing the number of allowed application servers from the default of 1. Again, go to Management Portal > System Administration > Configuration > Connectivity > ECP Settings and in the pane on the right, “This System as an ECP Data Server,” change the “Maximum number of application servers” to 2 (or one more than whatever you have in use already). Then click Save and restart the instance for the change to take effect.

![](https://lh3.googleusercontent.com/Ga4HgbDhTUWW9PZAAzU8avjDz4xAS0gPnbt6LrPVgvOT8wR0vTOyhiikbxN8K-hTGxSgwf1DK-SbVzcmk4BSbr7rLFALvu6F_LrCwDeOXHXg8yVnGzmdG4C-wwWruP1pYjupURWO) 

Because I’m lazy and this is just an example, I’ll use the USER database, but you can create a new one or use any database you have on hand (that you won’t worry about if it becomes unreadable).

Next, we’ll configure our application server. The first step is to connect the application server to our data server. Go to Management Portal > System Administration > Configuration > Connectivity > ECP Settings > Data Servers.

![](https://lh3.googleusercontent.com/nedYiBRc6QmFRANrRCy5nzrvPXcwuN5smIJhfKqKCP8ucixZhrhPu2bcRS7p2hpxWxs5_yS7p2MtmKV0pL-6aS7zvXRl0ymzkL5tePiKMSAKP3aEWx_5o1ZiXiW_OSgIRsYMAgLx) 

Then click the Add Server button and fill in the information about your data server. In my case, when I was setting up my data server, I didn’t choose the option Enabled or Required from the radiogroup “ECP SSL/TLS support,” so I don’t check “Use SSL/TLS.”

![](https://lh6.googleusercontent.com/Febacf76DKafxyDpu7nYXFpuyF6X-pqah40KKoLJDEOyOiEKrSS_GYwRxOChRBCdycdOblpVZv1jilqqfGBjYg4Hz7yymVC9jixWfqh_4EWCRjp8dOoCERdnkCT6IHI7b637iyRW) 

If you’re using another computer, you need to provide its name or IP address in the Server Name field.

![](https://lh5.googleusercontent.com/OH4EGKWkebKlbYswZoANzNXBXwF6PC5lJJ84w1sXWouTCIBXhHbjM_er98mbmRShZJA4Oi6DPAnZtcJzziECqp6mUZgJKyH2ptymKdO1ksHF1bnRYPZSitoLb_n6KDLmxHh8PWqu) 

Click Save and you’ll see a new entry in the list of ECP data servers. In my case, the Status is Normal, which means that the connection is operating as it should. You can read more on [ECP Connection States](https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=GSCALE_ecp_monitor_states) in the documentation.

![](https://lh6.googleusercontent.com/G3ig2M790wcLkP-4Hiw-fkcKxbaA_mBElmqoDgKa7LN2u4sy3QOZOxLuR9NdE3506-qKaW4vAAPMAn_TQFww4Q_3Z2lhr9PjgoBwx2gz3TuYeOGnuf619ph3-NP5FdjF4DTF6Hj5) 

If you’re connecting instances on different computers on the (local) network, you need to make sure your port (51773, in my case) is open. Check your configuration of firewalls, antivirus, and so forth.

Once the link is established, we need to map our remote database (USER, from IRIS) to our local application server (IRIST). To do this, we need to create a new database, so go to Management Portal > System Administration > Configuration > System Configuration > Namespaces and click the Create New Namespace button. Give the namespace a name and select “Remote Database” for the options, indicating the default database for Globals and for Routines.

![](https://lh4.googleusercontent.com/uayV3uNLQcVkey4Oe4G7_yc-Z8ledsSNDqBoCmX9CXgLM9G6-1OjcFEftWL0tP47R8Q_hLW9SDbVtW3H8OBYM1WVPixcXELxpCpn4lWb_9ezxs3DciF9AJFvQsgOYLMsYL5J-SAg) 

Now you need to create this remote database. To do so, click the “Create New Database…” button. Choose the remote server you created in the previous step from the dropdown list.

![](https://lh5.googleusercontent.com/MYOEHexh7I_dB3qOs8JIwfB6d644ldbJjRtOwTsfBICf0_WdEaZcLNcplSYEG56gQEOfzL-AJBmPiKlUIKcvfnHPe3Bl-DZqq7DkN3_GJYxa30Pn_rVjhw8qfqK13leMcdcniLFT) 

Then choose the directory of that instance.

![](https://lh5.googleusercontent.com/ABeAY_jrslmK6uDW8D4Oq_I4L0t5Jxt5M6MxwBxLnr3uq7v7pHmtKsq0CCcE4jb49iTzC9usnaLyiejlFxdGiU4dvJAiSXZTs9oNSTUi1nws7KxgayIsrZ9qwSVcJSjnuM2-N_5s) 

And come up with a new Database Name.

![](https://lh3.googleusercontent.com/rznbcBdOgvQvUxOIsUF0JVE8BOQdAFWesfGsV_5xHYT8QaJ1CdfgUw0ZbrCP94cGCTK76KkaXAx1yMrNKFanVVKQKzmRurXvoSdQNnBCAUYKa4Z8TQvFfiXEWUaaLNjgoc8bmp4u) 

Click Finish and don’t forget to choose the same database for Routines. Clear the checkbox “Enable namespace for interoperability productions,” click Save at the top of the page, and you’re all set.

Before we start working with our globals from C#, let’s check that everything is working and that we have access to the data stored in another instance. To do this, you can, for example, open a terminal on the application server (IRIST), switch the current namespace to the one you created, and set the value of some global.

![](https://lh3.googleusercontent.com/KOFe3YohAHGyusrk0aM-SAzKbT_BLmR07Jk2sGQTwrx9k1S98Mi5CvhiYX5vMpnIOaxKMRYmlb96LmmV492acYfDff33JQWAmvweCs4_DGzaKgfqOW49w1BZLBDut96EAM0Fqlq1) 

Then, in a terminal on the data server, you should be able to read this value.

![](https://lh4.googleusercontent.com/dXWBqUO_hC-w4n0hVLm2dMVyzD1ZyOulzI2mDuHYk_Prv9UgDp1ptmTsUZVti05PJcKZ1glcgBwBMJ37S3FYMw1WC6EMPYuOBdNKZ-0R8IQHkSJzbu2XgMy-WXKE0qvtWJy_NjdI) 

We get the correct value, which means we can proceed with a test application in C# and use eXTreme technology to work with data stored in our data server while having an in-memory connection to our application server.

First, let me remind you about the adjustments that need to be made for it all to work. To start, environmental variables for Windows must be set:

  * GLOBALS_HOME should contain the full path to the directory where you put the DBMS. In my case, this is C:\InterSystems\IRIST\.
  * PATH must contain the full path to the Bin directory. In my case, this is C:\InterSystems\IRIST\Bin\.

With both of my instances on the same computer, I have to set the path to my application server instance in the environmental variables, because nowhere in my application code am I directly specifying the instance I’ll be working with.

By the way, if you just set the variables, you’ll need to log into your Windows user instance again for the changes to take effect.

Now let’s open the existing eXTreme application from my [previous article](https://github.com/Gra-ach/eXTreme-example-for-IRIS). To make sure it will work, check that your license allows such connections. Also, see if the version of .NET you chose for your project is the same as the version referenced in your eXTreme library.

Before switching to the remote database, I decided to check if my program still works.

![](https://lh5.googleusercontent.com/s-Khc0MxZhnHmV9DOD0mm3VKVwCCcHJPfRKIRYSEB30wNhA1IDkfcPQxMIKU14vhS-Cq9e9TquidKusBaEX9E93rV88HAQemnv5kwe72UWna0QYJ69jEVE0HvvIxdWhCtjb0TZRI) 

And it works perfectly. I can see my globals in the database. For now, they’re in the local database of the IRIST instance.

![](https://lh5.googleusercontent.com/tSI6QLcZeH6yEZG0mvdX5r69ozKIYfzFFcXqkzLJcLvnohQgR1dcYRhmSASpyslZayVVHWeJmTC2EsBeA8fSF9HJ8KEeoViWaYCRHuSGnEdTXhOS_tP6kU1n2tFf0TfDrg-0vbvB) 

Now all I need to do to switch to my remote database is change the namespace when I’m connecting to the local instance, IRIST.

So instead of using the following code:

    Connection myConn = ConnectionContext.GetConnection();
    myConn.Connect("USER", "_SYSTEM", "SYS");

I’ll use:

    Connection myConn = ConnectionContext.GetConnection();
    myConn.Connect("IRIS_USER", "_SYSTEM", "SYS");

Thus, I’ll be accessing the namespace IRIS_USER from local application server instance IRIST, which is mapped to a remote database of the data server instance IRIS. The output is the same:

![](https://lh4.googleusercontent.com/tBFyynQpUYTEtFIFW04MAgy1Zr297mkwRKCwZvz2d5SvegDKwwliwEPNfWnBw2NMukQbMNZX1W9uMXZMTvbe-TFOhXpLfLRp3Eiq7PNprBfnI0zkmXH5Nwt-3kMBqwW78P0Qu4HL) 

And we can see this global in the IRIS instance:

![](https://lh3.googleusercontent.com/hH19J3QzjbEZdk7-0vORBWWwyi_2-zbuzjkKGHn85d6K0XO77-Qp6SyxUj6P2NXCt9zNz-ocTRhuCxo-auqIcKKM3PsMLoWoMA10t_u-IGyUobjcolryrQ0m2DRzOpUXsnM8Il1j) 

And through ECP:

![](https://lh5.googleusercontent.com/WOGuUOCNxOFTSTM_p1-Kh34uCJQSDMbnvj1HFY9ctzd2ub7w4IMWyiu59Cua2eGF8PyeCC_d0Gs1AlDjmAkjqcdHeUgrTXMwKdXYL5Vln9Ie2GDHoXAVzzGe3R7EpnLRLGJfTpMJ) 

Mission accomplished! You can use the InterSystems.CacheExtreme.dll library, which you can find in ..\bin\debug\ folder of the example, to connect to IRIS. And you can use ECP to connect to a remote database from an eXTreme application.

You can read more about setting up the ECP environment in [First Look: Scaling Systems for User Volume with InterSystems Distributed Caching](https://docs.intersystems.com/irislatest/csp/docbook/Doc.View.cls?KEY=AFL_ecp#AFL_ecp_explore). The sample project in this article is the same as in the previous one and is on [GitHub](https://github.com/Gra-ach/eXTreme-example-for-IRIS).