简体   繁体   中英

IIS Session Lost on Publish Web

Previously with my Web project in Visual Studio 2012 I was able to hit the 'Publish Web' button and as long as I did not make any changes to any .config or .cs files most of the time my session data would persist so I wouldn't have to log in again every time I make a small change to a .css or .html file.

After messing around with some settings in order to get debugging functioning with w3wp.exe now the session data is lost every single time I click Publish Web, even if I made absolutely no changes between publishes.

I don't know what I did to change this but I really need to be able to make changes to static files without having so sign in every single time. How do I stop the session from being killed?

It depends on what you're publishing. Even if you didn't make any changes knowingly, the app domain will unload if:

  • web.config was copied again (contents don't matter)
  • bin folder was modified (again, same dlls with newer timestamps are considered modification)

AFAIK, Publish always updates web.config . One thing I'm not sure is whether it updates web.config if you don't have any transforms (.debug.config or .release.config) or not.

There are couple of things you can try:

  • Set web.config 's Build Action to "None". That'd prevent it being copied to output altogether.

  • Define a new Build Configuration, say "Static Content". Using this Build Configuration, define a new publish profile, say "Static Content". Add these lines to your .csproj file

     <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> ... <OutputPath>bin\\</OutputPath> <NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn> <ExcludeFilesFromDeployment>web.config</ExcludeFilesFromDeployment> <ExcludeFoldersFromDeployment>bin</ExcludeFoldersFromDeployment> </PropertyGroup> 

    This would prevent these 2 folders from being deployed so your app domain won't unload and session won't be lost. And you can still use the other profile to do a full release when needed.

  • In your Publish > Settings > File Publish Options, ensure you have "Delete all existing files..." option unchecked.

Hopefully, one or all of these workarounds will resolve your issue.

Update: Based on the comments, here's a simple AfterBuild Target that recursively copies only changed files to publish folder. Simply copy the following code into your .csproj file and tweak the paths appropriately.

<!-- define static content to be copied over -->
<ItemGroup>
    <SourceFiles Include="CSS\**\*.css" />
    <SourceFiles Include="Scripts\**\*.js" />
 </ItemGroup>

<!-- The target that gets executed after build finishes and copies files recursively -->
<Target Name="AfterBuild">
    <Copy
        SourceFiles="@(SourceFiles)"
        DestinationFolder="C:\Publish\YourApp\%(RecursiveDir)"
        SkipUnchangedFiles="True"
    />
</Target>

Now when you build, you should see the static content getting published to your publish folder.

Notes:

  • %(RecursiveDir) copies files recursively, which helps in rebuilding the folder tree. Helpful if you have nested folders such as css\\fonts and css\\images .

  • Copy task has several other attributes (such as ContinueOnError , OverwriteReadOnlyFiles ) that you may want to tinker with.

  • You can also use standard VS macros in the destination path (eg $(SolutionRoot) , $(OutputDir) etc.).

Do you mean asp.net session? If yes then it is restored each time when the application pool is restarted . It could be caused by:

  • web config change
  • bin folder content change
  • IIS app pool recycle

Generally it is a wrong practice to rely on session data. You have to always check for the data existence and restore if not exit without forcing the user to log in.

But you can keep the session alive if you run it in SateServer or SqlServer mode: Session-State Modes

Solution 1:

Setup a new site in IIS to hold all of your static files. Eg

-Make sure it uses a different Application Pool than your other site

NewSite/Css/ .css NewSite/Scripts/ .js NewSite/JQuery NewSite/BootStrap

(don't actually create this structure, you can manage it with a new vs project)

etc

Give it a localhost host header (MachineName and Port) internal access only.

Now Create a new Project in the Solution with your existing project, move said files to it matching a structure like above.

Setup a new publish profile to deploy your css, scripts, etc etc to the New site you made http://{machinename}:port

Call the Profile something like Dev_PublishStatics or Prod_PublishStatics (w/e you want)

Now you can make changes to CSS/Scripts in Project 2 and publish them with resetting Site A

-Lastly In your Main site, Create virtual directories that point to the directories for Css, Scripts etc etc in the site you made above.

--Notes You can also move ASPX files etc etc to the new Site and map them via virtual directories too if you want, but the dlls would need to be in both sites Bin folders. But it works, just remember that if you changed code you need to publish to both sites.

Solution 2:

Change the app pool settings,

IIS Manager
-> Application Pools
->-> Select Site Application Pool
->->-> Advanced Settings on the actions menu

Now set Disable Recycle for Configuration Changes to True, this will prevent the site from resetting on configuration changes. You will need to remember to reset it yourself when new code needs loaded.

I also recommend setting

Start Mode = Always Running
Disable Overlapped Recycle = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM