AppEnd();
}
static void AppStart()
{
}
static void AppEnd()
{
}
}
Code Snippet Program.cs
For a VB Windows Forms application, you can use the Application Events functionality provided
the Windows application framework to specify Startup and Shutdown methods as shown in the
following listing:
Vb
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
Partial Friend Class MyApplication
Private Sub MyApp_Startup(ByVal sender As Object, _
ByVal e As StartupEventArgs) _
Handles Me.Startup
End Sub
Private Sub MyApp_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown
End Sub
End Class
End Namespace
Code Snippet ApplicationEvents.vb
Once you have a startup and shutdown method defined in your application, you can add the
SetupAttribute and TeardownAttribute attributes. Locate your startup method in the tree,
then right-click it and select Add Attribute. Select SetupAttribute and click OK. In the attribute
properties you will need to specify a value for the CustomEndpoint property, which instructs
Dotfuscator where to send any instrumentation messages. Click the ellipsis button and select
PreEmptive’s Free Runtime Intelligence Services from the list.
You shouldn’t collect information about application usage without asking
permission from your users, otherwise your application could be flagged as
spyware. The SetupAttribute provides three properties to help configure
this — OptInSourceElement, OptInSourceName, and OptInSourceOwner.
application Monitoring and Management .
955
The Runtime Intelligence Service is a managed service provided by PreEmptive Solutions that
aggregates and manages Runtime Intelligence data generated by your application, and a web portal
that includes runtime analytics. PreEmptive Solutions offers a free and a commercial version of its
Runtime Intelligence Service.
The free version of PreEmptive’s Runtime Intelligence Service is suitable only
for preliminary testing purposes because it lacks sufficient security and does not
include any guarantees of uptime or data retention. You should transition to the
commercial version or find/build an alternative for production deployment.
Finally, add the TeardownAttribute attribute to the appropriate method and then build the
Dotfuscator project. Run the application a couple of times to generate some instrumentation
messages. You can also use the TamperTester utility described in the previous section to create a
“tampered” version of the application that generates Tamper notifications.
Once you have run the application a number of times, open your browser and visit http:/free.
runtimeintelligence.com. You can log on to the portal using the unique identifier you generated
earlier for the CompanyKey
property.
The Welcome screen, which is displayed after you log on, shows the number of notification messages
that are queued waiting to be loaded into the database. You may need to wait until any pending
messages are loaded before you can view any data.
On the Application Overview menu you can view summary graphs that show data such as the
application usage over time, and the operating systems and .NET Framework versions used by your
users. The Application Scorecard report, shown in Figure 47-14, displays a list of all the applications
registered under this Company Key, along with key metrics about their usage.
fiGure 47-14
956 .
chaPter 47 obFuScATion, ApplicATion moniToring, And mAnAgemenT
Later in this chapter you see how the usage tracking can be extended to cover usage of specific
features within your application.
application expiry
The application expiry feature, also known as Shelf Life, allows you to specify an expiration date
for your application, after which it will no longer run. This can be useful in a number of scenarios,
such as when releasing beta or trial versions of software.
Application expiry requires a Shelf Life Activation Key (SLAK). This key is issued by PreEmptive,
and can be requested by visiting http://www.preemptive.com/products/shelflife.
Two attributes are available to help implement application expiry. The InsertShelfLifeAttribute
attribute enforces the expiration dates, ensuring that the application will not run after the
specified date. It can also send a notification to an arbitrary endpoint when an expired application
is executed. The InsertSignOfLifeAttribute attribute sends a notification to the Runtime
Intelligence service every time your application is executed. This allows you to find out how often an
application was executed.
Before adding the application expiry attributes, you should set up your assembly with the
BusinessAttribute, ApplicationAttribute, SetupAttribute, and TeardownAttribute
attributes, as described in the previous section on Runtime Intelligence.
It’s a good idea to add the application expiry attributes to a method that is called shortly after
the application is started. You may also want to add it to a method that is called regularly, just
in case your users leave your application running after the expiry date. In Figure 47-15, the
InsertShelfLifeAttribute and InsertSignOfLifeAttribute attributes have been added to
the Form1.InitializeComponent method. This ensures that the application expiry date will be
checked every time Form1 is invoked.
fiGure 47-15
application Monitoring and Management .
957
Both attributes require a Shelf Life Activation Key. Once you have obtained this key from PreEmptive,
save it to your local disk and set the path to this file in the ActivationKeyFile property. Setting the
ExpirationDate property to a date in the past is a good way to test this feature.
When an application expires, the behavior is determined by two settings. First, if you have Send Shelf Life
Notification Messages checked on the Instrumentation Options tab, it will send a notification message
to the endpoint you have specified. Second, if you have set the ExpirationNotificationSinkElement
property to DefaultAction, the application will immediately exit.
The commercial edition of Dotfuscator allows you to specify a warning date and add custom
handlers that are executed when the warning date or expiration date are reached. You could use
this to deactivate specific features or display a friendly message to the users advising them that the
application has expired.
The commercial version of Dotfuscator also allows your application to obtain the shelf life information
from an external location, such as a web service or configuration file. This allows you to support other
expiration scenarios such as expiring 30 days from installation or renewing annual subscriptions.
application usage tracking
Earlier in this chapter you saw how to add the SetupAttribute and TeardownAttribute attributes
to your application. By adding these attributes your application can send notification messages, and
thereby allow you to track usage data and system environment statistics for your applications. These
attributes are also used to determine application stability, because a missing Teardown notification
indicates that the application may have crashed or a user may have gotten frustrated and simply
forced an exit.
In addition to tracking application startup and shutdown, Dotfuscator allows you to further
instrument your code to track usage of specific features within your application. With Dotfuscator
CE, you can add up to ten FeatureAttribute attributes to your methods, each one specifying the
same or a different feature. This allows you to aggregate your application’s methods into a logical,
high-level “feature” grouping that is independent of the actual class hierarchy of your code.
In Figure 47-16, you can see that a FeatureAttribute attribute has been added to the
EvaluatePerson and the GenerateMagicNumber methods of the MathematicalGenius class. These
features have been given a descriptive name, which is displayed when viewing the usage reports.
These attributes also have the FeatureEventType property set to Tick, which simply tracks that the
feature has been used.
958 .
chaPter 47 obFuScATion, ApplicATion moniToring, And mAnAgemenT
fiGure 47-16
In addition to Tick feature tracking, you can also track the amount of time a feature was used. In
this case you will need to add two FeatureAttribute attributes — one with the FeatureEventType
property set to Start and the other set to Stop. This generates two instrumentation messages and
allows the Runtime Intelligence analytics service to calculate feature usage duration.
The commercial edition of Dotfuscator includes:
.
Unlimited feature tracking
.
Injection of the Microsoft WMI SDK for hardware and software stack detection
.
Extensible data capture to include custom data values
.
An SSL runtime data transmission option
Runtime Intelligence data can be used to improve the development process, provide greater
visibility into application usage for IT operations, and serve as an additional data source
for business activity and performance monitoring. Microsoft’s own Customer Experience
Improvement Program (CEIP) relies on this kind of usage data (for a description of its program,
visit http://www.microsoft.com/products/ceip/).
As managed code moves beyond the desktop and the in-house server to the web client (Silverlight),
the cloud (Azure), and your mobile devices, Runtime Intelligence will likely become an increasingly
important part of your application life cycle management toolkit.
summary .
959
suMMary
This chapter introduced two tools, IL Dasm and Reflector, which demonstrated how easy it is to
reverse-engineer .NET assemblies and learn their inner workings. You also learned how to use
Dotfuscator Software Services to:
.
Protect your intellectual property using obfuscation
.
Harden your applications against modification using tamper defense
.
Monitor and measure application usage with Runtime Intelligence instrumentation
.
Enforce your application’s end-of-life with shelf life
48
Packaging and Deployment
what’s in this chaPter?
.
Creating installers for your projects
.
Customizing the installation process
.
Verifying the presence of application prerequisites
.
Installing Windows Services
.
Deploying projects over the web with ClickOnce
.
Updating ClickOnce projects
One area of software development that is often overlooked is how to deploy the application.
Building an installer is a simple process and can transform your application from an amateur
utility to a professional tool. This chapter looks at how you can build a Windows Installer for
any type of .NET application.
Visual Studio 2010 also includes support for a ClickOnce deployment, which can be used to
build applications that can be dynamically updated. This is particularly important for rich
client applications that periodically need updating, because it solves the problem of how to roll
out those updates.
windows installers
Windows Installer has been a standard part of Windows since Windows 2000. It is the
component that manages the installation, maintenance, and removal of software packages.
Visual Studio 2010 comes with a rich user interface for building installation packages for a
variety of different applications that target the Windows Installer infrastructure. This chapter
962 .
chaPter 48 pAckAging And deploymenT
demonstrates building installers for desktop applications and windows services. The next chapter
contains information about packaging and deploying web applications.
building an installer
To build an installer with Visual Studio 2010, you need to add an additional project to the
application that you want to deploy. Figure 48-1 shows the available setup and deployment project
types. The Setup Project should be used for Windows Forms or service applications, and the Web
Setup Project should be used for ASP.NET web sites or web services. If you want to build an installer
that will be integrated into a larger installer, you may want to build a merge module. Alternatively,
a CAB Project can be used to create a package that can be deployed via a web browser. The Setup
Wizard steps you through the process of creating the correct project for the type of application
you’ re deploying.
fiGure 48-1
Web Setup Projects are covered in the next chapter.
In this case, you are going to use the Setup Wizard to create an installer for a simple C# Windows
Forms Application, CallCentre. After acknowledging the Setup Wizard splash screen, the first
decision is specifying whether you want to create an installer or a redistributable package. For an
installer, you need to choose between a Windows application or a web application installer. The
basic difference is that the Windows application installer places the application in the appropriate
folder within Program Files, whereas the web application installer creates a virtual directory under
the root folder for the specifi ed web site. In the case of a redistributable package, the choice is
between a merge module, which can be integrated into a larger installer, or a CAB fi le.
Regardless of the type of deployment project
you are creating, the next step in the Setup
Wizard is the most important because it
determines the set of fi les to be deployed.
Figure 48 - 2 shows the third screen in the
Setup Wizard, which prompts you to select
which fi les or project outputs will be included
in the deployment project. In this case,
the primary output for your CallCentre
Application has been selected, because you
want to include the main executable and any
assemblies on which this executable depends.
The Content Files item has also been selected,
which will include any fi les with the build
action set to Content. In the remaining step in
the Setup Wizard, you can choose to add fi les
that were not part of any existing project. For example, this might include release notes, licensing