Пример #1
0
 public void DoWork()
 {
     while (true)                                         //Each loop gets and returns one message pair.
     {
         Byte[]       buffer            = new Byte[1024]; // Buffer for reading data
         MemoryStream memStream         = new MemoryStream();
         int          numberOfBytesRead = 0;
         try {
             do
             {
                 numberOfBytesRead = netStream.Read(buffer, 0, buffer.Length);
                 //myCompleteMessage.AppendFormat("{0}",Encoding.UTF8.GetString(buffer,0,numberOfBytesRead));
                 memStream.Write(buffer, 0, numberOfBytesRead);
             }while(netStream.DataAvailable);
         }
         catch {                //if connection was closed by client.
             break;
         }
         //Console.Write("Processing...");
         //string debugMessage=Encoding.UTF8.GetString(memStream.GetBuffer());
         //memStream.Position=0;
         //if(debugMessage.StartsWith("SELECT * FROM claimformitem")){
         //	Debug.WriteLine(debugMessage);
         //}
         DataTransferObject dto = DataTransferObject.Deserialize(memStream.GetBuffer());              //myCompleteMessage.ToString());
         memStream.Close();
         //Process and send response to client--------------------------------------------------------------
         byte[]        data;
         XmlSerializer serializer;
         memStream = new MemoryStream();
         try {
             if (dto.GetType().BaseType == typeof(DtoCommandBase))
             {
                 int          result = BusinessLayer.ProcessCommand((DtoCommandBase)dto);
                 DtoServerAck ack    = new DtoServerAck();
                 ack.IDorRows = result;
                 serializer   = new XmlSerializer(typeof(DtoServerAck));
                 serializer.Serialize(memStream, ack);
             }
             else if (dto.GetType().BaseType == typeof(DtoQueryBase))
             {
                 DataSet ds = BusinessLayer.ProcessQuery((DtoQueryBase)dto);
                 serializer = new XmlSerializer(typeof(DataSet));
                 serializer.Serialize(memStream, ds);
             }
         }
         catch (Exception e) {
             DtoException exception = new DtoException();
             exception.Message = e.Message;
             serializer        = new XmlSerializer(typeof(DtoException));
             serializer.Serialize(memStream, exception);
         }
         data = Encoding.UTF8.GetBytes("<EOF>");
         memStream.Write(data, 0, data.Length);
         data = memStream.ToArray();
         //#if DEBUG
         //	string dataString=Encoding.UTF8.GetString(data);
         //	Debug.WriteLine("Server Sent: "+dataString);
         //#endif
         memStream.Close();
         netStream.Write(data, 0, data.Length);
     }            //wait for the next message
     //connection was lost.  Client probably closed program
     netStream.Close();
 }
Пример #2
0
 public string ProcessRequest(string dtoString)
 {
     //The web service (xml) serializer/deserializer is removing the '\r' portion of our newlines during the data transfer.
     //Replacing the string is not the best solution but it works for now. The replacing happens here (server side) and after result is returned on the client side.
     //It's done server side for usage purposes within the methods being called (exampe: inserting into db) and then on the client side for displaying purposes.
     dtoString = dtoString.Replace("\n", "\r\n");
                 #if DEBUG
     //System.Threading.Thread.Sleep(100);//to test slowness issues with web service.
                 #endif
     DataTransferObject dto = DataTransferObject.Deserialize(dtoString);
     //XmlSerializer serializer;
     try {
         Type type = dto.GetType();
         if (type == typeof(DtoGetTable))
         {
             DtoGetTable dtoGetTable = (DtoGetTable)dto;
             Userods.CheckCredentials(dtoGetTable.Credentials);                    //will throw exception if fails.
             string      className  = dtoGetTable.MethodName.Split('.')[0];
             string      methodName = dtoGetTable.MethodName.Split('.')[1];
             string      assemb     = Assembly.GetAssembly(typeof(Db)).FullName;         //any OpenDentBusiness class will do.
             Type        classType  = Type.GetType("OpenDentBusiness." + className + "," + assemb);
             DtoObject[] parameters = dtoGetTable.Params;
             Type[]      paramTypes = DtoObject.GenerateTypes(parameters, assemb);
             MethodInfo  methodInfo = classType.GetMethod(methodName, paramTypes);
             if (methodInfo == null)
             {
                 throw new ApplicationException("Method not found with " + parameters.Length.ToString() + " parameters: " + dtoGetTable.MethodName);
             }
             object[]  paramObjs = DtoObject.GenerateObjects(parameters);
             DataTable dt        = (DataTable)methodInfo.Invoke(null, paramObjs);
             String    response  = XmlConverter.TableToXml(dt);
             return(response);
         }
         else if (type == typeof(DtoGetTableLow))
         {
             DtoGetTableLow dtoGetTableLow = (DtoGetTableLow)dto;
             Userods.CheckCredentials(dtoGetTableLow.Credentials);                    //will throw exception if fails.
             DtoObject[] parameters = dtoGetTableLow.Params;
             object[]    paramObjs  = DtoObject.GenerateObjects(parameters);
             DataTable   dt         = Reports.GetTable((string)paramObjs[0]);
             String      response   = XmlConverter.TableToXml(dt);
             return(response);
         }
         else if (type == typeof(DtoGetDS))
         {
             DtoGetDS dtoGetDS = (DtoGetDS)dto;
             Userods.CheckCredentials(dtoGetDS.Credentials);                    //will throw exception if fails.
             string      className  = dtoGetDS.MethodName.Split('.')[0];
             string      methodName = dtoGetDS.MethodName.Split('.')[1];
             string      assemb     = Assembly.GetAssembly(typeof(Db)).FullName;         //any OpenDentBusiness class will do.
             Type        classType  = Type.GetType("OpenDentBusiness." + className + "," + assemb);
             DtoObject[] parameters = dtoGetDS.Params;
             Type[]      paramTypes = DtoObject.GenerateTypes(parameters, assemb);
             MethodInfo  methodInfo = classType.GetMethod(methodName, paramTypes);
             if (methodInfo == null)
             {
                 throw new ApplicationException("Method not found with " + parameters.Length.ToString() + " parameters: " + dtoGetDS.MethodName);
             }
             object[] paramObjs = DtoObject.GenerateObjects(parameters);
             DataSet  ds        = (DataSet)methodInfo.Invoke(null, paramObjs);
             String   response  = XmlConverter.DsToXml(ds);
             return(response);
         }
         else if (type == typeof(DtoGetLong))
         {
             DtoGetLong dtoGetLong = (DtoGetLong)dto;
             Userods.CheckCredentials(dtoGetLong.Credentials);                    //will throw exception if fails.
             string      className  = dtoGetLong.MethodName.Split('.')[0];
             string      methodName = dtoGetLong.MethodName.Split('.')[1];
             string      assemb     = Assembly.GetAssembly(typeof(Db)).FullName;         //any OpenDentBusiness class will do.
             Type        classType  = Type.GetType("OpenDentBusiness." + className + "," + assemb);
             DtoObject[] parameters = dtoGetLong.Params;
             Type[]      paramTypes = DtoObject.GenerateTypes(parameters, assemb);
             MethodInfo  methodInfo = classType.GetMethod(methodName, paramTypes);
             if (methodInfo == null)
             {
                 throw new ApplicationException("Method not found with " + parameters.Length.ToString() + " parameters: " + dtoGetLong.MethodName);
             }
             object[] paramObjs  = DtoObject.GenerateObjects(parameters);
             long     longResult = (long)methodInfo.Invoke(null, paramObjs);
             return(longResult.ToString());
         }
         else if (type == typeof(DtoGetInt))
         {
             DtoGetInt dtoGetInt = (DtoGetInt)dto;
             Userods.CheckCredentials(dtoGetInt.Credentials);                    //will throw exception if fails.
             string      className  = dtoGetInt.MethodName.Split('.')[0];
             string      methodName = dtoGetInt.MethodName.Split('.')[1];
             string      assemb     = Assembly.GetAssembly(typeof(Db)).FullName;         //any OpenDentBusiness class will do.
             Type        classType  = Type.GetType("OpenDentBusiness." + className + "," + assemb);
             DtoObject[] parameters = dtoGetInt.Params;
             Type[]      paramTypes = DtoObject.GenerateTypes(parameters, assemb);
             MethodInfo  methodInfo = classType.GetMethod(methodName, paramTypes);
             if (methodInfo == null)
             {
                 throw new ApplicationException("Method not found with " + parameters.Length.ToString() + " parameters: " + dtoGetInt.MethodName);
             }
             object[] paramObjs = DtoObject.GenerateObjects(parameters);
             int      intResult = (int)methodInfo.Invoke(null, paramObjs);
             return(intResult.ToString());
         }
         else if (type == typeof(DtoGetVoid))
         {
             DtoGetVoid dtoGetVoid = (DtoGetVoid)dto;
             Userods.CheckCredentials(dtoGetVoid.Credentials);                    //will throw exception if fails.
             string      className  = dtoGetVoid.MethodName.Split('.')[0];
             string      methodName = dtoGetVoid.MethodName.Split('.')[1];
             string      assemb     = Assembly.GetAssembly(typeof(Db)).FullName;         //any OpenDentBusiness class will do.
             Type        classType  = Type.GetType("OpenDentBusiness." + className + "," + assemb);
             DtoObject[] parameters = dtoGetVoid.Params;
             Type[]      paramTypes = DtoObject.GenerateTypes(parameters, assemb);
             MethodInfo  methodInfo = classType.GetMethod(methodName, paramTypes);
             if (methodInfo == null)
             {
                 throw new ApplicationException("Method not found with " + parameters.Length.ToString() + " parameters: " + dtoGetVoid.MethodName);
             }
             object[] paramObjs = DtoObject.GenerateObjects(parameters);
             methodInfo.Invoke(null, paramObjs);
             return("0");
         }
         else if (type == typeof(DtoGetObject))
         {
             DtoGetObject dtoGetObject = (DtoGetObject)dto;
             string       className    = dtoGetObject.MethodName.Split('.')[0];
             string       methodName   = dtoGetObject.MethodName.Split('.')[1];
             if (className != "Security" || methodName != "LogInWeb")                    //because credentials will be checked inside that method
             {
                 Userods.CheckCredentials(dtoGetObject.Credentials);                     //will throw exception if fails.
             }
             string      assemb     = Assembly.GetAssembly(typeof(Db)).FullName;         //any OpenDentBusiness class will do.
             Type        classType  = Type.GetType("OpenDentBusiness." + className + "," + assemb);
             DtoObject[] parameters = dtoGetObject.Params;
             Type[]      paramTypes = DtoObject.GenerateTypes(parameters, assemb);
             MethodInfo  methodInfo = classType.GetMethod(methodName, paramTypes);
             if (methodInfo == null)
             {
                 throw new ApplicationException("Method not found with " + parameters.Length.ToString() + " parameters: " + dtoGetObject.MethodName);
             }
             if (className == "Security" && methodName == "LogInWeb")
             {
                 string mappedPath = Server.MapPath(".");
                 parameters[2] = new DtoObject(mappedPath, typeof(string));                     //because we can't access this variable from within OpenDentBusiness.
                 RemotingClient.RemotingRole = RemotingRole.ServerWeb;
             }
             object[] paramObjs  = DtoObject.GenerateObjects(parameters);
             Object   objResult  = methodInfo.Invoke(null, paramObjs);
             Type     returnType = methodInfo.ReturnType;
             return(XmlConverter.Serialize(returnType, objResult));
         }
         else if (type == typeof(DtoGetString))
         {
             DtoGetString dtoGetString = (DtoGetString)dto;
             Userods.CheckCredentials(dtoGetString.Credentials);                    //will throw exception if fails.
             string      className  = dtoGetString.MethodName.Split('.')[0];
             string      methodName = dtoGetString.MethodName.Split('.')[1];
             string      assemb     = Assembly.GetAssembly(typeof(Db)).FullName;         //any OpenDentBusiness class will do.
             Type        classType  = Type.GetType("OpenDentBusiness." + className + "," + assemb);
             DtoObject[] parameters = dtoGetString.Params;
             Type[]      paramTypes = DtoObject.GenerateTypes(parameters, assemb);
             MethodInfo  methodInfo = classType.GetMethod(methodName, paramTypes);
             if (methodInfo == null)
             {
                 throw new ApplicationException("Method not found with " + parameters.Length.ToString() + " parameters: " + dtoGetString.MethodName);
             }
             object[] paramObjs = DtoObject.GenerateObjects(parameters);
             string   strResult = (string)methodInfo.Invoke(null, paramObjs);
             //strResult=strResult.Replace("\r","\\r");
             //return XmlConverter.Serialize(typeof(string),strResult);
             return(strResult);
         }
         else if (type == typeof(DtoGetBool))
         {
             DtoGetBool dtoGetBool = (DtoGetBool)dto;
             Userods.CheckCredentials(dtoGetBool.Credentials);                    //will throw exception if fails.
             string      className  = dtoGetBool.MethodName.Split('.')[0];
             string      methodName = dtoGetBool.MethodName.Split('.')[1];
             string      assemb     = Assembly.GetAssembly(typeof(Db)).FullName;         //any OpenDentBusiness class will do.
             Type        classType  = Type.GetType("OpenDentBusiness." + className + "," + assemb);
             DtoObject[] parameters = dtoGetBool.Params;
             Type[]      paramTypes = DtoObject.GenerateTypes(parameters, assemb);
             MethodInfo  methodInfo = classType.GetMethod(methodName, paramTypes);
             if (methodInfo == null)
             {
                 throw new ApplicationException("Method not found with " + parameters.Length.ToString() + " parameters: " + dtoGetBool.MethodName);
             }
             object[] paramObjs  = DtoObject.GenerateObjects(parameters);
             bool     boolResult = (bool)methodInfo.Invoke(null, paramObjs);
             return(boolResult.ToString());
         }
         else
         {
             throw new NotSupportedException("Dto type not supported: " + type.FullName);
         }
     }
     catch (Exception e) {
         DtoException exception = new DtoException();
         if (e.InnerException == null)
         {
             exception.Message = e.Message;
         }
         else
         {
             exception.Message = e.InnerException.Message;
         }
         return(exception.Serialize());
     }
 }