Posts

Showing posts from 2012

How to hide generated javascript file in visual studio code

In this post I am going to show you a cool trick in visual studio code. Let's suppose you are writing typescript code in visual studio code and you want to hide generated javascript code.

Create .vscode folder in your folder and then create settings.json file and paste following code

{ "files.exclude": { "node_modules":true, "**/.git": true, "**/.DS_Store": true, "**/*.js.map": true, "**/*.js": {"when": "$(basename).ts"}     } }

How to read incoming and outgoing message in wcf

Image
In this post,I will show you how to read outgoing and incoming message in wcf.Create a new console application and add following assembly reference in the projectusing System.ServiceModel; using System.ServiceModel.Description;and paste following codeusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Description; namespace WCF_MessageInspector { [ServiceContract] publicinterface IHelloService { [OperationContract] string Greet(string name); } [ConsoleServiceBehavior] [ServiceBehavior] publicclass HelloService : IHelloService { publicstring Greet(string name) { return"Hello ," + name; } } class Program { staticvoid Main(string[] args) { string baseUrl = "http://localhost:12345"; ServiceHost host = new ServiceHost(typeof(HelloService), new Uri(b…

How to read namespace based xml using Linq

In this post,I will show you how to read xml with namespace.For this demo,I will use following xmlstructure<?xml version="1.0" encoding="utf-8" ?> <accounts xmlns="urn:account"> <account>1001</account> <account>1002</account> </accounts> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace XLinq { class Program { static void Main(string[] args) { var doc = XDocument.Load("accounts.xml"); var query = from x in doc.Descendants("{urn:account}account") select x; foreach (var item in query) { Console.WriteLine(item); } } } }

How to send request from CURL command tool to wcf reset service

In this post,I will show you how to test your rest service from command prompt using CURL tool. What is CURL:A command line tool for getting or sending files using URL syntax.For this demo I have created a simple reset service which contains three method as belowDownload Source Code[ICurlService]using System;using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; [ServiceContract(SessionMode = SessionMode.NotAllowed)] publicinterface ICurlService { [WebGet(UriTemplate = "date/{year}/{month}/{day}", ResponseFormat = WebMessageFormat.Xml)] [OperationContract] string GetDate(string day, string month, string year); [WebGet(UriTemplate = "greet", ResponseFormat = WebMessageFormat.Json)] [OperationContract] string Greeting(); [WebInvoke(Method = "POST", UriTemplate = "submit", BodyStyle = WebMessageBodyStyle.Wra…

Wish you all a very happy krishna janmashtami

Image

How to call python generator in c#

In this post,I will show you how to call python generator in c#.Create a console Application and then right click on the project and add new file fib.py and add following code
class Fib:

def__init__(self,num):

self.a=0

self.b=1

self.num=num

self.index=0

def__iter__(self):

returnself

def next(self):

ifself.index>self.num:

raiseStopIteration

else:

self.index +=1

self.a,self.b=self.b,self.a+self.b

returnself.a

Open Progrm.cs and add following code

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Scripting.Hosting; namespace IronPython_Tut1 { class Program { staticvoid Main(string[] args) { //Create runtime ScriptRuntime runtime = IronPython.Hosting.Python.CreateRuntime(); //excute ScriptScope scope = runtime.ExecuteFile(@"Fib.py"); //get the Fib object dynamic Fib = scope.GetVariable("Fib"); foreach (var item in Fib(10)) …

How to call python class in c#

In this post,I will show you how to call python class in c#,Create a new python class named Person.py and paste following code


class Person:
def__init__(self,firstName,lastName,age):
self.firstName=firstName
self.lastName=lastName
self.age=age

def__str__(self):
returnself.firstName +","+self.lastName+","+self.age
def get_full_name(self):
return"%s,%s"%(self.firstName, self.lastName)



Open program.cs and add following code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Scripting.Hosting; namespace IronPython_Tut1 { class Program { staticvoid Main(string[] args) { //Create runtime ScriptRuntime runtime = IronPython.Hosting.Python.CreateRuntime(); //excute ScriptScope scope = runtime.ExecuteFile(@"Person.py"); //get the Person object dynamic Person = scope.GetVariable("Person"); //Create instanc of Person class. …

IronPython and C#

Image
What is IronPython
IronPython is an open-source implementation of the Python programming language which is tightly integrated with the .Net Framework.
IronPython can use the .Net Framework and python libraries,and other .NET languaages can use Python code just as easily
In this post I will show you how to call Python method in c#.
Create a new Console application and visual studio
Right click on the project and click on Manage Nuget Packages and search for IronPython

After installing IronPython import following namespaces
using Microsoft.Scripting.Hosting;

Right click on project and add new file Calculator.py and add following python code


def add(a,b):
return a+b

Open Program.cs file and add following code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Scripting.Hosting; namespace IronPython_Tut1 { class Program { staticvoid Main(string[] args) { //Create runtime ScriptRuntime runtime = I…

How to implement twitter search in asp.net

Image
In this post,I will show you how to implement twitter search in asp.net.In this project,I am using following things



<ahref="http://search.twitter.com/search.json"target="_blank">Twitter search Api</a><ahref="http://jquery.com/"target="_blank">Jquery</a><ahref="http://handlebarsjs.com/"target="_blank">Handlebars jquery template</a><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Twitter.aspx.cs" Inherits="Twitter" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title><styletype="text/css"> body { width: 600px; margin: auto; } .tweets { …

Passing complex objects into a WCF Rest Service

In this post,I will show you how to pass complex json data from jquery to wcf service. Create a new website in visual studio.Add a new item wcf service named BlogService and replace the existing code with followingIBlog.cs using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.ServiceModel.Web; [ServiceContract] publicinterface IBlog { [WebInvoke(UriTemplate = "blogs", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] [OperationContract] string SubmitBlock(Blog blogs); } IBlogService.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.ServiceModel.Web; using System.ServiceModel.Activation; [ServiceBehavior]�…

How to add Header and Footer on every page by using HttpModule in asp.net

In this post,I will show you a very simple way to add header and footer on every page using HttpModule. Open visual studio and create a new website.Right click on website and add a new class named HeaderAndFooter,and inherit this class with IHttpModule inteface. using System; using System.Web; using System.IO; publicclass HeaderAndFooter : IHttpModule { conststring PageHeaderText = "<h1>Header Added by Module<h1>"; conststring PageFooterText = "<h1>Footer added by Module</h1>"; publicvoid Init(HttpApplication app) { // Register for pipeline events app.BeginRequest += new EventHandler(OnBeginRequest); app.EndRequest += new EventHandler(OnEndRequest); } publicvoid Dispose() { // Nothing to do here } publicvoid OnBeginRequest(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; HttpContext ctx = app.Context; // Add custom …

WCF self hosted service over HTTPS

Image
In this post I will show you how to host self hosting wcf service over ssl.
Let's create a simple wcf service
Create a new console application in visual studio and paste the following codeusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace SelfHostSSL { [ServiceContract] publicinterface IGreeting { [OperationContract] string SendGreeting(); } publicclass Implementation : IGreeting { publicstring SendGreeting() { return"Hello world"; } } class Program { staticvoid Main(string[] args) { ServiceHost host = new ServiceHost(typeof(Implementation)); host.Open(); Console.WriteLine("Service is ready"); Console.ReadLine(); } } }
After that add new item app.config and paste following code in it<?xmlversion="1.0"encoding="utf-8…

How to create Self-Signed SSL Certificate using OpenSSL

In this post I will show you how to create a Self-Signed certificate using OpenSSL.
Generate Private Key and Certificate Signing Request A private key and certificate signing request are required to create an SSL certificate. These can
be generated with a few simple commands.



C:\> openssl genrsa -des3 -out localhost.key 1024 --------------------------------------------- Enter pass phrase for site.key: Verifying - Enter pass phrase for site.key: C:\> mv localhost.key localhost.org.key C:\> openssl rsa -in localhost.org.key -out localhost.key Enter pass phrase for localhost.org.key: writing RSA key C:\> openssl req -new -key localhost.key -out server.csr ---------------------------------------------------- Country Name (2 letter code) [AU]:IN State or Province Name (full name) [Some-State]:HR
Generate SSL CertificateC:\> openssl x509 -req -days 365 -in server.cer -signkey localhost.key -out final.cerThe final.cer  file is y…

Project Euler Problem4[C#]

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProjectEuler { class Program { staticvoid Main(string[] args) { Console.WriteLine(Problem4(600851475143UL)); } /// <summary>/// http://projecteuler.net/problem=3/// </summary>/// <param name="number"></param>/// <returns></returns>publicstaticulong Problem4(ulong number) { ulong i = 2; while (i * i < number) { while (number % i == 0) { number = number / i; } i = i + 1; } return number; } } }

Project Euler Problem2 [c#]

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProjectEuler { class Program { staticvoid Main(string[] args) { Console.WriteLine(Problem2()); } /// <summary>/// http://projecteuler.net/problem=2/// </summary>/// <returns></returns>publicstaticulong Problem2() { var sum = Fibonacci().Where(x => x % 2 == 0) .TakeWhile(x => x < 4000000) .Aggregate((add, x) => add + x); return sum; } publicstatic IEnumerable<ulong> Fibonacci() { var a = 0UL; var b = 1UL; var c = a + b; while (true) { yield return c; c = a + b; a = b; b = c; } } } }

Project Euler Problem1 [C#]

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProjectEuler { class Program { staticvoid Main(string[] args) { Console.WriteLine(Problem1()); } /*If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.*/publicstaticint Problem1() { var sum = Enumerable.Range(1, 999) .Where(x => x % 3 == 0 || x % 5 == 0) .Sum(); return sum; } } }

How to used google chart in asp.net[google chart]

Image
In this post,I will show you how to display google chart in asp.net.Before going to implementation details,go through the following link

Visualization: Pie Chart


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PieChart.aspx.cs" Inherits="PiChart" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headid="Head1"runat="server"><title>jQuery Google Chart Demo</title><scripttype="text/javascript"src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> google.load("visualization", "1", { packages: [&q…