application won't start if needed assembly is missing...

R

Rainer Queck

Hello NG,

I realized, that my application won't start, if a referenced assembly is
missing.
Basically this is understandable, but since I destribute my applictaion
without a setup, it can happen that I forget one or the other .dll.

Since my app depends on a whole bunch of .dll it would be great, if the app
could tell me which assemblies are missing instead of not starting at all.

Is there a way to achieve this desired behaviour?

Regards
Rainer Queck
 
C

ClayB

You can subscribe to the static AssemblyResolve event which is raised
when an assembly is not found.

AppDomain.CurrentDomain.AssemblyResolve += new
ResolveEventHandler(AssemblyResolver);


public static Assembly AssemblyResolver(object sender,
System.ResolveEventArgs e)
{
MessageBox.Show(e.Name);
return null;
}

=====================
Clay Burch
Syncfusion, Inc.
 
R

Rainer Queck

Hi Clay,

thanks for your hint, but it does not work here.

I added the following code to my "program.cs" but the message box never
shows....
Is there something I am doing wrong?

Regards
Rainer

static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += new
ResolveEventHandler(CurrentDomain_AssemblyResolve);
....
}
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object
sender, ResolveEventArgs args)
{
MessageBox.Show(String.Format("Assembly {0} is missing", args.Name));
return null;
}
 
R

Rainer Queck

Hi Peter,

thanks for your hints.

I read the article, and tried the differen illustarated ways, but somehow I
get no MessageBoxes to show on the different ways.
Same like answert to Clay's hint.

Is there "some setting" I have to make to get these things working?

Regards
Rainer Queck
 
N

Nicholas Paldino [.NET/C# MVP]

Rainer,

While Peter and Clay are giving you good suggestions, I would like to
suggest that you take a higher-level view on the whole issue. A question
that you should be asking yourself is if you have a large number of dlls
that you need to distribute, why aren't you using an installer? Obviously,
this is not a matter of a lone exe, but something a little more complex.

Why not just have an installer which will guarantee that the assemblies
be there? Better yet, why not use a ClickOnce setup which will make sure
that all the assemblies in your program are there?

The best code you will write is the code you never have to write, and I
see you coding a number of things you don't have to when there are tools out
there which will do what you want (installation package tools and
ClickOnce).

Hope this helps.
 
R

Rainer Queck

Hi Nicholas,

I fully see the point, that a Installer would solve my current problem, but
my application goes into a "company installation" for a productive system,
where a Installer can not be used. Therefore I have to provide my project as
a set of folders and files.

Regards
Rainer

Nicholas Paldino said:
Rainer,

While Peter and Clay are giving you good suggestions, I would like to
suggest that you take a higher-level view on the whole issue. A question
that you should be asking yourself is if you have a large number of dlls
that you need to distribute, why aren't you using an installer?
Obviously, this is not a matter of a lone exe, but something a little more
complex.

Why not just have an installer which will guarantee that the assemblies
be there? Better yet, why not use a ClickOnce setup which will make sure
that all the assemblies in your program are there?

The best code you will write is the code you never have to write, and I
see you coding a number of things you don't have to when there are tools
out there which will do what you want (installation package tools and
ClickOnce).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rainer Queck said:
Hello NG,

I realized, that my application won't start, if a referenced assembly is
missing.
Basically this is understandable, but since I destribute my applictaion
without a setup, it can happen that I forget one or the other .dll.

Since my app depends on a whole bunch of .dll it would be great, if the
app could tell me which assemblies are missing instead of not starting at
all.

Is there a way to achieve this desired behaviour?

Regards
Rainer Queck
 
N

Nicholas Paldino [.NET/C# MVP]

Rainer,

I would still question the approach. This is the whole point of merge
files. You can create the merge file which will install your
components/exe, and then it can be included in a larger install project
(assuming you are using Windows Installer technology, or pretty much any
other installer technology out there which will leverage the Windows
Installer file formats).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rainer Queck said:
Hi Nicholas,

I fully see the point, that a Installer would solve my current problem,
but my application goes into a "company installation" for a productive
system, where a Installer can not be used. Therefore I have to provide my
project as a set of folders and files.

Regards
Rainer

Nicholas Paldino said:
Rainer,

While Peter and Clay are giving you good suggestions, I would like to
suggest that you take a higher-level view on the whole issue. A question
that you should be asking yourself is if you have a large number of dlls
that you need to distribute, why aren't you using an installer?
Obviously, this is not a matter of a lone exe, but something a little
more complex.

Why not just have an installer which will guarantee that the
assemblies be there? Better yet, why not use a ClickOnce setup which
will make sure that all the assemblies in your program are there?

The best code you will write is the code you never have to write, and
I see you coding a number of things you don't have to when there are
tools out there which will do what you want (installation package tools
and ClickOnce).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rainer Queck said:
Hello NG,

I realized, that my application won't start, if a referenced assembly is
missing.
Basically this is understandable, but since I destribute my applictaion
without a setup, it can happen that I forget one or the other .dll.

Since my app depends on a whole bunch of .dll it would be great, if the
app could tell me which assemblies are missing instead of not starting
at all.

Is there a way to achieve this desired behaviour?

Regards
Rainer Queck
 
G

Guest

Rainer,
Nick is absolutely right. Even in the situation where your company cannot
use an MSI installer or merge module, you can still create an installer for
your project that will ensure that all dependencies are present, perform an
install locally, and then provide your company with the resultant folder and
files.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




Rainer Queck said:
Hi Nicholas,

I fully see the point, that a Installer would solve my current problem, but
my application goes into a "company installation" for a productive system,
where a Installer can not be used. Therefore I have to provide my project as
a set of folders and files.

Regards
Rainer

Nicholas Paldino said:
Rainer,

While Peter and Clay are giving you good suggestions, I would like to
suggest that you take a higher-level view on the whole issue. A question
that you should be asking yourself is if you have a large number of dlls
that you need to distribute, why aren't you using an installer?
Obviously, this is not a matter of a lone exe, but something a little more
complex.

Why not just have an installer which will guarantee that the assemblies
be there? Better yet, why not use a ClickOnce setup which will make sure
that all the assemblies in your program are there?

The best code you will write is the code you never have to write, and I
see you coding a number of things you don't have to when there are tools
out there which will do what you want (installation package tools and
ClickOnce).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rainer Queck said:
Hello NG,

I realized, that my application won't start, if a referenced assembly is
missing.
Basically this is understandable, but since I destribute my applictaion
without a setup, it can happen that I forget one or the other .dll.

Since my app depends on a whole bunch of .dll it would be great, if the
app could tell me which assemblies are missing instead of not starting at
all.

Is there a way to achieve this desired behaviour?

Regards
Rainer Queck
 
J

Jeffrey Tan[MSFT]

Hi Rainer,

AppDomain.AssemblyResolve will be fired when the resolution of an assembly
fails, that is when your application is missing any assembly. Note: .Net is
using delay assembly loading style, so if your main Exe is linking with
some class library assembly but did not use it yet, the .Net loader will
not try to resolve and load it yet. Below is a sample to demonstrate this:

//class library assembly
namespace ClassLibrary1
{
public class Class1
{
public static int Add(int a, int b)
{
return a + b;
}
}
}

//I created a .Net Winform project
//and use this class library assembly
//in the Button.Click event
private void button1_Click(object sender, EventArgs e)
{
ClassLibrary1.Class1.Add(3, 4);
}

[STAThread]
static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += new
ResolveEventHandler(CurrentDomain_AssemblyResolve);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object
sender, ResolveEventArgs args)
{
MessageBox.Show(String.Format("Assembly {0} is missing", args.Name));
return null;
}

After I deleted the ClassLibrary1.dll in the Exe folder, I double-click the
exe to launch the application. Everything works well, the
CurrentDomain_AssemblyResolve is not fired. Then, when I click the button
in the form, the CurrentDomain_AssemblyResolve is fired and the CLR is
asking you for *help* to find the missing assembly.

You may give this a test to see if the behavior is consistent.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rainer Queck

Hi Peter,

Peter Bromberg said:
Rainer,
Nick is absolutely right. Even in the situation where your company cannot
use an MSI installer or merge module, you can still create an installer
for
your project that will ensure that all dependencies are present, perform
an
install locally, and then provide your company with the resultant folder
and
files.
Actualy this is exactly what I am doing at the moment. The only unnice point
is, that in my controlpanel / software I have a Installed sofware wich I
don't really have installed. This is no problem, it just bothers me for some
unexplainable reason ;-)

Rainer
 
R

Rainer Queck

Hi Jeffrey,

"Jeffrey Tan[MSFT]" said:
Hi Rainer,

AppDomain.AssemblyResolve will be fired when the resolution of an assembly
fails, that is when your application is missing any assembly. Note: .Net
is
using delay assembly loading style, so if your main Exe is linking with
some class library assembly but did not use it yet, the .Net loader will
not try to resolve and load it yet.

actually this is the situation I have, still I don't get the event.

static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += new
ResolveEventHandler(CurrentDomain_AssemblyResolve);
// Start Diagnose
ReportSettings reporterSettings = new ReportSettings();
FileDiagnose.Instance.DiagDir =
Helpers.PathWithBackSlash(reporterSettings.DiagDir, true);
....
}

FileDiagnose and Helpers are classes, locate in seperate dll, which for
testing I deleted.
Still the event is not fired.

Regards
Rainer
 
R

Rainer Queck

Hi Peter,

Peter Bromberg said:
Try doing the install from within your solution by right-clicking on the
Setup project and choosing "Install". You can then copy the target folder
and
contents somewhere else. Then right- click again and choose "uninstall".
Now
you have your "stuff" and the entry in Control Panel Add/Remove will be
gone.

Thanks for this hint!
That is the way I will do it from now on.

Regards
Rainer
 
J

Jeffrey Tan[MSFT]

Hi Rainer,

Thanks for your feedback.

This looks a bit strange. Have you tried this in a little sample project?
This test will help to identify if this problem is this project specific.
If this problem can be reproduced in a little sample project, please feel
free to send to me at (e-mail address removed)(remove "online.").
Furthermore, if you need my working sample project, please feel free to
tell me, thanks.

If you want to get Peter Bromberg's unhandled exception filter solution
work, you may also demonstrates the problem in the sample project, I will
help to give it a troubleshoot. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Derek

Rainer,

I think the problem you are having is that you are trying to use the reference in the same spot you are trying to trap the error. It fires the event as soon as it starts to load Main (because the missing reference is used there). As a test, put the code calling the dll in another function/sub/whateveryoucallthem in C#, and then call that from main. Or, rename Main as something else and create a new Main to handle these errors.

For example (forgive my C# syntax, I don't code much in it):

static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += new
ResolveEventHandler(CurrentDomain_AssemblyResolve);
// Start Diagnose
oldmain();
}

static void oldMain()
{
// Start Diagnose
ReportSettings reporterSettings = new ReportSettings();
FileDiagnose.Instance.DiagDir =
Helpers.PathWithBackSlash(reporterSettings.DiagDir, true);
.....
}

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
R

Rainer Queck

Hi Derek,

Sorry, that I did not respond so long time, but I had to switch to a
different project, and forgot about this thread. Now, back to this project,
I memorized my problem, and checked the NG, finding your Hint. It looks like
you are absolutely right! With the way you described, the event gets fired.

Thanks for your help!
Rainer
 
R

Rainer Queck

Hello Jeffrey,

Sorry for not responding to your offer that long time, but I had to switch
to a different project and there fore did not monitor my thread for a while.
Now beeing back, I found the time to continue work on my problem again.

Derek (see his answer to the thread on 9th April) found the reason for my
problem. With is workaround, the event gets fired.

Thanks for your offer, but I up to now was not able to reproduce the problem
in a little demo project.
Anyhow, I have my workaround :)

Regards
Rainer Queck
 
J

Jeffrey Tan[MSFT]

Hi Rainer,

Glad to see Derek's reply can help you. If you need further help, please
feel free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top