Problem adding reference to external C# .dll  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Problem adding reference to external C# .dll

Postby riverTrader » 03 Aug 2012

I might be remedial here; I’m trying to add a reference to an external c# .dll (a set of proprietary trading algorithms I have created) – and I cannot seem to get mc.net to recognize it.

I have created a small test C# library project, compiled the .dll and copied it to the “C:\Program Files (x86)\TS Support\MultiCharts .NET” directory
I have successfully added a reference to the .dll in VS
I have created a test study (copied from movingAvg_1Line) and added a using reference to it in VS
The PlStudies project compiles in VS, including the test indicator class
When I open the project in the mc.net editor the indicator will not compile
The error:
“The type or namespace name 'Wild' could not be found (are you missing a using directive or an assembly reference?)

If I comment out the ‘using’ reference the study compiles in VS and shows green(ready) in mc.net editor

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Problem adding reference to external C# .dll

Postby riverTrader » 04 Aug 2012

another data point:

I can only add a reference to a .net .dll (Wild.MxInterop.dll) from within VS
I add the reference, save, exit, reload in VS, all is fine, builds, explorer shows the ref

If I then exit VS, open in mc.net editor, compile and exig mc.net editor
When I re-open the project in VS, the reference to the .dll has been deleted

inquiring minds want to know

do I need to sign it and add the assembly to the GAC?
do I need to register it with mc.net in some way?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Problem adding reference to external C# .dll

Postby JoshM » 05 Aug 2012

If I then exit VS, open in mc.net editor, compile and exig mc.net editor
When I re-open the project in VS, the reference to the .dll has been deleted
Have you tried to add a reference in the PowerLanguage .NET editor? (Right-click in editor opened file --> References) I suppose that those should remain persistent, while those in the VS solution get overwritten by MC .NET when it detects a change.

(Just a speculation from my part)

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Problem adding reference to external C# .dll

Postby riverTrader » 05 Aug 2012

I’ve made some progress on adding an external .dll reference to mc.net but still no dice.
Mc.Net exhibits strange behaviors leading me to believe there is a bug, or multiple bugs in the mc.net handling of the referencing

Strange behaviors:

1. If I add a reference to the VS PlStudies8 project in VS it does not show up in mc.net editor
2. If I add a reference to the .dll in the mc.net editor the script will compile with the reference to the MxChartsIO.dll
3. If I only add the reference to the mc.net editor (do not add ref in VS first) the reference does not appear in VS
4. If I add a reference to the dll in VS BEFORE I have added the reference in the mc.net editor, when I close and then reopen VS the reference (in VS) is lost.
5. If I add a reference to the dll in VS AFTER I have successfully added the reference in the mc.net editor, when I close and then reopen VS the reference remains

Intellisense:
1. After I have successfully compiled the MxChartsIO.dll
a. I exit VS, mc.net, mc.net editor
b. I restart mc.net, mc.net editor
c. I open a test indicator; the indicator compiles correctly with the using MxChartsIO directive;
d. When I edit in the mc.net editor, the editor responds with intellisense correctly
e. When I add a new method to the mc.net script referencing MxChartsIO the script compiles correctly.
2. I now open VS and edit the MxChartsIO.dll to add a new public method
a. I recompile in VS
b. When I go to the mc.net editor the C# script compiles
c. Intellisense does NOT respond correctly (does not find the new method)
d. I can type in the proper syntax for the new method and the mc.net editor will compile
e. If I close and then reopen mc.net intellisense works correctly.
3. As long as the syntax is correct the mc.net script compiles correctly in both VS and the mc.net editor

RunTime
1. I open mc.net and attempt to insert the study I have created
a. I get the following error message from mc.net

Stranger Still:
1. Now, oddly enough when I tried the entire exercise with a single public method (public bool test()) in the .dll
a. The script compiles and runs correctly in mc.net.
2. When I tried the exercise with nothing but a single public method returning a double (public bool testDouble())
a. The script compiles but fails in mc.net.
3. When I retried the exercise, this time with all the test methods in the attached project
a. The script compiles but fails in mc.net.
4. The error message is the same in all the failed examples above.

Reproduction:

1 I created a simple test dll: MxChartsIO.dll
a. It exposes several public methods
i. testBool()
ii. testRef()
iii. testOut()
iv. testDouble()
v. testRandomDouble()
vi. testRandomDouble(double close)
vii. testTryCatch()
viii. testThrow()
4. I have created a simple test mc.net C# script referencing the .dll
a. It is a simple modification of Mov_Avg_1_Line
b. It attempts to call the MxChartsIO methods in the CalcBar() method.

The modified mc.net script calling mxio methods:

Code: Select all

protected override void CalcBar(){
bool testBool = mxio.testBool();
double testDouble = mxio.testDouble();
double testRandomDouble = mxio.testRandomDouble();
double testDoubleClose = mxio.testDouble(price.Value);
double testDoubleRandomClose = mxio.testRandomDouble(price.Value);
bool testNewBool = mxio.testNewBool();

double inDouble = 1325.25;
bool testRef = mxio.testRef(ref inDouble);

double outDouble;
bool testOut = mxio.testOut(out outDouble);

//bool testTryCatch = mxio.testTryCatch();
//bool testTryThrow = mxio.testThrow();
The MxChartsIO.dll source:

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Wild.MxInterOp {
public class MxChartsIO {

Random rand = new Random();
public MxChartsIO() {
testTryCatch();
}

public bool testBool() {
return true;
}
public bool testRef(ref double inDouble) {
return true;
}
public bool testOut(out double outDouble) {
outDouble = 33;
return true;
}

public double testDouble() {
return 1370;
}

public double testDouble(double close) {
double close2 = (close + (close / 10));
return close2;
}

public double testRandomDouble() {
return (double)rand.Next(1350, 1390);
}

public double testRandomDouble(double close) {
int close2 = (int)(close + (close / 10));
return (double)rand.Next((int)close, close2);
}

public bool testTryCatch(){
try {
throw new NotImplementedException();
}
catch (Exception ex) {
Debug.WriteLine(ex.ToString()); if (Debugger.IsAttached) Debugger.Break();
return false;
}
}
public bool testThrow() {
throw new NotImplementedException();
}

public bool testNewBool(){
return true;
}

}


}
Attachments
Wild.MxInterOp.zip
(24.94 KiB) Downloaded 409 times

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Problem adding reference to external C# .dll

Postby riverTrader » 05 Aug 2012

BTW I did post this to the project management site as a bug. I still didn't find a category for mc.net so I posted it under other/other.

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Problem adding reference to external C# .dll

Postby riverTrader » 06 Aug 2012

here is the url to the issue reported in project manager:

https://www.multicharts.com/pm/viewissu ... no=MC-1079

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Problem adding reference to external C# .dll

Postby Henry MultiСharts » 06 Aug 2012

Hello riverTrader,

All references should be added through MultiCharts .Net Editor only. Once you do that and compile the study you can modify and debug your code in Visual Studio. Please try this solution and let me know how it goes.

bluejack
Posts: 42
Joined: 02 Aug 2012
Has thanked: 25 times
Been thanked: 27 times

Re: Problem adding reference to external C# .dll

Postby bluejack » 06 Aug 2012

@rivertrader:
Seems you have to add the reference from inside the PowerLanguage .NET Editor.

Rightclick into script => References...

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Problem adding reference to external C# .dll

Postby riverTrader » 06 Aug 2012

Naw, Bluejack, read the post. I added from inside the mc.net editor; no dice.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Problem adding reference to external C# .dll

Postby JoshM » 07 Aug 2012

Have you tried to add a reference in the PowerLanguage .NET editor? (Right-click in editor opened file --> References) I suppose that those should remain persistent, while those in the VS solution get overwritten by MC .NET when it detects a change.

(Just a speculation from my part)
My 'speculation' above seems to work fine for me - I have no trouble adding a DLL to MC .NET. Perhaps my beginner's mind was an advantage in this regard. :)

Here's what I done:

1) Create a class library (NET 3.5 client) in VS, compiled, and placed the DLL on my desktop.

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestDLLAug12
{
public class MathClass
{
public double TotalSum(double firstDouble, double secondDouble)
{
return (firstDouble + secondDouble);
}
}
}
2) Started the PowerLanguage .NET Editor, opened the file to which I wanted to add the reference, and right-clicked References -> Add Reference, and browsed to the desktop.

3) Opened the PLStudies solution in VS. The reference didn't show up yet:
Image

So, right-clicked References in the Solution Navigator --> Add References --> Browse --> Navigated to DLL file.

4) Incorporated the DLL contents in a test indicator:

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using TestDLLAug12;

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class MyTestIndicator : IndicatorObject
{
public MyTestIndicator(object _ctx) : base(_ctx) { }

private MathClass myMath = new MathClass();
private double totalSum;
private double firstSum, secondSum;

private UserDefinedMethods myMethods;

protected override void Create()
{
myMethods = new UserDefinedMethods(this);

Output.Clear();
}

protected override void StartCalc() { }

protected override void CalcBar()
{
firstSum = Bars.CloseValue;
secondSum = Bars.CloseValue * 10;
totalSum = myMath.TotalSum(firstSum, secondSum);

myMethods.Print(string.Format("DLL's TotalSum: {0} + {1} = {2}",
firstSum, secondSum, totalSum));
}
}
}
5) Saved the file, the save was picked up by MC .NET, and added the indicator to the chart. The following output was generated:

Code: Select all

12:13:26. 927 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 18 | DLL's TotalSum: 1,56386 + 15,6386 = 17,20246
12:13:47. 676 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 19 | DLL's TotalSum: 1,56389 + 15,6389 = 17,20279
12:13:49. 223 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 20 | DLL's TotalSum: 1,56387 + 15,6387 = 17,20257
12:13:51. 921 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 21 | DLL's TotalSum: 1,56386 + 15,6386 = 17,20246
12:13:54. 181 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 22 | DLL's TotalSum: 1,56383 + 15,6383 = 17,20213
12:13:55. 430 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 23 | DLL's TotalSum: 1,56381 + 15,6381 = 17,20191
12:13:55. 430 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 24 | DLL's TotalSum: 1,56379 + 15,6379 = 17,20169
12:13:56. 775 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 25 | DLL's TotalSum: 1,56378 + 15,6378 = 17,20158
12:14:21. 920 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 26 | DLL's TotalSum: 1,56382 + 15,6382 = 17,20202
12:14:24. 071 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 27 | DLL's TotalSum: 1,56384 + 15,6384 = 17,20224
12:14:25. 273 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 28 | DLL's TotalSum: 1,56386 + 15,6386 = 17,20246
12:14:27. 831 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 29 | DLL's TotalSum: 1,56385 + 15,6385 = 17,20235
12:14:29. 392 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 30 | DLL's TotalSum: 1,5638 + 15,638 = 17,2018
12:14:30. 232 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 31 | DLL's TotalSum: 1,56384 + 15,6384 = 17,20224
12:14:36. 584 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 32 | DLL's TotalSum: 1,56383 + 15,6383 = 17,20213
12:14:39. 534 | GBP/USD - MyTestIndicator | #1017 - 07-08-12 12:16:00 | BarTicks 33 | DLL's TotalSum: 1,56384 + 15,6384 = 17,20224
Looks like its working. :)

Important note:
I've added the reference to the MyTestIndicator indicator. If I right-click this file in the MC .NET editor and choose References, I can see the reference.

However, if I open another indicator (say ADX), the reference doesn't show up:
Image

But, the IntelliSense does pick up the reference even though it isn't added in the references list (in other words, it is there, just 'hidden'):

Image

However, even though it doesn't show up, it does remain there after compiling. In other words, if I press Compile in the MC .NET editor, the reference isn't removed from the Visual Studio solution file.


Perhaps this behavior gave the impression that adding references doesn't work? At least, it seems to be working now - but I'm not that knowledgeable in C# to make a definite judgement on that.

Edit: After closing MultiCharts .NET, the .NET editor, and VS, and restarting them all, the reference was still there.

After creating a new function, which then got added to the VS solution file, VS reloaded the new version of the solution, and the reference was still there.
Attachments
scr.07-08-2012 10.51.35.png
(8.79 KiB) Downloaded 3327 times
scr.07-08-2012 10.50.20.png
(9 KiB) Downloaded 3250 times
scr.07-08-2012 10.25.03.png
(62.1 KiB) Downloaded 3298 times

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Problem adding reference to external C# .dll

Postby Henry MultiСharts » 07 Aug 2012

I've added the reference to the MyTestIndicator indicator. If I right-click this file in the MC .NET editor and choose References, I can see the reference.

However, if I open another indicator (say ADX), the reference doesn't show up:


But, the IntelliSense does pick up the reference even though it isn't added in the references list (in other words, it is there, just 'hidden')
That is correct. The references you have added are global and available for all studies.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Problem adding reference to external C# .dll

Postby Henry MultiСharts » 07 Aug 2012

I’ve made some progress on adding an external .dll reference to mc.net but still no dice.
Mc.Net exhibits strange behaviors leading me to believe there is a bug, or multiple bugs in the mc.net handling of the referencing

Strange behaviors:

1. If I add a reference to the VS PlStudies8 project in VS it does not show up in mc.net editor
2. If I add a reference to the .dll in the mc.net editor the script will compile with the reference to the MxChartsIO.dll
3. If I only add the reference to the mc.net editor (do not add ref in VS first) the reference does not appear in VS
4. If I add a reference to the dll in VS BEFORE I have added the reference in the mc.net editor, when I close and then reopen VS the reference (in VS) is lost.
5. If I add a reference to the dll in VS AFTER I have successfully added the reference in the mc.net editor, when I close and then reopen VS the reference remains
All references should be added through MultiCharts .Net Editor only. There is only one direction of synchronization: from МultiСharts .Net editor to PlStudies* project. The changes done in VS for PlStudies* project would not be saved and would not show up in MC.Net editor.
When external dll reference is created in .Net Editor this reference is not added in PlStudies* project
This issue is confirmed. It will be fixed in one of the future versions of MultiCharts .Net
When update external reference dll (new method, new class, etc.) IntelliSense don't accept/monitor these changes
That is not a bug, though we agree that this behavior is inconvenient. We will evaluate possible improvements for it.

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Problem adding reference to external C# .dll  [SOLVED]

Postby riverTrader » 07 Aug 2012

Okay; I seem to have got it working; it is still quirky, but I can work around it. Proviso on removing the quirks in the next release of mc.net, mark this one as SOLVED.

I cannot stress how fantastic this functionality is – it opens up MulltiCharts to a Universe of new features and functionality.

The proper procedure seems to be as follows:

1. Create a .dll in VS & compile
2. Add reference in PL.net editor
a. Open the indicator and add a reference to the .dll you’ve created
b. Compile the indicator in Pl.net editor
c. Test: success.
3. Add a reference to the .dll in VS
a. Open VS PlStudies8 project
b. Add a reference to the .dll in VS
i. Note: the study will not compile in VS at first because it does not recognize the reference added in PL.net editor.

Modifying the .dll
1. Shut down MC.net before recompiling changes to the external dll
a. As would be expected VS will be unable to recompile any changes to the .dll while mc.net is using it in its process
i. Error 1 Unable to copy file "obj\Debug\MxTestO1.dll" to "bin\Debug\MxTestO1.dll". The process cannot access the file 'bin\Debug\MxTestO1.dll' because it is being used by another process. MxTestO1
2. Restart Mc.Net
a. Mc.net runs the existing .DLL methods w/o problem
3.

Modifying the Script
1. Make changes to mc.net script and recompile
a. It doesn’t seem to matter at this point whether you make the changes in the PL.net editor or in VS
i. They are propagated correctly.


RiverTrader

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Problem adding reference to external C# .dll

Postby riverTrader » 07 Aug 2012

Final Note:

If you happen to run into problems my advice is to

1. remove the reference to the .DLL in the PL.net editor
2. shut everything down
a. mc.net
b. PL.net editor
c. Vs (PLStudies8)
d. Vs your .DLL project
3. Delete all copies of your .DLL
a. (there should be only one, unless you are copying it elsewhere in the VS post build events)
4. Open VS .dll project & recompile
5. Open PL.Net editor and add reference
6. Compile & test in PL.net editor
7. Open VS PlStudies8 and add reference to .dll

RiverTrader


Return to “MultiCharts .NET”