如何获取从中提取数据的服务器的服务器地址

编程入门 行业动态 更新时间:2024-10-28 20:31:43
本文介绍了如何获取从中提取数据的服务器的服务器地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道我们在Java驱动程序中使用cursor.getServerAddress()拥有了它.但是,我在C#中找不到任何相关的调用.

I know we have it in Java driver using cursor.getServerAddress(). However, I couldn't find any relevant call in C#.

推荐答案

不幸的是,在深入研究了C#驱动程序源代码(以及Java源代码以了解其作用)之后,实际的服务器地址用作提取的一部分被作为私有成员(_serverInstance)隐藏在Enumerator中.

Unfortunately, after digging a little deeper though through the C# driver source code (and the Java source code to see what it was doing), the actual server address that was used as part of the fetch is hidden away in the Enumerator as a private member (_serverInstance).

尤其是,它隐藏在名为MongoCursorEnumerator的类中.不幸的是,没有一种诚实"的方式来做到这一点.我想可以使用.NET反射来实现它,但这通常不是一个好主意,尤其是对于您不拥有的代码.如果您想要驱动程序的自定义派生,则可以轻松地将其添加为新属性.您可能要在此处添加问题.

In particular, it's hidden away in a class called the MongoCursorEnumerator. Unfortunately, there isn't an "honest" way of getting to it. You could use .NET reflection I suppose to get to it, but that's generally not a good idea, especially with code you don't own. If you wanted to have a custom fork of the driver, you could easily add it as a new property. You might want to add an issue here.

通常,您可以获取MongoDB连接使用的实例,因为它们在MongoServerInstance类的Address属性中可用.

In general, you can get the instances used by the MongoDB connection, as they are available on the MongoServerInstance class, the Address property.

var client = new MongoClient(); var server = client.GetServer(); // server.Instance.Address

文档

但是,您也可以通过Server属性从cursor中获取它:

However, in your case you can also get it from the cursor via the Server property:

cursor.Server.Instance.Address.Host

cursor.Server.Instances[]

游标服务器文档

有趣的是,当有多个实例可用时,C#驱动程序将引发异常.因此,如果您不知道有多少实例,则需要处理exception(InvalidOperationException),或者始终使用Instances[]属性来返回关联的实例.

The C# driver interestingly throws and exception when more than one Instance is available. So, if you don't know how many instances there are, you either need to handle the exception (InvalidOperationException), or always use the Instances[] property to return the associated instances.

更多推荐

如何获取从中提取数据的服务器的服务器地址

本文发布于:2023-11-01 23:48:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550751.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   地址   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!