顯示具有 .NET-Visual Studio 標籤的文章。 顯示所有文章
顯示具有 .NET-Visual Studio 標籤的文章。 顯示所有文章

2025年12月21日 星期日

[.NET/Visual Studio] 如何使用Visual Studio Performance Profiler來找memory leak?

要抓.NET應用程式的memory leak,最基本就是使用Visual Studio Performance Profiler,

該如何用呢? 操作步驟如下:

  • 打開Performance Profiler(Alt + F2)
  • 選擇分析目標,此處使用啟動專案並勾選Memory Usage,點擊啟動(建議使用release build)

  • 在baseline時機點,點擊快照

  • 執行懷疑有memory leak的操作後,再快照1次來比較2次快照的差異(點擊紅框處)。

  • 可針對size差異做排序,找出那些物件造成leak,點擊其中一個物件,下方面板會顯示此物件的參考鏈,就可以去判斷為何物件還沒被GC。

  • 取得所需的快照後,即可停止收集,並可將結果儲存起來。


參考資料:
https://learn.microsoft.com/en-us/visualstudio/profiling/memory-usage?view=visualstudio
https://learn.microsoft.com/en-us/visualstudio/profiling/memory-usage-without-debugging2?view=visualstudio&pivots=programming-language-dotnet
https://learn.microsoft.com/en-us/visualstudio/profiling/hot-path-to-root?view=visualstudio


2019年9月17日 星期二

[Visual Studio] Console程式建立app.Release.config與app.Debug.config

前言:
使用Visual Studio(VS)開發Web Application,會有Web.config的設定檔,
預設會有Web.Debug.config與Web.Release.config來做組態管理,
但主控台專案的app.config預設卻沒有??


作法1:
● 在csproj專案上右鍵->卸載專案->編輯xxx.csproj
打開專案檔後,通常就可看到PropertyGroup的element,
在這群element附近(通常在之後)貼上:
<PropertyGroup>
    <ProjectConfigFileName>App.config</ProjectConfigFileName>
</PropertyGroup>
如:



● 接著往下找到ItemGroup的element,會有如下的設定,
<ItemGroup>
    <None Include="App.config" />
</ItemGroup>
 在上述的<None Include="App.config" />下加入:
 <None Include="App.Debug.config">
      <DependentUpon>App.config</DependentUpon>
</None>
<None Include="App.Release.config">
      <DependentUpon>App.config</DependentUpon>
</None>
如:


●然後在<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />下方加入:
  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.targets" />
  <Target Name="AfterBuild">
    <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
  </Target>
如:


●重新載入專案後,在專案上右鍵->加入->新增項目->加入App.Debug.config/App.Release.config


● 最後,在各自組態設定檔中加入欲轉換的設定。
  <applicationSettings>
    <Project.Properties.Settings>
      <setting name="key" serializeAs="String" xdt:Transform="Replace">
        <value>value</value>
      </setting>
    </Project.Properties.Settings>
  </applicationSettings>

  <appSettings>
    <add key="key" value="value" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
  </appSettings>

作法2:
使用Configuration Transform,免除上述手動的作法,
方便快速又支援轉換預覽。操作部分可以參考該網址的說明。


參考資料:
https://demo.tc/post/775
https://marketplace.visualstudio.com/items?itemName=GolanAvraham.ConfigurationTransform

2017年10月24日 星期二

[.Net/VS] Could not load file or assembly 'Microsoft.Web.Deployment'與莫名發行失敗(fail no output message)


● 使用VS針對web application使用發行,
出現Could not load file or assembly 'Microsoft.Web.Deployment, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35', 究竟發生什麼事??

原以為是web application需要參考Microsoft.Web.Deployment,
經過一番嘗試之後,
發現在本機(client端)安裝web deploy即可。

安裝完到路徑C:\Program Files,會出現IIS\Microsoft Web Deploy V3資料夾,
裡面會有Microsoft.Web.Deployment.dll,如下圖。



● 發行後,VS左下角狀態列出現發行失敗,且無其他訊息。

可將專案實體資料夾下bin內檔案手動清除,再發行看看。

2017年10月22日 星期日

[.Net/VS] Web Deployment 工作失敗:Microsoft.Web.Deployment.DeploymentManager的類型初始設定式發生例外狀況(threw an exception type initializer)

說明:
使用VS內建發行,點選「驗證連接(Validate Connection)」,
出現如下錯誤訊息。


解法:
搜尋網路解法,有人說要解除Microsoft Web Deploy dbSqlPackage Provider(如下圖所示),
但是我的Server並無安裝該程式。


又看到重新安裝Server上Web deploy的解法,

於是使用Web Platform Installer 5.0安裝:
● web deploy 3.6
● web deploy 3.6 for Hosting Servers
● web deploy 3.6 without bundled SQL support(latest)

再到控制台->解除安裝程式,
找到web deploy 3.6後,點選「變更」,
改成完全安裝就可以了,如下圖。



參考資料:
https://docs.microsoft.com/en-us/iis/publish/using-web-deploy/web-deploy-automatic-backups
https://stackoverflow.com/questions/6351289/web-deployment-task-failed-the-type-initializer-for-microsoft-web-deployment
http://blog.sanc.idv.tw/2014/08/aspnet-web-deploymvciis.html

2016年4月19日 星期二

[Visual Studio] 使用Visual Studio 2015從程式碼逆向工程產出類別圖(class diagram)

說明:
從Visual Studio 2010後,可從架構總管(architecture explorer),提供從程式碼逆向產出類別圖,但VS 2015拿掉了架構總管,那該怎麼產出類別圖?


作法:
使用類別檢視(VS -> 檢視 -> 類別檢視)。
切換至類別檢視,點開你的專案,會列出專案全部namespce,
再點開namespace,會列出包含的class,在cs檔上右鍵,選擇類別圖檢視,
VS就會新增ClassDiagram1.cd,上面就有該class的圖,
再將其他class拖曳至ClassDiagram1.cd,即可產出想要的類別圖。


參考資料:
https://blogs.msdn.microsoft.com/visualstudioalm/2014/10/24/upcoming-changes-in-visual-studio-architecture-and-design-tools/

2016年2月16日 星期二

[Visual Studio] 如何自動同步程式發行版本與最小必要版本? (強制更新發行程式) / minimum required version auto-increment in the clickonce deployment

說明:
使用clickonce發行程式安裝檔,想強制更新client程式,需指定最小必要版本,因發行版本可自動遞增,如何讓最小必要版本同步發行版本? 讓client程式可自動更新。


做法:
1. 方案總管中的專案圖示 -> 右鍵 -> 卸載專案
2. 一樣專案圖示  -> 右鍵 -> 編輯xxx.csproj
3. 在<Project>與</Project>的tag中插入以下設定:
<Target Name="AutoSetMinimumRequiredVersion" BeforeTargets="GenerateDeploymentManifest">
  <FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
    <Output PropertyName="MinimumRequiredVersion" TaskParameter="OutputVersion"  />
  </FormatVersion>
  <FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
    <Output PropertyName="_DeploymentBuiltMinimumRequiredVersion" TaskParameter="OutputVersion"  />
  </FormatVersion>
</Target>
設定是將ApplicationVersion(如: 1.0.0.%2a)與ApplicationRevision(修訂號,如: 2)合併成最終發行版號,寫入最小必要版本,再寫入部署資訊清單內的最小必要版本屬性。

4. 儲存並關閉csproj -> 發行後檢視部署資訊清單(xxx.application) -> 檢視minimum required version是否有值且正確。


參考資料:
http://stackoverflow.com/questions/202491/automatically-increment-minimum-required-version-in-a-clickonce-deployment
http://stackoverflow.com/questions/202491/automatically-increment-minimum-required-version-in-a-clickonce-deployment

2015年11月26日 星期四

[.NET] VS錯誤訊息出現「重試次數超過10次。作業失敗。」、「由於另一個處理序正在使用檔案,所以無法存取該檔案」

說明:
使用VS 2010建置專案出現:
無法將 "obj\x86\Debug\xxx.exe" 複製到 "bin\Debug\xxx.exe"。
重試次數超過 10 次。作業失敗。
無法將檔案 "obj\x86\Debug\xxx.exe" 複製到 "bin\Debug\xxx.exe"。
由於另一個處理序正在使用檔案 'bin\Debug\xxx.exe',所以無法存取該檔案。


做法:
這是因為xxx.exe正在process中,將該開啓的程式關掉與關閉工作管理員-處理程序中的process,若還是不行,先清除專案再至Debug目錄下,將xxx.exe刪除再建置。

2015年11月11日 星期三

[.NET] Release建置卻無出現參考的dll檔

說明:使用VS 2010(.NET Framework 4.0),建置程式後,發現參考的dll沒進到Release資料夾內?


原因:因VS 2010加入參考多了內嵌Interop型別,預設為True,導致複製到本機變為False,因此無輸出dll至Release資料夾內,發行時造成某些功能無法正常運作。將內嵌Interop型別改回False,複製到本機即連動為True,重新建置即可。



2014年6月11日 星期三

[C#] 加入參考Microsoft.office.core的dll


做法:
加入參考->選取COM tab ->找到Microsoft Office (1x.0) Object Library


參考資料:
http://social.msdn.microsoft.com/Forums/en-US/6fef9910-fed8-4b13-9366-66d24c0966a7/microsoftofficecoredll-location