Tips and Tricks in a world of Mix

Very good source of courses used by Microsoft is now available for free for 6 month , including all it’s features.

All you need is an account and usage of the benefit.

1.Create Microsoft account

Capture
4.You’ll have the get the code and then you can activate the account of Pluralsight and use it.
Enjoy!
Capture2

When you need a test running during your process to insert data you may want to use task for async insertion.

Also you may run it parallel to your process from outside.

If you want to be able to stop you should implement Cancellation Token and put it inside the process as a stop condition for a while loop.

 


namespace AsyncTest
{
 public class InitTestPerSecond{
 public Task initData;
 public InitTestPerSecond()
 {
 initData = new Task()(
 ()=>
 {
 while(true)
 {
 MyDAL.InsertInTableEachSecond(DateTime.Now);
 Thread.Sleep(1000);
 }
 });
 initData.Start();
 }
}
 
namespace MyDAL
{
 public class MyDAL{
 public static void InsertInTableEachSecond(DateTime timestamp)
 {
 try{
 OracleConnection localConn = new OracleConnection(Utils.GetConnectionString());
 localConn.Open();
 int maxId;
 using (OracleCommand maxIdCommand = new OracleCommand("SELECT MAX(MYID) FROM SCHEMA_NAME.MYTABLE", localConn))
 {
 maxId = int.Parse(maxIdCommand.ExecuteScalar().ToString());
 }
 using (OracleCommand initMyData = localConn.CreateCommand())
 {
 
 initMyData = string.Format(" INSERT INTO SCHEMA_NAME.MYTABLE (MYID , COL1, DATE)
 VALUES 
 ({0}, 
 '{0} test ' ,
 {1})", ++maxId ,Utils.GetDateFormat(timeStamp) ));
 
 initMyData.ExecuteNonQuery();
 }
 localConn.Close();
 localConn.Dispose();
 }
 catch(Exception ex){
 throw ex;
 }
 }
 
 }
 
}
 
public class Utils{
 
 public static string GetDateFormat(DateTime date){
 return string.Format(@"TO_DATE('{0}', 'MM/DD/YYYY HH24:MI:SS')", date.ToString("MM/dd/YYYY HH:mm:ss"));
 }
 
 public static string GetConnectionString()
 {
 return ConnectionStringHandler.ConnectionString;
 }
} 
 
 
public static class ConnectionStringHandler
{
 public static string ConnectionString= String.Empty;
 static ConnectionStringHandler()
 {
 Decrypt();
 ConnectionString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
 Encrypt();
 }
}


useful sql

Backup data

If NEW_TABLE already exists then …

insert into new_table select * from old_table
/

If you want to create NEW_TABLE based on the records in OLD_TABLE …

create table new_table as select * from old_table
/


In ORACLE

SELECT REPLACE(REPLACE(YourColumn,CHAR(13),NULL),CHAR(10),NULL)
FROM YourTable


FOR ONE CHAR :
SELECT replace(YourColumn,CHAR(13),'')
FROM YourTable

OR
update YourTable
set YourColumn =replace(YourColumn,CHAR(13),'')

IN .NET 
MyString.Replace("\r\n","")   

or..

MyString.Replace("\r\n",@"\n")

Potential Blacklist
Special control chars 0-31 by Unicode must be handled
White list 
[^a-zA-Zא-ת0-9._] -and punctuation chars by your choice
- this is a start of a regex pattern - [^] -everything that not in the pattern is not allowed.
also can do 
[^\w\s\p{IsHebrew}\p{P}] - not taking accesnt chars like ~^ , not finished , but a start

better handled by StringBuilder with loop through chars than Regex 
(more than 10 times better).just Whitelist of allowed chars and if in range 
like this 

if(c>=a && c<=z)...
  sb.append(c)
else
  sb.append(' '); //replacement char
 

 

For  usage of commands in cmd with ElasticSearch you should install curl.

Better put it in c:\ for convenience .

You should download ES   from here and install it.

You can check it in chrome – http://localhost:9200/

or

run it in cmd  c:\curl -X GET http://localhost:9200/

Run few commands to automatize the process and make it easier to work with :

1) running as a service on windows

c:\elasticsearch-{version}bin>service install
 
 
 
2)install plugins 
You better get the basics 
bin/plugin --install mobz/elasticsearch-head
bin/plugin --install lukas-vlcek/bigdesk
 
image

 

installing marvel will give you the tools that ES itself promotes

bin/plugin -i elasticsearch/marvel/latest

 

3)In VS2013 quick launch – type nuget – choose Package Manager

paste-

Install-Package NEST –Version  1.2.1

it will get you the .net api for elasticsearh. the version is optional . without it gets the latest  version

public static void Encrypt(){

           Configuration objConfiguration = null;

           if(HttpContext.Current!=null)        //if we are in web application of some sort

               objConfiguration = WebConfigurationManager.OpenWebConfiguration (“~”);

           else       ///if not web

                objConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

           ConfigurationSection objSection = “connectionStrings”;

           //if encrypted already don’t do it again

           if(objSection!=null && !objSection.SectionInformation.IsProtected){

                           objSection.SectionInformation.ProtectionSection(string.Empty);

                           objSection.SectionInformation.ForseSave = true;

                          objConfiguration.Save(ConfigurationSaveMode.Full);

           }     

}

 

public static void Encrypt(){

                     Configuration objConfiguration = null;

                       if(HttpContext.Current!=null) //if we are in web application of some sort

                                   objConfiguration = WebConfigurationManager.OpenWebConfiguration (“~”);

                       else ///if not web

                                    objConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                      ConfigurationSection objSection = “connectionStrings”;

                           //if encrypted then decrypt

                            if(objSection!=null && !objSection.SectionInformation.IsProtected){

         objSection.SectionInformation.UnprotectSection();

  //objConfiguration.Save();  – you can save the change but it will reset all the global data of your application in iis (relevant to comment it for web applications,can stay on in others)

}

}

So the mission was to set static member for connectionString , so that we’ll get the string from web.config just once. On the way we did Encrypt and Decrypt of the connectionString.

So the trouble was that we couldn’t be sure that the static field is staying or going on the request- response model of WebApi.

The easy solution was to reuse the existing code to init HttpContext.Current.Application[“connectionString”] value if it’s null at the global.asax at Application_Start function.

When initialized in that function at the host/server side of the distributed system , the HttpContext.Current.Application[“connectionString”] is initialized for the first time and until the IISReset.

If you want something constant for one request the way to go is to put it in Init function at gobal.asax 

The trouble was that the Application variable has been resetting itself each request.

The solution was that because of decryption that has been saving the web.config it has been resetting the site values held by the IIS, so actually it has been recycling the site data resetting the Application variables values also.

At the end the encryption has been executed on publish to the server, the decrypt has been decrypting the secured section but hasn’t been saving it back , so the web.config actually haven’t changed throughout running host , so the Application data kept intact.

Tag Cloud

Follow

Get every new post delivered to your Inbox.