Professional Documents
Culture Documents
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.
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
Right-click on your project in Visual Studio (1) and select the entry "Manage NuGet
Packages..." (2).
First select "nuget.org" (3) in "Package source" and then search for
"Siemens.Collaboration.Net.TiaPortal.Openness.Resolver" (4) for the assembly re-
solve.
Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 5
2 Solution with NuGet Package
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
• Windows Registry
The installation directory is read from the Windows Registry.
Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 6
2 Solution with NuGet Package
Api.Global.Openness().Initialize();
Example:
RunTiaPortal();
The resolver checks the following locations in order to find and load the Sie-
© Siemens AG 2023 All rights reserved
Api.Global.Openness().Initialize(engineeringAssemblySelector:
assemblies => assemblies.First());
Api.Global.Openness().Initialize(tiaMajorVersion: 16);
Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 7
3 Solution without NuGet Package
Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 8
3 Solution without NuGet Package
For Assembly Resolve to work correctly, the property "Copy Local" of the
Siemens.Engineering.dll must be set to "False".
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>
<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
if (siemensEngineeringAssemblyPath == null
|| !File.Exists(siemensEngineeringAssemblyPath)) return null;
Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 10
4 Property "Copy Local" of the Siemens.Engineering.dll
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
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
Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 12
5 Appendix
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
Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 13
5 Appendix
Assembly Resolve
Entry-ID: 109815895, V1.0.1, 02/2023 14