1

My assembly (service) had developed for using in the WinForms Context. So if there are troubles somewhere, my dll will show error form with corresponding message.

But now I'm going to use it. The code is very old and dirty, that's why I don't want to use pattern approaches, etc. Anyway, I need to have 100% working way to determine, what context I have.

  1. I'm in WinForm
  2. I'm in WinService
  3. I'm in Web
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
EvgeniyK
  • 377
  • 3
  • 12
  • Possible duplicate: http://stackoverflow.com/questions/5464979/determine-whether-running-in-asp-net-or-winforms-console-without-system-web – Dyppl Oct 27 '11 at 04:50
  • Actually to determine if it is in Web Context: if (System.Web.HttpContext.Current != null) // we here – EvgeniyK Oct 27 '11 at 04:59

1 Answers1

4

If I understand correctly, you are on the path to create a nightmare to maintain.

Your assembly should have no knowledge of who is consuming it. Imagine tomorrow there's another context on which your assembly is used, you'll have to change your code again and add more dependencies that shouldn't be there on the first place.

IMHO, you either create an OnError event, on which you call the appropriate delegate the consumers of your assembly designate for it, or at the very least you do something similar to what System.Console.Out does; that is, you create a property of type TextWriter which you use to log all errors by default, but allow the consumers of your assembly to have a setter for this property so that they can redirect the log to their own TextWriter and handle the messages as appropriate.

Does that make sense?

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Yes, you are right, it's not true way la-la-la but I all need is right-now-strong-straight-solution – EvgeniyK Oct 27 '11 at 05:20
  • @EvgeniyK since you will go this path, make sure the consumer WinService is setup with "Allow Service to Interact with Desktop" since I am pretty sure you'll be calling MessageBox to your heart's content. Control Panel -->Admin Tools --> Services (right click your service) --> Properties --> LogOn --> Tick "Allow service to interact with desktop" – Icarus Oct 27 '11 at 05:37
  • hmm you miss my target again =) I don't want to show message, I JUST want to know where I am?! My underlying goals may be throw exception through wcf wire for silverlight-appropriate format, may be some else – EvgeniyK Oct 27 '11 at 05:54