Can I detect if my code is executing in an Azure worker role?

前端 未结 5 1884
情深已故
情深已故 2021-01-18 13:24

I have some shared assemblies/projects that are used within Winforms apps, windows services and now Azure worker roles.

Is there any way that I can detect at runtime

5条回答
  •  攒了一身酷
    2021-01-18 14:10

    Like you say, adding references to all your end-products is not the way to go. I would say this is a problem solved very easily using Dependency Injection.

    Define an interface which yields this information (in a shared assembly):

    public enum DeploymentType { WinForms, WinServices, Azure }
    
    public interface IWhatDeploymentAmIUsing {
        DeploymentType DeploymentType { get; }
    }
    

    And create a class that implements this interface.

    WinForms (in your winforms project):

    public class WinFormDeploymentType : IWhatDeploymentAmIUsing {
        public DeploymentType DeploymentType { get { return DeploymentType.WinForms; } }
    }
    

    WinServices (in your windows service project):

    public class WinServicesDeploymentType : IWhatDeploymentAmIUsing {
        public DeploymentType DeploymentType { get { return DeploymentType.WinServices; } }
    }
    

    Azure (in your azure project):

    public class AzureDeploymentType : IWhatDeploymentAmIUsing {
        public DeploymentType DeploymentType { get { return DeploymentType.Azure; } }
    }
    

    Now wire it up using your favorite DI tool.

提交回复
热议问题