I have noticed that over the last year while my app has been in production, it sometimes just switches off.
When I debug the web api project locally and use the connection string of the database on Azure, it works.
Whenever I then publish the project to Azure, it says Error 500, can't handle this request. Even with the connection string hard coded in the appsettings.json.
What I did notice though was when I changed firewall settings on the database server recently, that's when it switched off. I don't know if that's the source.
What could the problem be?
And would you think it, it was the server IP address.
I had to find the virtual IP address of the web app under Properties in the left pane and add it to the server rules of the database.
This question already has answers here:
Can't connect to SQL Server express from .net core app running on docker
(4 answers)
Closed last month.
I have a ASP.NET Core 2.2 project for which I turned on docker support. The project runs fine as long as it does not require database connection. For e.g Login. When I enter user credentials, I get the error as below.
An unhandled exception occurred while processing the request.
Win32Exception: A connection attempt failed because the connected
party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond
Unknown location SqlException: 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: TCP Provider, error: 0 - A connection attempt
failed because the connected party did not properly respond after a
period of time, or established connection failed because connected
host has failed to respond.)
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, object providerInfo,
bool redirectedUserInstance, SqlConnectionString
userConnectionOptions, SessionData reconnectSessionData, bool
applyTransientFaultHandling) InvalidOperationException: An exception
has been raised that is likely due to a transient failure. If you are
connecting to a SQL Azure database consider using
SqlAzureExecutionStrategy.
Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy+d__7.MoveNext()
Previously the error was different when TCP/IP was not enabled in my sql server configuration manager.(Do not remember error)
I followed the steps mentioned in the link https://jack-vanlightly.com/blog/2017/9/24/how-to-connect-to-your-local-sql-server-from-inside-docker to solve that issue.
I have enabled TCP/IP and named pipes as well. Also I can connect using IP from SQL Management Studio.
Current connection string:
"ConnectionStrings": {
"DefaultConnection": "Server=xxx.xx.xx.x,1433;Database=TestDB;User ID=username;Password=pwd;Trusted_Connection=True;MultipleActiveResultSets=true"
},
Please let me know if I am missing any step. I want to connect to a local sql from my docker project
This took me some time to work out, there are a number of small issues that may be wrong, which makes it frustrating. This can get even more frustrating as the .net core web application visual studio 2017 project that gets auto-generated doesn't work straight out of the box. Having this experience as your first exposure to Docker isn't ideal.
I initially also had the incorrect assumption that the docker image would come with SQL Server inside the container, this is not the case, it's trying to access the database server that must be pre-installed on the host machine.
Steps to follow (tested for a windows docker image, rather than linux);
Get the IP address of your host machine, by running a command prompt and typing IPCONFIG
Set the database connection string within you appsettings.json file to this Ip address, followed by the SQL Server port number, i.e.;
192.168.X.X,1433
Set the connection string not to use Trusted_Connection (delete this from the connection string) and hard code in the User Id and Password;
User Id=sa;Password=SuperSecurePassword;
If I didn't do this, on certain SQL Server configurations I'd get an unusual error. EDIT: the error
Cannot authenticate using Kerberos. Ensure Kerberos has been initialized on the client with 'kinit' and a Service Principal Name has been registered for the SQL Server to allow Kerberos authentication.
Connection String show look something like this;
"Server=192.168.X.X,1433;Database=MyDatabase;User Id=sa;Password=SuperSecurePassword;MultipleActiveResultSets=true"
now on the host machine you need to open up the windows firewall, and add a new inbound connection rule, for TCP port 1433.
After all this, and it still doesn't work, try a reboot; I struggled for a long time and had to reboot, which brought it into life; I'm not sure Docker had initialised correctly, this is mostly speculation though! I don't really like to say rebooting is a fix for problems, it's not really an answer, but in certain cases it does the trick.
edit, one final thing, running visual studio in administrator mode (right click icon, "run as administrator") helps too.
Apologises for the resurrection of an old thread, but this issue seems to still exist and the information available is a bit patchy on how to fix this, considering the small number of things that need to be done.
So what has worked for me is:
public static string DockerHostMachineIpAddress => Dns.GetHostAddresses(new Uri("http://docker.for.win.localhost").Host)[0].ToString();
docker.for.win.localhost is an address that inner dns of docker will resolve as localhost (not localhost inside docker but the hosting machine). The above code provides u with an ip address of your localhost and you are able to create connection string like this:
$"Server={DockerHostMachineIpAddress}\\SQL2017;Database=YourDB;User Id=Admin;Password=123;"
This will work if you are using named sql server instance - replace SQL2017 with ur instance name and crediatials with correct user.
HOWEVER!
For migrations and update-database localhost works fine. Probably since its not handled from docker container.
EDIT:
Second solution is a bit cleaner but you need to have a project that supports docker-compose.
In docker-compose.yml file in definition of your service below image property definition add:
extra_hosts:
- "localhost:192.168.65.2"
You still have to know what IP address (which you can find out by using the original answer) you have to add alias for, but once you know it it's a bit more clean. On more than 5 different machines that I worked on this IP is correct.
I ran into the same problem, and followed all the steps mentioned by Phil but I still could not solve this.
Finally, in addition to the steps in the specified answer, I had to enable my SQL Server for TCP protocol by following these steps.
1. Open SQL Server Configuration Manager
2. Select "SQL Server Network Configuration" > "Protocols for MSSQLSERVER"
3. Edit "TCP/IP" properties, and change "Enabled" to "Yes"
I had the same issue, followed the same approach as on the website you mentioned. It should work like that if you have the networks with standard configuration.
Simply disable the firewall for your public network.
My issue was simply, that windows seemed to classify the network created by Docker as public network and so the firewall blocked it, same as for this one:
Can't connect to SQL Server express from .net core app running on docker
It's not marked there as an answer but rather edited in the question.
There are many versions of this question on stackoverflow. I have tried a lot of the solutions suggested there - but none of these work for me.
I have a VB ASP.NET application running on IIS on Windows Server 2008. It calls a webservice. The Server running the Web Service has a self signed certificate. I have imported the Cert into the Trust Store of the IIS machine. I can see the certificate in IE->Options->Content->Certificates->Trusted Root Certification Authorities.
When the webservice is called, an exception is thrown
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
The same IIS VB ASP.NET Application runs fine on another machine with a similar setup.
These are the different basic things I have checked
The URL used to call the webservice is the same as the CN on the Cert imported into the Trusted Root CA.
Time is synced between the calling machine and the webservice host
The Cert isn't expired.
The IIS machine has Proxy settings in IIS, but Webservice Host machine as added to list of exceptions where Proxy should not be used.
I wrote a small command line test program in VB.NET calling the same webservice which runs fine.
After importing the Cert into the Trusted CA store, I have done a iisreset.
Only thing I haven't tried yet is rebooting the IIS machine - this is production machine & I cannot reboot it for at least a couple of days.
What else can I try?
Internet Explorer certificate settings apply only to the current user. It's highly unlikely that the same user you're logged in as when operating IE is the same as the one being used to host the IIS application. You could add the certificate to the trusted certs for the appropriate service account, but a simpler approach would be to add it to the "Local Computer" trusted certs.
Run Microsoft Management Console (type "mmc" in run dialog) File > Add / Remove Snap-Ins > Certificates, chose "Computer account" for the certificates to manage. This way, any user or service account on the server will see the certificate as trusted.
Alternatively, depending on how you're accessing the web-service, you could add an exception within the application itself. I would recommend code that checks the thumbprint / hash of the certificate though, as opposed to allowing it to connect to any un-trusted certificate.
I am using SQL managment studio 2008 running on an Amazon EC2 machine. I am unable to connect to the database in my asp.net application. The EC2 instance has been set to accept connections over the SQL port. I am also able to remote the machine as well as view websites hosted on the server. Listed below is part of the connection string relating to this instance. When the program is ran and this connection string is called, it returns tcp error 0 - no return response. it just times out.
<add name="ProjectServer" connectionString="Data Source=*IP ADDRESS HERE*,1433;Initial Catalog=*Catalog Name*;User ID=IP-0A6ED514\Administrator;"/>
I removed the ip and the catalog name for the example, but I am sure they are correct.
The only thing that I could think may cause an error, is the differences in names between the user id and the server name - the server name is ip-0A6ED514\sharepoint but the user name is ip-0A6ED514\administrator when I log into the sql server manager on the EC2 instance. A password is not used. Not sure if I would need to leave in a blank string for password - also not sure if the difference between server name and user id to log in makes a difference. Any help is appreciated. Thank you.
update - when this connection string is used with out the port, i get tcp provider error 40 - when the port is in there, i get error 0
edit- the sql server is using windows authentication - does this make a difference? Usually I always use SQL server authentication
edit 2- now running sql express at same location, would i need a \SQLEXPRESS tag? I feel like after looking around and what could be the error, it is that I am reaching the machine ok, but its not pointing me to the DB, any suggestions?
I was looking in posts similar to mine, but didn't find any response to my issue.
Recently the MySql server was changed, right now I'm using a different one,
I use this connection string in my asp.net vb web application project:
<add key="MySQLConnStr" value="server=xx.xxx.x.xx; user id=mysql_link; database=mysql; pooling=false; password=xxxx"/>
Ok when I run the WebApp in my computer (development environment) it works perfect, now, in the host (production env.) I get the "Unable to connect..." error.
What could be the problem here??? I just changed the old MySql server ip for the new one.
I restarted the application pools in the host, restarted the webapp as well, but nothing.
thanks in advance!
Is it possible that your MySql runs on different port number?.
(any port different from the default 3306)
If so, add to your connection string "Port=xxxx;"
EDIT: It's also possible that your MySql Administrator has not given to the ip address/name of your server the permission to connect whilst has given them to your local pc
complete connection string in c# code ..
public MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=XXXX;database=yourDBname;Port=XXXX");
Thanks