網頁

2017年7月21日星期五

ASP.NET Core / CentOS / Apache 問題集

前天以為安裝好DotNET Core至CentOS,看到ASP.NET頁面,應該就可以專心進入開發程序。
但這兩天預到的問題實在太多,由Visual Studio本身對.NET Core的兼容性,.NET Core本身的問題,到Project設定,再到Apache等等問題,我都花了不少時間解決。
始終.NET Core還是很新的產品,Google後都很難找到實際解決方法,所以我把預到的問題和解決方法寫下來,希望幫到其他人。

開發環境可以參照我前天所寫的ASP.NET Core + CentOS 7 + Apache安裝攻略
Error 1 - The project has not been restored or restore failed - run `dotnet restore`
一次就出現4個Error Message:

Severity Code Description Project File Line Suppression State
Error 3. You may be trying to publish a library, which is not supported. Use `dotnet pack` to distribute libraries. SkinCV C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets 262
Error 2. The project does not list one of 'win10-x64, win81-x64, win8-x64, win7-x64' in the 'runtimes' section. SkinCV C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets 262
Error 1. The project has not been restored or restore failed - run `dotnet restore` SkinCV C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets 262
Error Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible with one of the target runtimes: 'win10-x64, win81-x64, win8-x64, win7-x64'. Possible causes: SkinCV C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets 262

這個錯誤大多數出現在當你以Core 1.0.0開新Project後,做第一次Nuget Package更新到v1.0.1後,就會出現。
幸好這個問題已經在Github上有人報告issue #2442
解決方法是打開project.json,加上:
"runtimes": {
    "win10-x64": {},
    "win8-x64": {} 
},
之後找出"Microsoft.NETCore.App",修改為
"Microsoft.NETCore.App": {
    "version": "1.0.1",
    "type": "platform"
}
或者你會發現,在dependencies裡同樣都有"Microsoft.NETCore.App": "1.0.1",我自己就把這個刪除,但大家視乎情況而定。

Error 2 - .Net Core CLI建立不到Controller
在CLI執行Command:
dotnet restore
dotnet aspnet-codegenerator controller -name SignupformController -m Signupform -dc SkinCVContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
No executable found matching command "dotnet-aspnet-codegenerator"
Error Message:

No executable found matching command "dotnet-aspnet-codegenerator"

stackoverflow上有人討論。
但有兩個解決辦法應該都是正確的。
Visual Studio 2017 解決方法:
csproj file加上:

        
 
Visual Studio 2015 解決方法:
在project.json中的dependencies部份加上:
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
  "version": "1.1.0-preview4-final",
  "type": "build"
},
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
  "type": "build",
  "version": "1.1.0-preview4-final"
}
在project.json中的tools部份加上:
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
  "version": "1.1.0-preview4-final",
  "imports": [
    "portable-net45+win8"
  ]
}
與此同時,你會掉進dependency hell

或者你會想到"為什麼不用Nuget Package Manager安裝呢?"
我自己的情況就是這樣,用Nuget安裝會出Error Message,直接修改project.json就沒問題。

Error Package 'Microsoft.VisualStudio.Web.CodeGeneration.Tools 1.0.1' uses features that are not supported by the current version of NuGet.
To upgrade NuGet, see http://docs.nuget.org/consume/installing-nuget.


這裡我都搞了很久,就如之前所說,.NET Core和相關dependencies版本暫時還算混亂,上述的"CodeGeneration.Tools"和"CodeGeneration.Mvc",
你在Nuget上看就會見到不同版本適用於不同的Microsoft.NETCore.App你不能隨便找個版本用,一定要符合兩者跟"Microsoft.NETCore.App"的版本要求,而CodeGeneration.*兩者的版本要一致
附上三個Package Nuget連結自己對一對版本,我試過很多組合,只可以說新不一定好。
  1. Microsoft.VisualStudio.Web.CodeGeneration.Tools
  2. Microsoft.VisualStudio.Web.CodeGenerators.Mvc
  3. Microsoft.NETCore.App

Error 3 - Could not load file or assembly 'Microsoft.DotNet.InternalAbstractions
Error 2的問題相信你搞完一輪,一切準備就緒,拍下Enter鍵。沒錯!又Error!整個.NET Core CLI崩潰!
Error Message:

Unhandled Exception: System.IO.FileNotFoundException:
Could not load file or assembly 'Microsoft.DotNet.InternalAbstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

不過這個就簡單得多,在project.json中的dependencies部份加上這句就行:
"Microsoft.DotNet.InternalAbstractions": "1.0.0"


Error 4 - Solution Explorer missing (Add Controller / View) menu item in context menu
這個問題其實跟上述Error 2有關,就是Solution Explorer right-click menu沒有[Add Controller/View]的選項做Scaffolding。
一開始我以為沒有是正常的,所以至會用原始方法(所以才經歷到Error 2)。


但當我搞好Error 2後,我發現與此同時[Add Controller/View]都出現了。

要操作正常之前提到採用的"Microsoft.VisualStudio.Web.CodeGeneration.Tools"和"Microsoft.VisualStudio.Web.CodeGeneration.Mvc"的版本尤關重要。
否則就會出現:

System.NullReferenceException: Object reference not set to an instance of an object.



Error 5 - 中文字於頁面中出現亂碼
因為你的檔案編碼不是UTF-8,你可以試試同時打開"/Views/Home/Index.cshtml"和你出現亂碼的View,再去VS的Menu>File>Advanced Save Options看看檔案編碼就知道問題所在。
這源自.NET Core Tool的錯誤,因為所建立的檔案會跟你的系統一樣,以我為例就是[Traditional Chinese (Big5) - CodePage 950]
所以解決方法是選擇[Unicode (UTF-8 without signature) - Codepage 65001]就OK。


Error 6 - invalid operationexception _ValidationScriptsPartial.cshtml was not found
執行Project時,出現以下錯誤:

InvalidOperationException: The partial view '_ValidationScriptsPartial' was not found. The following locations were searched:
/Views/Movies/_ValidationScriptsPartial.cshtml
/Views/Shared/_ValidationScriptsPartial.cshtml


解決方法:
自行建立"_ValidationScriptsPartial.cshtml",貼上下面的代碼,儲存至"Views/Shared" folder。

    
    


    
    


Error 7 - No executable found matching command "dotnet-bundle"
Error Message:

dotnet publish -c release -r centos.7-x64
Publishing SkinCV for .NETCoreApp,Version=v1.0/centos.7-x64
No executable found matching command "dotnet-bundle"

原因是project.json中沒有"BundlerMinifier.Core"或版本不對。
在project.json加入:
"BundlerMinifier.Core": "2.0.238",
同樣要自行設置版本,若你的"Microsoft.NETCore.App"是1.0.1就用跟我一樣的2.0.238。

Error 8 - No executable found matching command "bower" / "gulp"
Error Message:

dotnet publish -c release -r centos.7-x64
Publishing SkinCV for .NETCoreApp,Version=v1.0/centos.7-x64
No executable found matching command "bower"

除了Nuget,Core還會用到叫bower的package manager,類似node.js的NPM。
在Windows下,打開CMD,執行:
npm install -g bower
npm install -g gulp

結果應該如下:
Microsoft Windows [版本 10.0.14393]
(c) 2016 Microsoft Corporation. 著作權所有,並保留一切權利。

C:\Users\SamSung>npm install -g bower
npm WARN deprecated bower@1.8.0: ..psst! While Bower is maintained, we recommend Yarn and Webpack for *new* front-end projects! Yarn's advantage is security and reliability, and Webpack's is support for both CommonJS and AMD projects. Currently there's no migration path, but please help to create it: https://github.com/bower/bower/issues/2467
C:\Users\SamSung\AppData\Roaming\npm\bower -> C:\Users\SamSung\AppData\Roaming\npm\node_modules\bower\bin\bower
bower@1.8.0 C:\Users\SamSung\AppData\Roaming\npm\node_modules\bower

C:\Users\SamSung>npm install -g gulp
npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated minimatch@0.2.14: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated graceful-fs@1.2.3: graceful-fs v3.0.0 and before will fail on node releases >= v7.0. Please update to graceful-fs@^4.0.0 as soon as possible. Use 'npm ls graceful-fs' to find it in the tree.
C:\Users\SamSung\AppData\Roaming\npm\gulp -> C:\Users\SamSung\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js
gulp@3.9.1 C:\Users\SamSung\AppData\Roaming\npm\node_modules\gulp
├── interpret@1.0.3
├── pretty-hrtime@1.0.3
├── deprecated@0.0.1
├── archy@1.0.0
├── tildify@1.2.0 (os-homedir@1.0.2)
├── minimist@1.2.0
├── v8flags@2.1.1 (user-home@1.1.1)
├── chalk@1.1.3 (supports-color@2.0.0, escape-string-regexp@1.0.5, ansi-styles@2.2.1, has-ansi@2.0.0, strip-ansi@3.0.1)
├── semver@4.3.6
├── orchestrator@0.3.8 (stream-consume@0.1.0, sequencify@0.0.7, end-of-stream@0.1.5)
├── gulp-util@3.0.8 (array-differ@1.0.0, object-assign@3.0.0, lodash._reinterpolate@3.0.0, lodash._reescape@3.0.0, lodash._reevaluate@3.0.0, array-uniq@1.0.3, beeper@1.1.1, replace-ext@0.0.1, dateformat@2.0.0, has-gulplog@0.1.0, fancy-log@1.3.0, gulplog@1.0.0, vinyl@0.5.3, lodash.template@3.6.2, through2@2.0.3, multipipe@0.1.2)
├── liftoff@2.3.0 (lodash.isstring@4.0.1, lodash.isplainobject@4.0.6, rechoir@0.6.2, flagged-respawn@0.3.2, extend@3.0.1, lodash.mapvalues@4.6.0, resolve@1.3.3, fined@1.1.0, findup-sync@0.4.3)
└── vinyl-fs@0.3.14 (strip-bom@1.0.0, graceful-fs@3.0.11, vinyl@0.4.6, defaults@1.0.3, mkdirp@0.5.1, through2@0.6.5, glob-stream@3.1.18, glob-watcher@0.0.6)

其實我遇到的問題還未止於此,Deploy時還有其他問題,在另一個Post再寫。

沒有留言:

發佈留言