Azure cosmosdb mongo api error ion asp.net core - asp.net-core-2.0

I am getting the below error when I try to connect to Azure cosmosdb mongo api from my asp.net core :
Error: TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector
Code used:
readonly string _cn = #"mongodb://testdb12321:f2iORCXklWzAHJLKbZ6lwesWU32SygfUN2Kud1pu1OtMBoe9lBPqHMWdbc0fF7zgl7FryTSCGv7EGCa6X8gZKw==#testdb12321.documents.azure.com:10255/?ssl=true&replicaSet=globaldb";
var client = new MongoClient(_cn);
var db = client.GetDatabase("researchdb");
var cns = db.ListCollectionNames().First();

Related

Azure Cosmos DB client throws "HttpRequestException: attempt was made to access a socket in a way forbidden by its access permissions" underneath

I use CosmosClient from SDK Microsoft.Azure.Cosmos 3.28.0 in ASP.NET Core 3.1 in Azure Durable Function. This client is getting and sending data from/to my cosmos instance (Core (SQL)) and it works fine but I see that it constantly throws exception in following http request for metadata
GET 169.254.169.254/metadata/instance
System.Net.Http.HttpRequestException: An attempt was made to access a socket in a way forbidden by its access permissions.
I use following configuration:
private static void RegisterCosmosDbClient(ContainerBuilder builder)
{
builder.Register(c => new SocketsHttpHandler()
{
PooledConnectionLifetime = TimeSpan.FromMinutes(10), // Customize this value based on desired DNS refresh timer
MaxConnectionsPerServer = 20, // Customize the maximum number of allowed connections
}).As<SocketsHttpHandler>().SingleInstance();
builder.Register(
x =>
{
var cosmosDbOptions = x.Resolve<CosmosDbOptions>();
var socketsHttpHandler = x.Resolve<SocketsHttpHandler>();
return new CosmosClient(cosmosDbOptions.ConnectionString, new CosmosClientOptions()
{
ConnectionMode = ConnectionMode.Direct,
PortReuseMode = PortReuseMode.PrivatePortPool,
IdleTcpConnectionTimeout = new TimeSpan(0, 23, 59, 59),
SerializerOptions = new CosmosSerializationOptions()
{
PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
},
HttpClientFactory = () => new HttpClient(socketsHttpHandler, disposeHandler: false)
});
})
.AsSelf()
.SingleInstance();
}
I also tried approach with passing IHttpClientFactory from this blog but it didn't help.
It looks like there are no new sockets available in your environment therefore you are getting the socket forbidden error. Please review how to manage connection for Azure Cosmos DB clients and you should use a singleton Azure Cosmos DB client for the lifetime of your application to resolve the issue. In case if you still facing the issue leveraging the singleton object then please let me know so we can further review it.
That particular IP and path is for https://learn.microsoft.com/azure/virtual-machines/windows/instance-metadata-service?tabs=windows
The SDK is attempting to detect the Azure information. It could mean for Durable Functions, this information and endpoint is not available.
This does not affect SDK operations and should not block you from performing other actions on the CosmosClient instance.

Connection pool setting in node.js app which connects to Azure GraphDB and query using gremlin

Following this tutorial where a node.js app connects to Azure cosmos - GraphDB and query the GraphDB using gremlin https://learn.microsoft.com/en-us/azure/cosmos-db/create-graph-nodejs
Created an app.js as below. This is working fine. I can submit a gremlin query and get the result. Can I set a connection pool using this gremlin library? I couldn't find any source on how to set up a fixed number of the connection pool and use a connection from the pool and send the connection to the pool back after getting a result or after a fixed time out delay.
package.json
"gremlin": "^3.3.4"
app.js
const gremlin = require('gremlin');
const endpoint = 'endpoint';
const port = 'port';
const username = 'username';
const password = 'password';
const url = `wss://${endpoint}:${port}/gremlin`;
const authenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator(
username,
password
);
graphDBClient = new GremlinClient(url, {
authenticator,
traversalsource: 'g',
rejectUnauthorized: true,
mimeType: 'application/vnd.gremlin-v2.0+json'
});
graphDBClient.submit(gremlinQuery);

504 Gateway Time-Out 240000 ms when using EventSource

I'm using this node.js library in order to Reconnect to an EventSource in case it closes.
The Problem
My web app is hosted on Microsoft Azure , in which i've enabled Web Sockets
After 240000ms the event source object closes with a 504 Gateway time-out error.
Web app architecture:
The project is designed with the Spring boot framework and this is one of my Streaming Endpoints
#GetMapping(path = "/completed/receive", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Event> receiveCompleted() {
return Flux.create(sink -> {
completedProcessor.register(sink::next);
});
}
Headers I've tried and didn't work
Connection : Keep-Alive
Keep - Alive: timeout=500000
Enable Web Sockets from Microsoft Azure
How i'm listening to the endpoint with JS
var eventSourceCompleted;
eventSourceCompleted = new ReconnectingEventSource("/api/completed/receive");
// Receive messages
eventSourceCompleted.onmessage = function (evt) {
//Do Something
};
eventSourceCompleted.onerror = function (e) {
console.log("EventSource failed: " + e);
};
No matter what i try, 240k ms later the connection closes, how can that be?

Cannot connect to Azure Redis after changing Minimum TLS version to 1.2

In my .NET Framework 4.6.1 application I am using StackExchange.Redis.StrongName 1.2.6 to connect to Azure Redis.
This is the code
public RedisContext(string connectionString = null)
{
if (connectionString == null) return;
Lazy<ConfigurationOptions> lazyConfiguration
= new Lazy<ConfigurationOptions>(() => ConfigurationOptions.Parse(connectionString));
var configuration = lazyConfiguration.Value;
configuration.SslProtocols = SslProtocols.Tls12;//just added
configuration.AbortOnConnectFail = false;
Lazy<ConnectionMultiplexer> lazyConnection =
new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(configuration));
_connectionMultiplexer = lazyConnection.Value;
LogProvider.IsDisabled = true;
var connectionEndpoints = _connectionMultiplexer.GetEndPoints();
_lockFactory = new RedisLockFactory(connectionEndpoints.Select(endpoint => new RedisLockEndPoint
{
EndPoint = endpoint,
Password = configuration.Password,
Ssl = configuration.Ssl
}));
}
In Azure, I have changed the Redis resource to use TLS1.2 and in code I have added this line:
configuration.SslProtocols = SslProtocols.Tls12;//just added
And now, nothing works anymore. This is the error I get in Application Insights:
Error connecting to Redis. It was not possible to connect to the redis server(s); ConnectTimeout
I have also tried to add ",ssl=True,sslprotocols=tls12" to the redis connection string, but with the same result.
Try referencing StackExchange.Redis instead of StackExchange.Redis.StrongName. I have done that in a few of my projects and now it works. However some 3rd party still use StrongName rather than the normal redis one. StackExchange.Redis.StrongName is now deprecated. https://github.com/Azure/aspnet-redis-providers/issues/107. I assume you are trying to connect to Azure Redis in relation to them stopping TLS 1.0 and 1.1 support?

switching mongodb on request auth issue

i am developing a multitenant app in node that switches db based on the request.
i am using mongoose to create the connection to the mongo deployment where i have 3 dbs.
all my code is written in coffeescript.
here is how i create the initial connection:
conn = mongoose.createConnection('mongodb://<user>:<pwd>#<host>:<port>,<host>:<port>/main?replicaSet=set-xxxxxxxxxx');
here the code in the request
db = conn.useDb('demo')
myModel = db.model('mymodel')
for obj in objects
o = new settingModel(obj)
o.save (err, obj) ->
console.log 'err is', err if err
i can switch db and query the db but when i try to write to it i get a:
errmsg: 'not authorized on demo to execute command { insert: "settings", writeConcern: { w: 1 }...
how can i solve this issue? the databases are all hosted on compose.io

Resources