2017-08-21 83 views
1

我有一个.net核心1.1 web应用程序,我正在与我的团队合作。在某些情况下,我们需要使用IIS Express来调试应用程序,而在其他情况下,我们需要使用WebListener。由于WebListner命令会导致应用程序在IIS Express下运行时发生崩溃,因此我希望使用预处理器指令在IIS Express下运行应用程序时禁用此功能。该守则将是这个样子:.Net Core中每次启动设置的预处理器指令

#if !RUNNING_UNDER_IIS_EXPRESS 
    .UseWebListener(options => 
    { 
     options.ListenerSettings.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM; 
     options.ListenerSettings.Authentication.AllowAnonymous = false; 
    }) 
#endif 

谁能告诉我怎么可以设置此还是建议做整个事情的一个更好的办法?

回答

1

问题与您的问题是在编译时使用和评估预处理器指令,而不是运行时。所以如果你想要一个“easy”开关,你必须在你的csproj中将它定义为构建配置。你要构建的配置添加到您的csproj文件:

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='WebListener|AnyCPU'"> 
    <DefineConstants>DEBUG</DefineConstants> 
    <DebugSymbols>true</DebugSymbols> 
    <DebugType>full</DebugType> 
    <Optimize>false</Optimize> 
</PropertyGroup> 

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='IISExpress|AnyCPU'"> 
    <DefineConstants>DEBUG</DefineConstants> 
    <DebugSymbols>true</DebugSymbols> 
    <DebugType>full</DebugType> 
    <Optimize>false</Optimize> 
</PropertyGroup> 

而且你要添加的信息,这些信息建立配置awailable:

<PropertyGroup> 
    <TargetFramework>netcoreapp2.0</TargetFramework> 
    <Configurations>Debug;Release;WebListener;IISExpress</Configurations> 
</PropertyGroup> 

所以你可以使用你的代码,例如

#if WEBLISTENER 
    .UseWebListener(options => 
    { 
     options.ListenerSettings.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM; 
     options.ListenerSettings.Authentication.AllowAnonymous = false; 
    }) 
#endif 

#if IISEXPRESS 
    /* ... */ 
#endif 

但是:有了这个解决方案,你必须改变两者的启动设置和构建设置,您configuratio之间切换NS:

Build + Launch Configurations

因为你可以看看这些ressources更多信息: