Reputation: 463
I am trying to use the following code to query a database from a Blazor web page, the exception is as follows
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: 44 - Could not compose Service Principal Name (SPN) for Windows Integrated Authentication. Possible causes are server(s) incorrectly specified to connection API calls, Domain Name System (DNS) lookup failure or memory shortage)
System.PlatformNotSupportedException: System.Net.Dns:GetHostByName is not supported on this platform.
at System.Net.Dns.GetHostByName (System.String hostName) <0x38c58e8 + 0x0001c> in :0 ...
When I run the same code in a traditional web or console app the connection opens/closes instantly.
public class MySQLHelper
{
public static SqlConnection GetConnection()
{
string m_connectionString = "connection string here";
SqlConnection sqlConn = new SqlConnection(m_connectionString);
sqlConn.Open(); // fails to open when called from a regular registered 'service' in Blazor
return sqlConn;
}
}
Thoughts?
Upvotes: 0
Views: 3805
Reputation: 273244
System.PlatformNotSupportedException:
Indicates that you are trying this from a Blazor WebAssembly app.
A lot of API's are not (cannot be) supported on Wasm, Blazor is limited to the Browser security rules.
You cannot access a database directly from WebAssembly (or JavaScript).
Either use the JS IndexDb for small DBs on the client computer (that means not shared between users) or use a Server for Data access. Take a look at the "Blazor Server" or the "Blazor WebAssembly Hosted" project models.
Upvotes: 4