1

I have an app with multiple multi-threaded front ends (web mvc, wcf services, winforms, windows service) and they all use to the same persistence layer. I am curious if there is a way to have that disconnected layer (e.g. my persistence layer) self determine what type of front end created this instance of it? I was hoping maybe there was a way to use reflection to determine if it was running in an httpcontext, applicationcontext, etc.?

John S.
  • 1,937
  • 2
  • 18
  • 28
  • 5
    I hope not, since I'm not ready for the human race to be wiped out by machines. – siride Aug 03 '13 at 21:42
  • You use AppDomains to create a sandbox or to isolate potentially misbehaving code from your main app. Making it impossible to see what the main app looks like is *intentional*. The CLR helps, you can't iterate AppDomains either. So, no. – Hans Passant Aug 04 '13 at 02:18

2 Answers2

1

You can try to analyze the current process context in order to guess what executable launched it, or the properties of the parent process perhaps. For example, if Process.GetCurrentProcess().ProcessName is YourWinformProject, it was launched from a Winforms context, but if it's the name of the executable of IIS/Apache, it's running as a web page. Neither are 100% reliable though.

Just curious, why does it matters at all? I think that both domain logic and persistence should be the same, no matter from where it's being called. If it really needs to behave different, why not just pass an additional piece of data to the service/model/repository/whatever to tell how to act, based on the current context.

Alejandro
  • 7,290
  • 4
  • 34
  • 59
1

You can tell a good bit of your envirment by checking different static properties. Although none of these are foolproof.

You can tell if you are running in a web application by checking if HttpContext.Current is null (well you can at least tell if you are on a web thread). This is what Structure Map uses.

You can tell if you are in a WinForms app by checking System.Windows.Forms.Application.MessageLoop. Although like HttpContext.Current this will only work if you are on the UI thread. You could also AppDomain.CurrentDomain.GetAssemblies() to see if System.Windows.Forms.dll is loaded. There is also the Environment.UserInteractive which will tell you running with a UI be it WinForms, WPF, or a console app.

See this question for how to determine if you are running as a service.

For WCF you can should be able to check the OperationContext.Current. This will tell you if you are running the context of a WCF request or not.

Of course your domain layer really shouldn't have to check all of these things since it shouldn't care. If it needs to act differently with the different front ends, the front ends should inject that behavior using a interface implementation to provide the behavior.

Community
  • 1
  • 1
shf301
  • 31,086
  • 2
  • 52
  • 86
  • Your answer gives a lot of links for information that I was looking for, but didn't know what it was I needed to look up. Injection has always been my backup plan if I couldn't find something better. – John S. Aug 11 '13 at 00:27