📜 ⬆️ ⬇️

FastReport.Mono. Part 1: Run the Web demo report

Due to the fact that the official documentation of FastReport.Mono is outdated, and it is not even clear which version of the Debian distribution it belongs to (I can assume that it is 6 or younger), I decided to write a more or less sane installation instructions for the demo Web interface reports.

The problem arose because of the looming prospect of moving one of the projects where FastReport.Net was used, from Windows + Oracle to Linux + PostgreSQL. In addition, I was interested in the possibility of implementing everything in the form of microservices in Docker containers. Alas - “it was smooth on paper, but they forgot about the ravines”: I had to face a number of nuances, which I will try to describe in more or less detail and suggest possible solutions to problems.

In the process I took notes and reminders for myself in the project wiki, and only then I tried to systematize everything. The result was a lot of text, I will try to divide the publication, roughly, into three parts:
')

NB The whole following text is not a step by step guide to action, but possible solutions to problems (sometimes more than one method). By this, I advise you to immediately read the entire material, and then choose the appropriate method of installation.

Introduction


And so, the first thing I would like to draw attention to is the package names and dependency problems. If you plan to use Debian 8 (Jessie), then be prepared for the need to install Mono from the repository of the Mono-project or even build from source. The official Jessie repositories lack part of the packages, for example libapache2-mod-mono. I didn’t do this, so all the research was done on Debian 9 (Stretch). I strongly do not recommend installing the package with examples ASP.Net - asp.net-examples, since during the installation, an inclug will be added to the Apache config containing the same Mono settings for all virtual hosts, as well as a universal alias / samples will be added.

Second, when using the Apache virtual host configuration file from a publication in the official developers blog, note that it is written for Apache 2.2 and does not quite fit Apache 2.4. The problem lies in changing the configuration of access rights to directories that appeared in version 2.4: there is no Require all granted in the old config in the Directory section. In addition, some changes in the Mono settings are possible: something can be omitted because corresponds to the values ​​of "default", but something may not suit you. In principle, to run a demo project, you can omit almost everything. And, of course, depending on the selected configuration, setting environment variables or access rights to files and directories.

The third, and very interesting point, is that for correct work, you must either change the directory structure, not doing stupid copy-paste lines from the example, but after studying another post (why the authors did not combine two entries into one - a mystery to me). Or set the environment variable MONO_PATH so that it looks in the directory with the files FastReport. *. Dll and fix the web.config.

The fourth, and most controversial and interesting point - in some cases, XRX is needed for FastReport.Web to work. For example, a sample barcode project requires System.Windows.Forms. And, if there are no special problems when starting the server on behalf of the user logged in via the GUI, then in case of launching the project via SSH, Apache or the Docker container will need to set environmental variables, and in the case of containers, the X override from the outside.

The fifth point will be considered the process of assembling connectors for some popular DBMS. This is due to the fact that with FastReport (both Windows and Mono versions), the plugins for using the DBMS are delivered in the form of source texts, and not ready-to-use libraries. Will have to collect by hand.

But first things first. I assume that the system is already installed, and it is not completely empty - it contains your favorite text editor and archiver.

Initial setting


If you believe the official manual, the installation should not take a lot of time in everything should go smoothly through apt. Well, let's get started. Install Mono, an XSP web server, an Apache web server, and a module that allows you to run ASP.Net applications through Apache.

apt-get install mono-complete mono-xsp apache2 libapache2-mod-mono 

Next, download and unpack the demo project. Hereinafter, I will specifically use paths other than / var / www. First of all, it allows you to find errors in the configuration or even in the project itself. And secondly, the placement of third-party software products in / opt corresponds to the intended purpose of this directory.

 mkdir –p /opt/fastreport/htdocs #  wget https://www.fastreport.ru/public_download/frmono_demo.zip -O /tmp/frmono_demo.zip # , ,  unzip /tmp/frmono_demo.zip –d /tmp/frmono.demo cp –rp /tmp/frmono.demo/Demos/C#/Web/* /opt/fastreport/htdocs cd /opt/fastreport/htdocs xsp 


Figure 1. Starting XSP

XSP is a lightweight web server for APS.Net, written in C # and allows you to quickly and easily test simple applications. If you believe the documentation and advice of experienced - for use in the combat environment is not recommended. By default, XSP listens on port 9000 and all network adapters (IP 0.0.0.0). This procedure will show us if anything works in principle. I can assume that you will get any exception. Alas, the authors of this seemingly interesting tool for generating reports did not take care of the convenience of installing demonstration materials.


Fig. 2. Possible error message (clickable image)

This problem is due to the fact that Mono does not find the FastReport.Web.dll library. Moreover, the error occurs even if the file is copied to htdocs. I am not an expert in .NET, and even more so in Mono. And I didn’t read the link to the blog post of the authors that I gave in the introduction immediately (by the way, there is a question - why is there no emphasis on this, for example, the file names are highlighted in bold, but not the directory?). By this, I had to break my head. The first possible solution is to copy the DLL to the current directory and set the MONO_PATH environment variable :

 cp /tmp/frmono.demo/FastReport.*.dll ./ export MONO_PATH=./ 

But here, too, waiting for failure. Most likely, you received an exception that the namespace or type FastReport.Utils was not found. The problem can be solved - just register FastReport.Mono.dll in the web.config.


Fig. 3. Compilation Error - type or namespace Utils was not found (the image is clickable)

Add a dependency - in the web.config section, write the following line:

 <add assembly="FastReport.Mono, Version=2017.1.8.0, Culture=neutral, PublicKeyToken=db7e5ce63278458c"/> 

Version number and token may be different, at the moment they are relevant. You can get token from dll like this:

 sn -T FastReport.Mono.dll 

You can check the version number as follows:

 monodis --assembly FastReport.Mono.dll | grep "Version" 

The result of all our torment should be a slightly sane display of the page with a demo report.


Fig. 4. Not quite a working example.

Alas, something went wrong. It was assumed that the browser window will have barcodes, as in the screenshot in the official manual FastReport.Mono. You may not have this error, I will explain the reasons for its occurrence and the method of elimination just below.

In the meantime, back to the libraries. In the process of writing instructions, another way to solve the problem of connecting FastReport.Web.dll and FastReport.Mono.dll (Figures 2 , 3 ) was found, although, probably, the .NET developers would have done so immediately. It is enough to create the htdocs / bin directory and move FastReport.Web.dll and FastReport.Mono.dll there. There is no need to edit web.config and change MONO_PATH, Mono finds everything and throws no exceptions. Good or bad - hard to say. In principle, if everything is hidden behind the front end in the face of Apache, then the directory with dll-kami can be covered from prying eyes. The directory structure for this solution is shown in Figure 5.


Fig. 5. Demonstration project directory tree

Using System.Windows.Forms


The Web demo report in the list of dependencies contains System.Windows.Forms. If you looked in web.config, then I think you paid attention to the line:

 <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 

If you encounter a problem depicted in screenshot 4 or saw the message “No protocol specified” in the console, then one of the following options takes place:


In the first case, you need to install / run X. Options 2 and 3 are inherently similar, and the problem is solved by setting the environment variables DISPLAY , and possibly XAUTHORITY . The only difference is that if the OS was initially installed with a graphical environment, the user has the XAUTHORITY variable set and looks at the ~ / .Xauthority file, but the root doesn't have it. Basically, it’s enough to do the following before launching XSP:

 export DISPLAY=:0 

For root, add:

 export XAUTHORITY=/var/run/lightdm/root/:0 

In fact, running something on behalf of the superuser is a bad idea, but if you really really want to ... In addition, setting the XAUTHORITY variable is still useful when configuring Apache to allow the www-data user to use the X server (relevant if during the Debian installation packages were selected graphic environment). If the OS was initially installed in the minimum version, everything will be somewhat different, because Xorg may well be installed without a display manager. Or not installed at all.

If you are faced with the need to “wrap” FastReport.Mono into a container and decide to follow the principle “1 service - 1 container”, then you will have to create a container with the X-server and “forward” the socket between the containers. As a video driver, you'll have to use the Dummy display driver. This setting will be discussed in one of the following parts of the manual.

 apt-get install xserver-xorg-video-dummy 

An alternative option for Linux / Unix users could be a “forwarding” of the X server from the host OS (tcp or unix socket). To obtain this feature, you must install XQuartz on MacOS.

Apache setup


In principle, configuring Apache 2.4 and mod_mono does not require any special tricks. If you didn’t follow my advice and installed the asp.net-examples package, I recommend mod_mono commenting out the following line in
/etc/apache2/mods-available/mod_mono.conf:

 Include /etc/mono-server4/mono-server4-hosts.conf 

Otherwise, the alias / samples will be nailed to all your virtual hosts, according to which the very examples of using ASP.Net will be opened. In addition, Apache may in the logs and when restarting swear that mod_mono is already loaded. This is due to the fact that two modules are loaded - mod_mono and mod_mono_auto (for automatic deployment of applications). Mod_mono_auto can be safely disabled:

 a2dismod mod_mono_auto && service apache2 restart 

The virtual host configuration file from the official documentation is redundant in settings and in some places is not relevant. The minimum required config can be viewed under the spoiler.

001-mono.conf
 Listen 8081 <VirtualHost *:8081> DocumentRoot "/opt/fastreport/htdocs" <IfModule mod_mono.c> MonoUnixSocket FrSite /tmp/.mod_mono_server MonoServerPath FrSite /usr/bin/mod-mono-server4 #       htdocs/bin -      #      /opt/fastreport/htdocs     #    FastReport.*.dll MonoPath FrSite /usr/lib/mono/4.5:/usr/lib:/usr/lib/mono/4.0:/opt/fastreport/htdocs AddMonoApplications FrSite "/:/opt/fastreport/htdocs" MonoAutoApplication Disabled MonoDocumentRootDir /opt/fastreport/htdocs MonoDebug false MonoSetEnv FrSite DISPLAY=:0;HOME=/opt/fastreport AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd </IfModule> <Directory "/opt/fastreport/htdocs"> Require all granted Options Indexes FollowSymLinks MultiViews AllowOverride All <IfModule mod_mono.c> SetHandler mono MonoSetServerAlias FrSite DirectoryIndex Default.aspx </IfModule> </Directory> #     bin   dll #      <Directory "/opt/fastreport/htdocs/bin"> Require all denied </Directory> </VirtualHost> 

Actually, we put everything into /etc/apache2/sites-available/001-mono.conf with the help of our favorite text editor, turn on the site and restart Apache:

 a2ensite 001-mono && service apache2 restart 

Parse the entire config does not make deep sense, but on one line I would like to dwell upon:

 MonoSetEnv FrSite DISPLAY=:0;HOME=/opt/fastreport 

This is a very notable crutch that allows you to solve 2 problems at once. I have already described one of them in the section devoted to System.Windows.Forms - the application needs to specify DISPLAY. The second point is related to the fact that during the compilation and execution of the application, the ~ / .local and ~ / .mono directories are used, and the user www-data, from which Apache is running, has a home directory of / var / www. I thought it was not worth littering the whole system. In principle, it works. Of course, you could try to change the user and group for this particular virtual host, but it did not take off. As I understand it, mod_mono starts the server from an Apache user and when trying to use mpm_itk it did not work, because The Apache process does not have access to the Mono UNIX socket of the server. It is possible that the problem can be solved using FastCGI, but did not try, I do not pretend to be true.

The final touch is to give www-data user access to the X-server. And here we can use the ability to install XAUTHORITY for the superuser.

 export XAUTHORITY=/var/run/lightdm/root/:0 export DISPLAY=:0 xhost + local:www-data service apache2 restart 

I would like to draw attention to the fact that the changes made when calling xhost will be active only until shutdown / reboot. If you use lightdm, then it makes sense to wrap this construct in a script and set its autorun by adding the line display-setup-script = / path / to / script.sh in /etc/lightdm/lightdm.conf. Alternatively, you can change / etc / X11 / xinit / xinitrc.


Fig. 6. A workable example through Apache 2.4

Source: https://habr.com/ru/post/351976/


All Articles