简体   繁体   中英

C# MVC Core 5 Site throws encoding error when trying to open a connection to Azure SQL DB

As stated above. Under IIS Express on VS2019 I have no issues. When opening the site after deployment to Azure I get:

"The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature."

I initially tried adding every permutation of <meta charset="UTF-8"/> I found to no avail. Eventually I tracked the error down (by removing lines of code until the error no longer appeared) to firing when I tried to open a SqlConnection.

public DataTable FillDatatable(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            
            using (SqlConnection connect = new SqlConnection(CONST.CONN))
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                cmd.Connection = connect;
                connect.Open();    //--  Error throws when this is in the code
                da.Fill(dt);      //--  Error throws when this is in the code as this calls the
                                  //--  line prior innately.
            }
            
            return dt;
        }

Cannot figure out why for the life of me.

TL;DR - Encoding error thrown on SqlConnection.Open() when running from published Azure Web App but works perfectly under IIS Express in VS2019.

EDIT-

Quick and dirty connection string:

public const string CONN = "Data Source=MYDBADDRESS;Initial Catalog=Primary;Persist Security Info=True;User ID=USERNAME;Password=PWD;";

appsettings.json has only Logging information and "AllowedHosts"="*"

I never set any Firewall policies. Looking at Firewalls in the Portal there are none.

Site is very, very basic. Just trying to get it up and running before I continue any further and this data blocker is bringing me no joy. :/

Sql server needs to set firewall policy be default, so I assume that after deploying app to azure web app, ip address must change and may lead to some error.

@Destroigo here met the firewall problem. Congratulations to solve it:)

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