Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica .Net/Link in an Asp.Net application

I am using the Mathematica .Net/Link platform to create a web service to format and calculate math problems. However I am unable to get it working.

I create it using this code:

_Log.IpDebug("Starting the Kernel Link");
if (string.IsNullOrEmpty(_MathLinkArguments))
   _InternelKernel = MathLinkFactory.CreateKernelLink();
else
   _InternelKernel = MathLinkFactory.CreateKernelLink(_MathLinkArguments);
_Log.IpDebug("Kernel Link Started");
_InternelKernel.WaitAndDiscardAnswer();

The value of _MathLinkArguments is -linkmode launch -linkname \"C:\\Program Files\\Wolfram Research\\Mathematica\\7.0\\Math.exe\".

This piece of code is called from the Application_Start method of the global.asax.cs file.

When it gets to the WaitAndDiscardAnswer() call it gives the server error:

Error code: 11. Connected MathLink program has closed the link, but there might still be data underway. 

Note: The SampleCode given with the .NET/Link package (both a console app and a WinForms app) works.

Edit: I copied the console app sample code given with Mathematica into an asp.net page and it gave me the same error the first load and then on subsequent loads it gave me:

Error code: 1. MathLink connection was lost.

Edit2: I forgot to mention that when I have procmon and task manager open while running my app, I can tell that Math.exe starts but it immediately exits, which makes those error code make complete sense...but doesn't explain why that happened.

like image 201
James J. Regan IV Avatar asked Mar 04 '11 20:03

James J. Regan IV


1 Answers

To allow the .Net/Link to work in Asp.net (at least in IIS 7.5) you need to enable the property loadUserProfile on the app pool for the web site.

I am not entirely sure why this is the case, but from what I found while trying to debug this, there are some things that are gotten from the user's profile. I know for a fact that the default location of the kernel is, which explains why I couldn't use it with no arguments, and so I can only assume that other things are needed as well and without the profile it couldn't determine that.

But whatever the reason is this is required, it is, or at least it is a fix if you are getting similar problems like this in your own application.

like image 69
James J. Regan IV Avatar answered Sep 29 '22 03:09

James J. Regan IV