C# MongoDB set ReadPreference - c#

Probably right in front of me, but I am not sure how to set the ReadPreference.Secondary setting in the C# driver? I wanted to distribute the query load to my secondary nodes as opposed to the default. I have set slaveOk() on the nodes themselves.
Any help / example would be appreciated. I just can't find a good example of setting that property.
Thanks,
S
EDIT: So maybe ReadPreference hasn't been implemented in C# driver yet...that looks to be the case. So then I would use slaveok?
Something like one of the below?:
var mongoServer = MongoServer.Create("mongodb://localhost/?
replicaSet=myset;slaveOk=true");
var db = mongoServer.GetDatabase("MyDb");
var coll = db.GetCollection("MyColl");
or
var cs= db.CreateCollectionSettings<BsonDocument>("Users");
cs.SlaveOk = true;
var coll = db.GetCollection(cs);
EDIT2:
Looks like I might need to modify connection string to decorate each Mongo Instance as well?
mongodb://serverA:27017,serverB:27017,serverC:27017/?safe=true;replicaset=myreplicaset;slaveok=true

Yes, ReadPreferences have not been implemented in the C# driver. We at 10gen have been waiting to implement ReadPreferences until all the drivers, including mongos, could all implement at the same time. This support should come approximately at the time of server release 2.2.

Related

Create SQL linked server with a .net class (C#)

I have a program where I used "SQLConnectionStringBuilder" and it was quite easy and handy to do use.
Now, I would like to use the linked server class from Microsoft in order to create a linked server easily. However, I am a bit stuck trying to understand the documentation because it has no examples.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.management.smo.linkedserver?view=sql-smo-preview
So far I thought it would be something like below:
LinkedServer linkedServer = new LinkedServer();
CreateLinkedServer(string server, string database)
{
linkedServer.Name = "NewLinkedServer";
linkedServer.DataSource = server;
linkedServer.Catalog = database;
}
At the moment it gives the following error:
you must set Parent property.
I believe I am missing the authentication mode (and probably something else) to use in order to connect but I've only have seen in the list the "LinkedServerLogins" property which I am not sure how to use.
Can somebody help me?
Thanks in advance

Azure Redis - Key Wildcard using pattern matching?

As the title suggests, I am wondering if there is an efficient way to leverage the SCAN/Pattern. I am using c# .net core. When I search into the IDatabase, I can't really find anything other than SetScan that would allow for a pattern, but this is clearly not the correct thing. This is a bit unusual to me as the redis console in Azure Redis does support the concept of SCAN.
I need to be able to pass a partial key here:
Assume I can store keys like this: "SID_{0}_CID_{1}_OID_{2}_BID_{3}"
///BUT when I call Redis, I might only have part of the key
RedisValue value = await _redisConnection.BasicRetryAsync(async (db) => await db.StringGetAsync(key)); //<-- key needs to be like '*CID_123*BID_333*'
if(value.HasValue)
return value;
return string.Empty;
db implements IDatabase which implements IRedis and IDatabaseAsync
Anyone have an idea how I might achieve this? Python guy seems to have this available but I can't find similar.. Redis wildcard keys on get
I ended up doing the following:
public async Task<RedisValue[]> GetItems(string customPattern)
{
var endpoints = _connection.GetEndPoints();
var s= _connection.GetServer(endpoints[0]);
var keys = s.Keys(pattern: customPattern).ToArray();
return await _database.StringGetAsync(keys);
}
to riff the Northern Pikes... 'it ain't pretty it just looks that way.'

ArangoDB create edge collection

I tried create collection of edges in c# via ArangoDB-NET driver (https://github.com/yojimbo87/ArangoDB-NET) and this code not working.. My code:
var response = dbClient.Collection.Type(ACollectionType.Edge).KeyGeneratorType(AKeyGeneratorType.Autoincrement).WaitForSync(true).Create(
"EdgesCollection");
dbClient is ADatabase object.
Collection is created but document type is not edge. How can I do?
I'm not familiar with that driver, and your question seems to be related to implementation, not DB functionality. However, my best guess would be to omit the KeyGeneratorType and WaitForSync options:
var cType = ACollectionType.Edge;
var cName = "EdgesCollection";
var response = dbClient.Collection.Type(cType).Create(cName);
If this fails to work, then you may need to review the docs, maybe add some other options like KeyIncrement. Generally, I would accept the defaults and only modify (or provide explicit parameters for) things that you NEED to change (i.e. "don't work").

Is there a way to alter ChildContent in Blazor

I'd like to alter ChildContent my component receives from the parent like this:
<Markdown>
# Title
Some _Content_
</Markdown>
To interpret this content I would need to do something like Markdown.ToHTML(#ChildContent).
But as ChildContent is not a string, I need some means to access the ChildContent and retrieve it as string. Is this possible and how can it be done? Any other idea to solve this?
This isn't impossible but it's not a great way to go.
In order to take a render fragment and produce HTML, it needs to be run through a renderer. This isn't a particularly simple thing to achieve. If you want to see an example you can look at the Test Renderer produced by Steve Sanderson for his unit testing prototype.
You could have a go at creating your own renderer but I would suggesting considering a different approach.
Most probably you have already found a solution, but take a look at the code snippet bellow (.NET 5.0):
var builder = new RenderTreeBuilder();
builder.AddContent(0, this.ChildContent);
var frame = builder.GetFrames().Array.FirstOrDefault(x => new[]
{
RenderTreeFrameType.Text,
RenderTreeFrameType.Markup
}.Any(t => x.FrameType == t));
var value = frame?.MarkupContent;
Adding content to builder adds two frames, a region one and a text/markup one, depending on whether ChildContent contains plain text of HTML. Instead of guessing however, it is safer to use FirstOrDefault.
NB: This is kind of a hack and is not guaranteed that would work in future version of Blazor. I haven't used it with Blazor 6.0 or 7.0 (in preview, yet to be released) but I am able to retrieve the content of, pardon me, ChildContent.

Does Simple.OData.Client support open properties?

I am working with Simple Odata Library
https://github.com/object/Simple.OData.Client/wiki
I need to define open parameters, but i dont seam to see any definition or documentation for this.
Example for clarification:
Along with my oData call, i send a parameter called "mode", which i can set to any number between 0-10. My server will know what to do with it. This parameter however is not pre-defined.
Recent releases of Simple.OData.Client support OData open types, look at examples here:
https://github.com/object/Simple.OData.Client/blob/master/Simple.OData.Client.IntegrationTests/TripPinTests.cs
Search for tests containing "OpenProperty".
user2824991:
I think so. I have tested the untyped and typed scenario for both query and update.
For example:
var order = await client.For("Orders")
.Set(new {OrderId = 9, OrderName = "New Order", MyProperty = "Dynamic Property", GuidProperty = Guid.NewGuid()})
.InsertEntryAsync();
Where, "OrderId" and "OrderName" are both declared properties, while "MyProperty" and "GuidProperty" are both dynamic properties.
Here's my test codes update. it belongs to my sample project.

Categories