/// <summary>
        /// Returns StiTestConnectionResult that is the information of whether the connection string specified in this class is correct.
        /// </summary>
        /// <returns>The result of testing the connection string.</returns>
        public override StiTestConnectionResult TestConnection()
        {
            try
            {
                //var client = new MongoClient(this.ConnectionString);
                var client = AssemblyHelper.CreateObject("MongoDB.Driver.MongoClient", this.ConnectionString);

                //var server = client.GetServer();
                var server = client.GetType().GetMethod("GetServer").Invoke(client, null);
                if (server == null)
                {
                    throw new NotSupportedException();
                }

                //server.Ping();
                server.GetType().GetMethod("Ping").Invoke(server, null);
            }
            catch (Exception exception)
            {
                if (exception.InnerException != null)
                {
                    return(StiTestConnectionResult.MakeWrong(exception.InnerException.Message));
                }
                else
                {
                    return(StiTestConnectionResult.MakeWrong(exception.Message));
                }
            }

            return(StiTestConnectionResult.MakeFine());
        }
        private object GetDatabase()
        {
            //var client = new MongoClient(this.ConnectionString);
            var client = AssemblyHelper.CreateObject("MongoDB.Driver.MongoClient", this.ConnectionString);

            //var builder = new MongoUrlBuilder(this.ConnectionString);
            var builder = AssemblyHelper.CreateObject("MongoDB.Driver.MongoUrlBuilder", this.ConnectionString);

            //return client.GetServer().GetDatabase(builder.DatabaseName);
            var databaseName = builder.GetType().GetProperty("DatabaseName").GetValue(builder, null) as string;

            var method = client.GetType().GetMethod("GetServer");
            var server = method != null?method.Invoke(client, null) : null;

            if (server != null)//Net4
            {
                return(server.GetType().GetMethod("GetDatabase", new[] { typeof(string) }).Invoke(server, new object[] { databaseName }));
            }

            /*else //Net45
             * {
             *  var settingsType = AssemblyHelper.GetType("MongoDB.Driver.MongoDatabaseSettings");
             *  return client.GetType().GetMethod("GetDatabase", new[] {typeof (string), settingsType}).Invoke(client, new object[] {databaseName, null});
             * }*/
            throw new NotSupportedException();
        }
 private IAdapter CreateAdapter(string dllPath, string adapterClassFullName)
 {
     try
     {
         if (dllPath.IsNullOrEmpty())
         {
             return(null);
         }
         if (!File.Exists(dllPath))
         {
             throw new ArgumentException("File: " + dllPath + " does not exists!");
         }
         string key     = adapterClassFullName;//namespaceDotClassName;
         var    objType = AssemblyHelper.GetCache(key) as IAdapter;
         if (objType == null)
         {
             objType = (IAdapter)AssemblyHelper.CreateObject(dllPath, adapterClassFullName);
             AssemblyHelper.SetCache(key, objType);
         }
         return(objType);
     }
     catch (Exception ex)
     {
         throw new ArgumentException("\n>> " + GetType().FullName + ".CreateAdapter Error: " + ex.Message);
     }
 }
Exemple #4
0
        /// <summary>
        /// 检查登陆
        /// </summary>
        /// <param name="entity"></param>
        private void CheckLogin(Guid OrganizationId, string UserName, string UserPwd)
        {
            TB_Org_User user = new TB_Org_User();

            user.OrganizationId = OrganizationId.ToString();
            user.UserName       = UserName;
            user.UserPwd        = UserPwd;

            var usrBll = AssemblyHelper.CreateObject <YK.Interface.Organization.IOrgUser>();
            var result = usrBll.CheckUserLogin(user);

            IsLoginSucess = result == null ? false : true;
        }
 // also you can use CreateAdapter1 to realize IOC by reflection --by Liggin2019 at 200319
 private static ICblpAdapter CreateAdapter1(string cblpDllPath, string adapterClassFullName)
 {
     try
     {
         string key     = adapterClassFullName;//namespaceDotClassName;
         var    objType = AssemblyHelper.GetCache(key) as ICblpAdapter;
         if (objType == null)
         {
             objType = (ICblpAdapter)AssemblyHelper.CreateObject(cblpDllPath, adapterClassFullName);
             AssemblyHelper.SetCache(key, objType);
         }
         return(objType);
     }
     catch (Exception ex)
     {
         throw new ArgumentException("\n>> " + TypeName + ".CreateAdapter Error: " + ex.Message);
     }
 }
        public override DataTable GetDataTable(string collectionName, string query, int?index = null, int?count = null)
        {
            try
            {
                var database = GetDatabase();
                //var collection = database.GetCollection<BsonDocument>(collectionName);
                var method = database.GetType().GetMethods().FirstOrDefault(m => m.Name == "GetCollection" &&
                                                                            !m.IsGenericMethodDefinition &&
                                                                            m.GetParameters().Length == 1 &&
                                                                            m.GetParameters()[0].ParameterType ==
                                                                            typeof(string));

                object collection = null;
                if (method != null)//net40
                {
                    collection = method.Invoke(database, new object[] { collectionName });
                }

                /*else//net45
                 * {
                 *  var settingsType = AssemblyHelper.GetType("MongoDB.Driver.MongoCollectionSettings");
                 *  method = database.GetType().GetMethod("GetCollection", new[] { typeof (string), settingsType });
                 *  method = method.MakeGenericMethod(database.GetType());
                 *  collection = method.Invoke(database, new object[] { collectionName, null });
                 * }*/

                //var cursor = string.IsNullOrWhiteSpace(query) ? collection.FindAll() : collection.Find(new QueryDocument(BsonDocument.Parse(query)));
                var         assembly = new StiDataAssemblyHelper("MongoDB.Bson.dll");
                IEnumerable cursor   = null;
                if (string.IsNullOrWhiteSpace(query))
                {
                    method = collection.GetType().GetMethod("FindAll");
                    cursor = method.Invoke(collection, null) as IEnumerable;
                }
                else
                {
                    var bsonDocument = assembly.GetType("MongoDB.Bson.BsonDocument")
                                       .GetMethod("Parse")
                                       .Invoke(null, new object[] { query });
                    var queryDocument = AssemblyHelper.CreateObject("MongoDB.Driver.QueryDocument", bsonDocument);

                    cursor = collection.GetType().GetMethod("Find").Invoke(collection, new[] { queryDocument }) as IEnumerable;
                }

                var list = new List <JToken>();
                var i    = 0;
                foreach (var row in cursor)
                {
                    if (count != null && i >= count)
                    {
                        break;
                    }
                    if (index == null || i >= index)
                    {
                        try
                        {
                            //var jStr = row.ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict });

                            //var jsonWriterSettings = assembly.CreateObject("MongoDB.Bson.IO.JsonWriterSettings");
                            //jsonWriterSettings.GetType().GetProperty("OutputMode").SetValue(jsonWriterSettings, 0, null);

                            //var method2 = assembly.GetType("MongoDB.Bson.BsonExtensionMethods").GetMethod("ToJson", new[] {typeof(object), typeof(Type), jsonWriterSettings.GetType() });
                            //var jStr = method2.Invoke(row, new[] { jsonWriterSettings }) as string;
                            var jStr = GetAsJson(row);

                            var jToken = JToken.Parse(jStr);
                            list.Add(jToken);
                        }
                        catch
                        {
                        }
                    }

                    i++;
                }

                var dataSet = StiJsonToDataSetConverter.GetDataSet(list, true);
                return(dataSet != null && dataSet.Tables.Count > 0 ? dataSet.Tables[0] : null);
            }
            catch (Exception exception)
            {
                if (exception.InnerException != null)
                {
                    throw exception.InnerException;
                }
                else
                {
                    throw;
                }
            }
        }