You are on page 1of 14

TIA Portal Openness:

Referencing the
Siemens.Engineering.
dlls and Assembly
Resolve
Siemens
TIA Portal Openness Industry
Online
https://support.industry.siemens.com/cs/ww/en/view/109815895 Support
Legal information

Legal information
Use of application examples
Application examples illustrate the solution of automation tasks through an interaction of several
components in the form of text, graphics and/or software modules. The application examples are
a free service by Siemens AG and/or a subsidiary of Siemens AG ("Siemens"). They are
non-binding and make no claim to completeness or functionality regarding configuration and
equipment. The application examples merely offer help with typical tasks; they do not constitute
customer-specific solutions. You yourself are responsible for the proper and safe operation of the
products in accordance with applicable regulations and must also check the function of the re-
spective application example and customize it for your system.
Siemens grants you the non-exclusive, non-sublicensable and non-transferable right to have the
application examples used by technically trained personnel. Any change to the application exam-
ples is your responsibility. Sharing the application examples with third parties or copying the ap-
plication examples or excerpts thereof is permitted only in combination with your own products.
The application examples are not required to undergo the customary tests and quality inspections
of a chargeable product; they may have functional and performance defects as well as errors. It is
your responsibility to use them in such a manner that any malfunctions that may occur do not re-
sult in property damage or injury to persons.

Disclaimer of liability
Siemens shall not assume any liability, for any legal reason whatsoever, including, without limita-
tion, liability for the usability, availability, completeness and freedom from defects of the applica-
tion examples as well as for related information, configuration and performance data and any
damage caused thereby. This shall not apply in cases of mandatory liability, for example under
the German Product Liability Act, or in cases of intent, gross negligence, or culpable loss of life,
bodily injury or damage to health, non-compliance with a guarantee, fraudulent non-disclosure of
a defect, or culpable breach of material contractual obligations. Claims for damages arising from a
breach of material contractual obligations shall however be limited to the foreseeable damage typ-
© Siemens AG 2023 All rights reserved

ical of the type of agreement, unless liability arises from intent or gross negligence or is based on
loss of life, bodily injury or damage to health. The foregoing provisions do not imply any change in
the burden of proof to your detriment. You shall indemnify Siemens against existing or future
claims of third parties in this connection except where Siemens is mandatorily liable.
By using the application examples you acknowledge that Siemens cannot be held liable for any
damage beyond the liability provisions described.

Other information
Siemens reserves the right to make changes to the application examples at any time without no-
tice. In case of discrepancies between the suggestions in the application examples and other
Siemens publications such as catalogs, the content of the other documentation shall have prece-
dence.
The Siemens terms of use (https://support.industry.siemens.com) shall also apply.

Security information
Siemens provides products and solutions with Industrial Security functions that support the secure
operation of plants, systems, machines and networks.
In order to protect plants, systems, machines and networks against cyber threats, it is necessary
to implement – and continuously maintain – a holistic, state-of-the-art industrial security concept.
Siemens’ products and solutions constitute one element of such a concept.
Customers are responsible for preventing unauthorized access to their plants, systems, machines
and networks. Such systems, machines and components should only be connected to an enter-
prise network or the Internet if and to the extent such a connection is necessary and only when
appropriate security measures (e.g. firewalls and/or network segmentation) are in place.
For additional information on industrial security measures that may be implemented, please visit
https://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them more secure.
Siemens strongly recommends that product updates are applied as soon as they are available
and that the latest product versions are used. Use of product versions that are no longer support-
ed, and failure to apply the latest updates may increase customer’s exposure to cyber threats.
To stay informed about product updates, subscribe to the Siemens Industrial Security RSS Feed
at: https://www.siemens.com/industrialsecurity.

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 2
Table of contents

Table of contents
Legal information ......................................................................................................... 2
1 Introduction ........................................................................................................ 4
1.1 Problem Assembly Resolve ................................................................. 4
2 Solution with NuGet Package ........................................................................... 5
2.1 Installation of the NuGet Package ........................................................ 5
2.2 Add Siemens Engineering references .................................................. 6
2.3 Assembly Resolve ................................................................................ 7
3 Solution without NuGet Package ..................................................................... 8
3.1 Add Siemens Engineering references .................................................. 8
3.2 Assembly Resolve ................................................................................ 9
4 Property "Copy Local" of the Siemens.Engineering.dll .............................. 11
5 Appendix .......................................................................................................... 12
5.1 Service and support ........................................................................... 12
5.2 Industry Mall ....................................................................................... 13
5.3 Links and literature ............................................................................. 13
5.4 Application support ............................................................................. 13
5.5 Change documentation ...................................................................... 14
© Siemens AG 2023 All rights reserved

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 3
1 Introduction

1 Introduction
This application example shows how to include the Siemens Engineering refer-
ences required for TIA Portal Openness in the project and program code how to
load the suitable assembly version of the references on the computer via Assembly
Resolve at runtime.

Thus, the developed program uses the appropriate version of the Siemens Engi-
neering references and loads the references needed for execution at runtime. This
also allows the program to run with a newer TIA Portal version, even though an
older version was used to create it.

In the following chapters 2 Solution with NuGet Package and 3 Solution without
NuGet Package you will find two solutions for adding the Siemens.Engineering.dlls
and loading with Assembly Resolve. In chapter 2 the usage of an assembly resolv-
er standardized by Siemens is explained and should preferably be used. Chapter 3
describes exemplarily how you can create your own assembly resolver.

1.1 Problem Assembly Resolve


Excerpt from the Openness manual
© Siemens AG 2023 All rights reserved

Build the TIA Portal Openness program code to register on the AssemblyResolve
event as early as possible. Encapsulate access to TIA Portal in an additional object
or method.

Care must be taken when resolving the engineering assembly with an assembly
resolver method. If types from the engineering assembly are used before the as-
sembly resolver has been registered, the program crashes. This is because the
just-in-time translator (JIT translator) does not translate a method until it needs to
execute it. For example, if types of an engineering assembly are used in "Main",
the JIT translator tries to translate "Main" while the program is running. This fails
because the JIT translator does not know where to find the engineering assembly.
Registering the assembly resolver in Main does not change this. The method must
run and be translated before the assembly resolver can be registered.

The solution to this problem is to place the business logic that uses types from the
Siemens Engineering assembly in a separate method. In doing so, the separate
method uses only types that the JIT translator already understands. This example
uses a method that returns "void", has no parameters, and contains all business
logic. Now "Main" is successfully translated by the JIT translator because it under-
stands all types in Main.

When RunTiaPortal() is called during runtime, the assembly resolver is already reg-
istered. So the JIT translator knows where the Siemens Engineering Assembly is
located when trying to find the types of the Business Logic.

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 4
2 Solution with NuGet Package

2 Solution with NuGet Package

2.1 Installation of the NuGet Package


The following is an example of adding a package using the Microsoft Visual Studio
NuGet package manager. It will install e. g. the package for the Assembly Resolve.

Right-click on your project in Visual Studio (1) and select the entry "Manage NuGet
Packages..." (2).

Figure 2-1 Manage NuGet Packages


© Siemens AG 2023 All rights reserved

First select "nuget.org" (3) in "Package source" and then search for
"Siemens.Collaboration.Net.TiaPortal.Openness.Resolver" (4) for the assembly re-
solve.

Then click on "Install" (5) to install the package.

Figure 2-2 Installation of the Package

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 5
2 Solution with NuGet Package

2.2 Add Siemens Engineering references


To use the TIA Portal Openness API the Siemens.Engineering.dll from the
PublicAPI directory of the TIA Portal installation must be added to the project.
Depending on the type of project, install one of the two packages in the project as
described in 2.1.
• Openness Clients, package name:
Siemens.Collaboration.Net.TiaPortal.Packages.Openness.
• TIA Add-In, package name:
Siemens.Collaboration.Net.TiaPortal.Packages.OpennessAddIn

As package version select the corresponding TIA Portal main version for which the
application should be created. So e.g. for TIA Portal V18 choose the package ver-
sion 18.*.

With the help of the packages the Siemens.Engineering reference from one of two
locations is automatically referenced in the project:
• Environment variable TiaPortalLocation.
Define an environment variable in which the path to the TIA Portal installation is
set.
© Siemens AG 2023 All rights reserved

Figure 2-3 Environment variable TiaPortalLocation

• Windows Registry
The installation directory is read from the Windows Registry.

After the installation of the according package, the Siemens.Engineering reference


is automatically referenced in the project and the copy to output directory (Copy
Local) is disabled (see chapter 4).

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 6
2 Solution with NuGet Package

2.3 Assembly Resolve


The assembly resolver must be installed before accessing Siemens.Engineering
classes, e.g., when starting the app. Call the following method to install the assem-
bly resolver.

Api.Global.Openness().Initialize();

Example:

static void Main(string[] args)


{
Api.Global.Openness().Initialize();

RunTiaPortal();

private static void RunTiaPortal()


{
var tiaPortal = new TiaPortal(TiaPortalMode.WithUserInterface);
}

The resolver checks the following locations in order to find and load the Sie-
© Siemens AG 2023 All rights reserved

mens.Engineering.dlls on the user’s computer:


• the registry for TIA Portal installations
• running processes
• Environment variable 'TiaPortalLocation'.

It collects all found Siemens.Engineering references and automatically resolves the


correct assembly. For example, the application was created with TIA Openness
V16, at runtime TIA Portal V17 is found. The resolver will automatically resolve the
V16 assembly from the V17 installation. This behavior can be changed by specify-
ing an assembly selector:

Api.Global.Openness().Initialize(engineeringAssemblySelector:
assemblies => assemblies.First());

In this example, assemblies is the list of all found Siemens.Engineering assem-


blies, ordered by version. In this case the newest version is selected, or a fixed TIA
Portal version e.g. by

Api.Global.Openness().Initialize(tiaMajorVersion: 16);

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 7
3 Solution without NuGet Package

3 Solution without NuGet Package


3.1 Add Siemens Engineering references
The following is an example of how to add a reference in Microsoft Visual Studio.
Right-click on the "Dependencies" of your project (1) and select the "Add Assembly
Reference..." entry (2).

Figure 3-1 Add Siemens Engineering references


© Siemens AG 2023 All rights reserved

Now select the Siemens.Engineering.dll from the directory "


<TiaPortalInstallation>\PublicAPI\VXX". Then set the value "Copy Local" to "No"
(see chapter 4).

Figure 3-2 Disable Copy Local

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 8
3 Solution without NuGet Package

3.2 Assembly Resolve

For Assembly Resolve to work correctly, the property "Copy Local" of the
Siemens.Engineering.dll must be set to "False".

CAUTION See chapter 4 Property “Copy Local”

Before an openness function is used, the assembly resolver must be registered.


This must not be done in the same method in which the first openness access is
made.

Also note that the following program code is compilable only from C# 8.0. There-
fore, it is recommended to use the SDK-style project format in your Microsoft Visual
Studio project. This will allow you to change the <LangVersion> to Latest in the
.csproj file to use the latest C# version in your project.

First, a new project is created in Microsoft Visual Studio, such as a Class Library
with .NET Standard. Then the .csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
© Siemens AG 2023 All rights reserved

<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

Adjust this to:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>Latest</LangVersion>
</PropertyGroup>

</Project>

Example:
In the Main method of the developed program, a method MyResolver is registered
to the AssemblyResolve event. MyResolver is thus executed when the resolution of
a reference is necessary at runtime, such as when the loading of Sie-
mens.Engineering.dll is required.

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 9
3 Solution without NuGet Package

static void Main(string[] args)


{
AppDomain.CurrentDomain.AssemblyResolve += MyResolver;
RunTiaPortal();
}

private static void RunTiaPortal()


{
var tiaPortal = new TiaPortal(TiaPortalMode.WithUserInterface);
}

private static Assembly MyResolver(object sender, ResolveEventArgs args)


{
string siemensEngineeringDllName = "Siemens.Engineering";
string subKeyName = @"SOFTWARE\Siemens\Automation\Openness";

var assemblyName = new AssemblyName(args.Name);

if (!assemblyName.Name.StartsWith(siemensEngineeringDllName)) return null;

using var regBaseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,


RegistryView.Registry64);
using var opennessBaseKey = regBaseKey.OpenSubKey(subKeyName);

using var registryKeyLatestTiaVersion = opennessBaseKey?


.OpenSubKey(opennessBaseKey.GetSubKeyNames().Last());

var requestedVersionOfAssembly = assemblyName.Version.ToString();


© Siemens AG 2023 All rights reserved

using var assemblyVersionSubKey = registryKeyLatestTiaVersion


?.OpenSubKey("PublicAPI")
?.OpenSubKey(requestedVersionOfAssembly);

var siemensEngineeringAssemblyPath = assemblyVersionSubKey?


.GetValue(siemensEngineeringDllName).ToString();

if (siemensEngineeringAssemblyPath == null
|| !File.Exists(siemensEngineeringAssemblyPath)) return null;

var assembly = Assembly.LoadFrom(siemensEngineeringAssemblyPath);


return assembly;
}

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 10
4 Property "Copy Local" of the Siemens.Engineering.dll

4 Property "Copy Local" of the Sie-


mens.Engineering.dll
To trigger the assembly resolution for the Siemens.Engineering reference, the ref-
erence must not be located in the directory of the application. If this is the case, the
reference is already loaded from the directory of the application and no assembly
Resolve is called. To prevent this, the property "Copy Local" of the Sie-
mens.Engineering.dll must be set to "False".

Figure 4-1 Copy Local = False


© Siemens AG 2023 All rights reserved

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 11
5 Appendix

5 Appendix
5.1 Service and support
Industry Online Support
Do you have any questions or need assistance?
Siemens Industry Online Support offers round the clock access to our entire ser-
vice and support know-how and portfolio.
The Industry Online Support is the central address for information about our prod-
ucts, solutions and services.
Product information, manuals, downloads, FAQs, application examples and videos
– all information is accessible with just a few mouse clicks:
support.industry.siemens.com

Technical Support
The Technical Support of Siemens Industry provides you fast and competent sup-
port regarding all technical queries with numerous tailor-made offers
– ranging from basic support to individual support contracts.
Please send queries to Technical Support via Web form:
siemens.com/SupportRequest
© Siemens AG 2023 All rights reserved

SITRAIN – Digital Industry Academy


We support you with our globally available training courses for industry with practi-
cal experience, innovative learning methods and a concept that’s tailored to the
customer’s specific needs.
For more information on our offered trainings and courses, as well as their loca-
tions and dates, refer to our web page:
siemens.com/sitrain

Service offer
Our range of services includes the following:
• Plant data services
• Spare parts services
• Repair services
• On-site and maintenance services
• Retrofitting and modernization services
• Service programs and contracts
You can find detailed information on our range of services in the service catalog
web page:
support.industry.siemens.com/cs/sc

Industry Online Support app


You will receive optimum support wherever you are with the "Siemens Industry
Online Support" APP. The app is available for iOS and Android:
support.industry.siemens.com/cs/ww/en/sc/2067

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 12
5 Appendix

5.2 Industry Mall

The Siemens Industry Mall is the platform on which the entire siemens Industry
product portfolio is accessible. From the selection of products to the order and the
delivery tracking, the Industry Mall enables the complete purchasing processing –
directly and independently of time and location:
mall.industry.siemens.com

5.3 Links and literature


Table 5-1
No. Topic
\1\ Siemens Industry Online Support
https://support.industry.siemens.com
© Siemens AG 2023 All rights reserved

\2\ Link to this entry page of this application example


https://support.industry.siemens.com/cs/ww/en/view/109815895
\3\

5.4 Application support


Siemens AG
Digital Industries
DI FA PMA APC
Frauenauracher Str. 80
D- 91056 Erlangen, Germany
mailto: tech.team.motioncontrol@siemens.com

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 13
5 Appendix

5.5 Change documentation


Table 5-2
Version Date Modifications
V1.0 01/2023 First version
V1.0.1 02/2023 Version with NuGet package: Add another code example
with a Main method and the Initialize() method.

Version without NuGet package: Information that the sample


code only works as of C# 8.0
© Siemens AG 2023 All rights reserved

Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 14

You might also like