Skip to content

Programming C#: Using System.Net

I'm working on my first C# function block and I would like to use System.Net and System.Net.Sockets. When creating the function block in Visual Studio, I get an error saying that to use System.Net I must change the target framework to .NET Framework. It doesn't have a problem using System.IO so I'm not sure where I'm going wrong. Any insights?

Comments

  • Hi Rob, I have tried this and found the same - it appears that the System.Net namespace cannot be used in C# applications at the moment.

    It may be that the current documentation has gotten slightly ahead of the current implementation, before the next major software release in November. I am currently checking this internally and I'll let you know when I have a definitive answer.

    - Martin.

  • UPDATE 4.01.2021 - please see additional information from Marcel at the end of this thread.

    ======================================================================

    Hi Rob,

     The solution is to add a Reference to the "System" assembly to your C# project, as shown below.

    I'll see if we can get this important step added to our How To guide.

  • Hello Martin,

    I'm also trying to use the Reference System.Net and also System.Net.Http, but I'm ending up with the same issue, that the project will change the target.

    Do you have some informations regarding the possibility to include other references from C# ?

    Dominique

  • Hello Dominique,

    When you say "other references from C#", do you mean that you are unable to use some of the classes documented in the eCLR Programming Reference? (This programming reference is included in the "Documentation" folder in the Visual Studio Solution Explorer, for every solution created using the PLCnext add-in for Visual Studio). This reference contains the complete list of classes that you should be able to use in your C# project. If you cannot access some of these classes in your C# project, please let me know which one(s) you are having problems with.

    Note that the eCLR only contains a sub-set of the complete .NET framework that you would find on a PC, because the eCLR is designed specifically for deterministic, real-time programs. System.Net.Http is currently not one of the classes that is available in the eCLR.

    There are a few possibilities to work around this - for example, you could implement the HTTP protocol yourself on a TCP socket; or you could execute your HTTP requests in a non-realtime PLCnext component written in C++; or you could execute your HTTP requests in an external C# application running on .NET core.

    Hope this helps.

    Martin.

  • I'm having the same issue as Dominique. I want to use System.Net.Sockets.UdpClient. It is shown in the documentation. When I try to add a reference to System.Net, it wants to retarget the project to .NET Framework 4. When I first started this post, I was able to make it work. Now after updating to 2019.6, eCLR 3.2 Profile, and VS 2017 I can't seem to get it to work. Is it not possible to use System.Net?

     

    EDIT: I'm not sure what I was doing wrong but it seems to be working now. I went back to eCLR 3.1 and VS 2015. Created a new project and class with UdpClient and it worked. Then updated to eCLR 3.2 and VS 2017 and the same project I was having issues with is working now.

  • I have one addition to the answer of Martin. Make sure to use only references from Phoenix Contact or build for your target eCLR Profile. .NET References do not work. All available namespaces provided by Phoenix Contact can be found in each project created for PLCnext with Visual Studio in the file Documentation/eCLR-Programming-Reference.chm.
    @Rob H: Your issue is a combination of issues. Starting with Visual Studio 2017 we where not able to add System.dll as a default reference. You have to add the "System" reference for your eCLR Profile manually and this contains all available namespaces you are missing.

    Capture

     

     

Sign In or Register to comment.