Thứ Năm, 22 tháng 9, 2022

[ASP.NET] Lỗi kết nối Database (A network-related or instance-specific...) mặc dù kiểm tra mọi thứ đã đúng

Tình huống : 

Website đã config đúng thông số kết nói database (ConnectString) đồng thời kiểm tra kết nối DB đều OK, thậm chí khi monitor website truy cập vẫn có log kết nối DB thành công vào con SQL. Tuy nhiên website vẫn bị báo lỗi 

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified).


Nguyên nhân : 

- Vì 1 số lý do gì đó website sử dụng cả khai báo connectionString mặc định của .NET Framework (được khai báo trong file C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config)

Xử lý : 

- Trong khai báo connectionString của website thêm thẻ <clear /> vào

 <connectionStrings>
    <clear />



Thứ Năm, 14 tháng 10, 2021

[ASP.NET][IIS] Error CS0016: Could not write to output file ....

 Lỗi liên quan tới compiler csc của .NET Framework

Compiler Error Message: CS0016: Could not write to output file 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\3ab7b770\3750c939\en-US\App_GlobalResources.l8frv07t.resources.dll' -- 'Access is denied. '


Với lỗi này có 2 nguyên nhân

1. Thư mục Temp của ASP.NET không có quyền write, cần kiểm tra lại
2. Thư mục C:\Windows\Temp không có quyền write. Cần kiểm tra lại

Nếu đã đảm bảo 2 yếu tố trên mà vẫn lỗi thì có thể áp dụng.

thêm vào web.config :

<identity impersonate="true" userName="devdomain\youruser" password="yourpassword" />

Sau đó cấp full quyền cho user devdomain\yourusername vào thư muc Temp của ASP.NET

Thứ Năm, 9 tháng 9, 2021

[ASP.NET][MSSQL] Web ASP truy vấn SQL chậm qua network

Tình huống khi website asp.net truy cập chậm có liên quan đến truy vấn sql khi server web và server sql thuộc 2 server khác nhau

Quá trình kiểm tra lưu lượng network nếu sử dụng bandwidth nhiều mỗi lần có truy vấn database . Và kiểm tra Resource Waits trên SQL chỉ số Network I/O cao, có nghĩa là ứng dụng (application) không sử dụng hết lưu lượng dữ liệu mà SQL trả về, dẫn tới bị nghẽn cổ chai. mặc dù bandwidth network còn rất rộng và chưa sử dụng hết.

Để cải thiện, có thể kiểm tra cài đặt MARS trong ConnectionString.

MultipleActiveResultSets=True

Nếu có sử dụng thông số này thì bỏ đi hoặc set lại thành False.


Kếu quả Test thực tế trước và sau khi bỏ MultipleActiveResultSets : TTFB trang web giảm 1 nửa, bandwidth network tăng gấp đôi.




[ASP.NET] Xử lý lỗi Could not load file or assembly “*****” or one of its dependencies.

I. Lỗi Could not load file or assembly “*****” or one of its dependencies. Access is denied. ví dụ như ảnh dưới


Lỗi này là Access is denied nên không liên quan tới việc sai version.  Nếu sai version khai báo trong code thì lỗi sẽ phải có nội dung như sau “The located assembly’s manifest definition does not match the assembly reference”

Access is denied : xảy ra do 1 số nguyên nhân sau.


  • File dll bị lock truy cập, có thể kiểm tra process iis hoặc antivirus
  • Thư mục ASP.NET Temp không có quyền ghi đối với user chạy web (lỗi này thường gặp hơn)

Thư mục ASP.NET Temp mặc định sẽ ở đường dẫn C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

hoặc :

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

Cần kiểm tra quyền write/read cho user chạy web (là user indentity của pool chứa website) cho thư mục ASP.NET Temp trên

Một số trường hợp thử mục ASP.NET Temp này sẽ được khai báo lại, không sử dụng thư mục mặc định của Window nữa. Ví dụ sử dụng Plesk Obsidian 18.x , xem trong web.config của site sẽ có mục khai báo như sau

<compilation tempDirectory=”D:\vhosts\aireal.vn\tmp” />

Với trường hợp này thì cần set quyền read/write cho user chạy web (là user indentity của pool chứa website) lên thư mục temp được khai báo trong web.config của website



II. Lỗi Could not load file or assembly ‘*****’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference.


The located assembly’s manifest definition does not match the assembly reference : xảy ra do nguyên nhân là


  • phiên bản (version) của file thư viên (dll) không khớp với khai báo trong web.config.

Lỗi này thường xảy ra khi update code mới nhưng quyên khai báo lại phiên bản thư viện cho đúng với file đang sử dụng.

Trường hợp lỗi này bắt buộc phải sửa lại khai báo trong web.config cho đúng mới chạy được

  • Server không hỗ trợ thư viện mà Khách hàng sử dụng. Trường hợp này là những thư viện bắt buộc phải register vào registry của Window, đây là những loại thư viện bắt buộc phải setup trên server , ví dụ như thư viện của bộ ứng dụng văn phòng Office.

Could not load file or assembly ‘Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference

III . Could not load file or assembly ‘**** or one of its dependencies. The system cannot find the file specified


The system cannot find the file specified : Xảy ra khi bị thiếu file thư viện. Nguyên nhân thường do khi upload source bị thiếu file, cần kiểm tra thông báo lỗi tên gì và đối chiếu với source xem có file đó tồn tại trong thư mục bin của source code hay không

Thứ Tư, 4 tháng 8, 2021

[ASP.NET] kiểm tra , chuẩn đoán lỗi net::ERR_HTTP2_PROTOCOL_ERROR

 Với lỗi net::ERR_HTTP2_PROTOCOL_ERROR  xảy ra với ajax jquery XMLHttpRequest  thông thường nguyên nhân không liên quan tới HTTP2. 

Nguyên nhân chính dẫn tới lỗi này thường là do lỗi khi thực thi code trên website, hoặc do bị reset connection.

Nếu disable HTTP2 và để chạy HTTP1.1 mà theo dõi có phát sinh lỗi net::ERR_CONNECTION_RESET  thì nguyên nhân chính sẽ là do thời điểm đó kết nối bị mất không có phản hồi từ server dẫn tới code ajax timeout không nhận được responsed và trả về lỗi net::ERR_HTTP2_PROTOCOL_ERROR


Các bước để chuẩn đoán :

- Kiểm tra window eventlog "Application" xem có lỗi gì liên quan website hay không

- Kiểm tra window eventlog "System" xem có thông tin liên quan tới lỗi application pool của website hay không (Source WAS). nếu có log như sau thì đây là nguyên nhân

A process serving application pool 'xxx' suffered a fatal communication error with the Windows Process Activation Service. The process id was 'yyy'. The data field contains the error number.

Để kiểm tra chi tiết hơn thì cần sử dụng công cụ DebugDiag, chọn mục "Crash" -> "A Special IIS Web Application pool". Sau đó chọn pool để theo dõi. Khi nào có dump file thì dùng DebugDiag Analysis để mở lên xem chi tiết . Ví dụ như ảnh dưới



Tham khảo thêm

31623G9043 : lỗi do thiếu quyền ghi log file

39VTD27590 : Lỗi code gây crash pool.

Thứ Hai, 21 tháng 6, 2021

[ASP.NET] Read , Write Excel file với thư viện của MS Office

Một số lập trình viên hay sử dụng bộ thư viện Interop.Microsoft.Office.Interop.Excel.dll của MS Office để xử lý file Excel (VD : import, export dữ liệu từ file Excel)

Với thư viện này bắt buộc trên máy chủ phải có cài đặt (install) MS Office. Nếu không khi chay sẽ báo lỗi như bên dưới 

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))


Đối với thư viện Microsoft cũng khuyên cáo không sử dụng và không được Microsoft hỗ trợ để chạy cùng với các ứng dụng ngoài khác (bao gồm ASP, ASP.NET )


"Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment."

https://support.microsoft.com/en-us/topic/considerations-for-server-side-automation-of-office-48bcfe93-8a89-47f1-0bce-017433ad79e2

Nếu bạn buộc phải sử dụng thư viện này và không thể thay đổi thì cần sử dụng VPS riêng để có thể cài đặt MS Office. 

Hoặc có thể tham khảo các thử viện khác hỗ trợ thao tác trên file Excel

https://nugetmusthaves.com/Tag/excel

https://www.codeproject.com/Articles/1088970/Read-Write-Excel-file-with-OLEDB-in-Csharp-without

https://github.com/ExcelDataReader/ExcelDataReader



[Linux] Một số câu lệnh kiểm tra server khi bị tấn công DDoS

– Kiểm tra số connection trên port 80:
Code:
netstat -n | grep :80 |wc -l
 
– Kiểm tra số lượng connection đang ở trạng thái SYN_RECV:
Code:
netstat -n | grep :80 | grep SYN_RECV|wc -l
 
– Hiển thị tất cả các IP đang kết nối và số lượng kết nối từ mỗi IP:
Code:
netstat -an|grep :80 |awk '{print $5}'|cut -d":" -f1|sort|uniq -c|sort -rn
 
Nếu muốn kiểm tra IP nào mở nhiều SYN thì thêm vào:
Code:
netstat -an|grep :80|grep SYN |awk '{print $5}'|cut -d":" -f1|sort|uniq -c|sort -rn
 
– Đối với server có nhiều IP, để kiểm tra IP nào đang bị tấn công:
Code:
netstat -plan  | grep  :80 | awk '{print $4}'| cut -d: -f1 |sort |uniq -c