Having trouble retrieving collection from MongoDB database - c#

I am trying to learn MongoDB for my next project. I have installed MongoDB on my Windows 7 machine. I am able to create collection and retrieve data using mongo.exe. I am trying to use official C# driver to manipulate collection but my test console application goes in "abyss" while trying to connect to server (I guess).
I have written following code:
static void Main(string[] args)
{
try
{
// Create server settings to pass connection string, timeout, etc.
var settings = new MongoServerSettings();
settings.Server = new MongoServerAddress("localhost", 27017);
// Create server object to communicate with our server
var server = new MongoServer(settings);
server.Connect(TimeSpan.FromMilliseconds(1000));
// Get our database instance to reach collections and data
var message = string.Empty;
server.IsDatabaseNameValid("test", out message);
Console.WriteLine(message);
var database = server.GetDatabase("test");
var users = database.GetCollection("users").FindAll();
foreach (var user in users)
{
Console.WriteLine("User found");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Console.WriteLine(ex.StackTrace);
}
Console.ReadLine();
}
Console application stops working on line server.Connect(). I have put this line just to find problem, I know it is not necessary to connect to server explicitly. If I remove that line then Console application stops working on line foreach (var user in users)
I also tried to get server using following code with no success:
var mongoClient = new MongoClient("mongodb://<host>:27017");
var server = mongoClient.GetServer();

Code is perfect! It was idiosyncrasy of MongoDB that when you try to connect to MongoDB through C# driver, you will have to accept connection for first time. Next time onwards MongoDB will remember your last acceptance and move on.
There has to be setting to remove this condition but I haven't found it yet. I will update as soon as possible.
Happy MangoDB Learning.

use your last example like this:
var mongoClient = new MongoClient("mongodb://localhost");
var server = mongoClient.GetServer();
Unless it's not running on the same machine or not on the standard port

Related

Connecting from home Windows computer or Windows EC2 server to a Ubuntu MongoDB EC2 server via SSH and port forwarding?

I have two AWS EC2 instances:
Windows Server which will handle user requests for data.
Ubuntu server with MongoDB database on it to be accessed and manipulated by Windows Server.
I am attempting to write a program in C# .NET that will be able to connect to the Ubuntu server and its MongoDB when run from my local computer or from the Windows Server.
I am using Renci SSH.NET and the standard MongoDB driver. The MongoDB driver operation is explained here. For networking, I found one old guide here which helped me get part way. I have so far:
string SSHServerUserName = "ubuntu";
string SSHServerHost = "x.xx.xxx.xxx"; //public IP of ubuntu server as per AWS
PrivateKeyFile keyFile = new PrivateKeyFile(#"H:\MongoDB.pem", "filePass"); //mongo server AWS keyfile
PrivateKeyAuthenticationMethod authenticationMethod = new PrivateKeyAuthenticationMethod(SSHServerUserName, keyFile);
ConnectionInfo connectionInfo = new ConnectionInfo(SSHServerHost, SSHServerUserName, authenticationMethod); //uses DefaultPort = 22
SshClient sshClient = new SshClient(connectionInfo);
//sshClient.ErrorOccurred += delegate { Debug.WriteLine("SSH ERROR OCCURRED"); };
//sshClient.HostKeyReceived += delegate { Debug.WriteLine("SSH HOST KEY RECEIVED"); };
sshClient.Connect();
if (sshClient.IsConnected) {
string MongoDBHost = "xx.xx.xx.xx"; // **PRIVATE IP OF UBUNTU AWS EC2?
uint MongoDBPort = 27017;
ForwardedPortLocal forwardedPortLocal = new ForwardedPortLocal("127.0.0.1", 5477, MongoDBHost, MongoDBPort);
//forwardedPortLocal.Exception += delegate { Debug.WriteLine("FORWARDED PORT LOCAL EXCEPTION"); };
//forwardedPortLocal.RequestReceived += delegate { Debug.WriteLine("FORWARDED PORT REQUEST RECEIVED"); };
sshClient.AddForwardedPort(forwardedPortLocal);
forwardedPortLocal.Start();
MongoClientSettings mongoSettings = new MongoClientSettings();
mongoSettings.Server = new MongoServerAddress("localhost", 5477); //IS THIS RIGHT?
MongoClient mongoClient = new MongoClient(mongoSettings);
var iMongoDatabase = mongoClient.GetDatabase("test");
var profiles = iMongoDatabase.GetCollection<BsonDocument>("profiles");
Debug.WriteLine("GOT THE COLLECTION:"); //debugs out okay...
var document = new BsonDocument {
{ "name", "userName" },
{ "type", "newUser" },
{ "count", 1 },
{ "info", new BsonDocument
{
{ "x", 203 },
{ "y", 102 }
} }
};
profiles.InsertOne(document); //reports time out exception if using try/catch
Debug.Write("ATTEMPTED TO INSERT ONE");
}
I am getting sshClient.IsConnected() as true. However, I do not know if my forwarded port is working correctly or to what extent it is or isn't connecting to the Mongo Database. I can see from MongoDB Compass (GUI database navigator) nothing is being inserted, and from a try catch statement I can receive the exception that the MongoDB driver times out on the InsertOne command.
Questions:
How do I test the Forwarded Local Port? I see FORWARDED PORT REQUEST RECEIVED outputs but don't know if they're working.
Is the MongoDB server code (IPs, ports, "localhost") correct for this configuration? How do I test to see if it has connected properly to the database?
What is going wrong in the above?
This is my first database and EC2 configuration. Thanks for any help.

Get Linux server time from windows service c#

Is there anyway I can get linux server time from windows service given linux server IP ? I tried to use Cliwrap (https://github.com/Tyrrrz/CliWrap) and wrote the following function but it is not working as well:
public string GetLinuxServerTime()
{
using (var cli = new Cli("bash.exe"))
{
// Execute
var output = cli.Execute("ssh user#10.104.12.114 date");
return "abc";
}
}
Kindly suggest some another way.

Remote OPC Connected but can't read

I am writing a custom OPC Client application in C#, enabling reading of data from a RSLinx Server.
First of, I wasn't able to connect to the RSLINX Opc Server remotely. Exception Access Denied kept occurring.
I then alteredthe DCOM Settings of MyComputer -> Com Security -> Access Permissions and Launch and Activation Permissions, enabled everything for the User 'Everyone'
This then allowed me to connect, but when It comes to read the server I get the following exception -
[System.Runtime.InteropServices.COMException] {"Exception from HRESULT: 0x80040202"} System.Runtime.InteropServices.COMException
I have scoured the web as much as I possibly can, and they all boil down to Dcom related issues. I have simply tried everything with the Dcom Settings. I have made sure the settings are enabled for both MyComputer and RSLinx server.
I am using two .dll files from OPCFoundation - opcNetApi.dll , opcNetApi.Com.dll
Here is my code, (It may be handy)
private void readplc()
{
Opc.URL url = new Opc.URL("opcda://48.5.0.05/RSLinx OPC Server");
Opc.Da.Server server = null;
OpcCom.Factory fact = new OpcCom.Factory();
server = new Opc.Da.Server(fact, null);
try
{
server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential()));
}
catch (Exception exy)
{
MessageBox.Show(exy.Message);
}
// Create a group
Opc.Da.Subscription group;
Opc.Da.SubscriptionState groupState = new Opc.Da.SubscriptionState();
groupState.Name = "Group";
groupState.Active = true;
group = (Opc.Da.Subscription)server.CreateSubscription(groupState);
// add items to the group.
Opc.Da.Item[] items = new Opc.Da.Item[6];
items[0] = new Opc.Da.Item();
items[0].ItemName = "[ALARM]F20:9";
items[1] = new Opc.Da.Item();
items[1].ItemName = "[ALARM]F22:30";
items[2] = new Opc.Da.Item();
items[2].ItemName = "[ALARM]F22:6";
items[3] = new Opc.Da.Item();
items[3].ItemName = "[ALARM]F18:8";
items[4] = new Opc.Da.Item();
items[4].ItemName = "[ALARM]F22:32";
items[5] = new Opc.Da.Item();
items[5].ItemName = "[ALARM]F22:5";
items = group.AddItems(items);
try
{
group.DataChanged += new Opc.Da.DataChangedEventHandler(OnTransactionCompleted); // COM EXCEPTION THROWN HERE
Console.ReadLine();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Console.ReadKey();
}
}
private void OnTransactionCompleted(object group, object hReq, Opc.Da.ItemValueResult[] items)
{
for (int i = 0; i < items.GetLength(0); i++)
{
}
}
I know for certain that the code works as I have tried building the application connecting as a localhost on the PC I am trying to remote connect to, it reads the data happily.
Hopefully, someone will have some idea what is going on, I have spent over 12 hours in the last 4 working days trying to work it out!
This is working for me:
_opcServer = new Server(_comFactory, null) { Url = new Opc.URL("opcda://localhost/FactoryTalk Gateway") };
_opcServer.Connect();

Explorer OM is not supported in a 64bit process

I was trying to create send ports using C# .NET through following code :
using Microsoft.BizTalk.ExplorerOM;
private void CreateSendPort()
{
// connect to the local BizTalk Management database
BtsCatalogExplorer catalog = new BtsCatalogExplorer();
catalog.ConnectionString = "Server=.;Initial Catalog=BizTalkMgmtDb;Integrated Security=SSPI;";
try
{
// create a new static one-way SendPort
SendPort myStaticOnewaySendPort = catalog.AddNewSendPort(false, false);
myStaticOnewaySendPort.Name = "myStaticOnewaySendPort1";
myStaticOnewaySendPort.PrimaryTransport.TransportType = catalog.ProtocolTypes[0];
myStaticOnewaySendPort.PrimaryTransport.Address = "http://sample1";
myStaticOnewaySendPort.SendPipeline = catalog.Pipelines["Microsoft.BizTalk.DefaultPipelines.XMLTransmit"];
// create a new dynamic two-way sendPort
SendPort myDynamicTwowaySendPort = catalog.AddNewSendPort(true, true);
myDynamicTwowaySendPort.Name = "myDynamicTwowaySendPort1";
myDynamicTwowaySendPort.SendPipeline = catalog.Pipelines["Microsoft.BizTalk.DefaultPipelines.XMLTransmit"];
myDynamicTwowaySendPort.ReceivePipeline = catalog.Pipelines["Microsoft.BizTalk.DefaultPipelines.XMLReceive"];
// persist changes to BizTalk Management database
catalog.SaveChanges();
}
catch(Exception e)
{
catalog.DiscardChanges();
throw e;
}
}
Source
But I'm getting following issue
Explorer OM is not supported in a 64bit process.
when this line is executed :
BtsCatalogExplorer catalog = new BtsCatalogExplorer();
I'm well aware of the fact i.e. : "Warning
Microsoft.BizTalk.ExplorerOM.dll is only supported if used from 32 bit processes. If you are building a solution for a 64 bit system you should not use this library."
But in this case how can I create send ports on 64bit machine, Can anybody please help me with this?
Force it to run in a 32 bit process.
http://lostechies.com/gabrielschenker/2009/10/21/force-net-application-to-run-in-32bit-process-on-64bit-os/
As of BizTalk 2010, this restriction was lifted and ExplorerOM can be used in 64-bit and 32-bit processes.

How to Create and Execute an SSIS Package on remote sql Server source and destination using EzAPI

I need to create a Console application that can copy a table from one remote sql server instance to another remote sql server instance.
I'm using a library called EzAPI
Both connections (Source and Destinations) and table name will be provided as parameters to the Console application.
Here is my try:
public class OleDBToOleDB : EzSrcDestPackage<EzOleDbSource, EzSqlOleDbCM, EzOleDbDestination, EzSqlOleDbCM>
{
public OleDBToOleDB(Package p) : base(p) { }
public static implicit operator OleDBToOleDB(Package p) { return new OleDBToOleDB(p); }
public OleDBToOleDB(string SrcServer, string SrcDB, string SrcTable,string SrcUser,string SrcPassword, string DstServer, string DstDB, string DstTable,string DstUser,string DstPassword)
{
SrcConn.SetConnectionString(SrcServer, SrcDB);
SrcConn.ServerName = SrcServer;
SrcConn.InitialCatalog = SrcDB;
SrcConn.UserName = SrcUser;
SrcConn.Password = SrcPassword;
Source.Table = SrcTable;
DestConn.SetConnectionString(DstServer, DstDB);
DestConn.ServerName = DstServer;
DestConn.InitialCatalog = DstDB;
DestConn.UserName = DstUser;
DestConn.Password = DstPassword;
Dest.Table = DstTable;
}
}
static void Main(string[] args)
{
OleDBToOleDB p = new OleDBToOleDB("localhost", "TestDB", "Address", "sa", "123", "localhost", "DestDB", "Address", "sa", "123");
p.Execute();
Console.Write(string.Format("Package2 executed with result {0}\n",p.ExecutionResult));
}
The problem with this code is:
It does not create the table on the destination server, so I should
create it manually by myself.
This code runs successfully on the localhost, but when I try to
change the server name to a remote server it raises this error:
Unhandled Exception: System.Runtime.InteropServices.COMException
(0xC020801C): Exception from HRESULT: 0xC020801C
After searching on the web I found that this error means that this error is an Integration services AcquireConnections exception.
So how can I get this code running on a remote sql server instance and have the package create the table on the destination server before transferring the data.
Thank you in advance.
SSIS needs both tabled during the package generation, since it has to get the whole metadata for each column in your tables. You can set the ValidateMetadata property to false, in this case it will not validate it, but you will need to fill all the data by yourself. This is not easy.
I think the most easy thing you can do is:
1)generate the package with local connections and turn off the validation for your destination component
2)now set the new connection strings to your source and destination compoennts
3)run the package
this is a not really clean workaround. This should be usually done with a configurations, but since you do not save your package you can try this as well.
I think it can be also easier to implement your task with SqlBuldCopy without SSIS

Categories