Skip to main content

Posts

Convert json string to CLR type

In this post I will show you how to convert json string to CLR types using c#


Assembly Required
System.Web.Design
using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Web.Script.Serialization; namespace ConsoleApplication1 { publicclass Likes { public List<Like> data { get; set; } } publicclass Like { publicstring Category { get; set; } publicstring Name { get; set; } publicstring ID { get; set; } publicstring created_time { get; set; } public Like() { } } publicclass Program { staticvoid Main(string[] args) { string jsonData = @"{ ""data"": [ { ""category"": ""Community"", ""name"": ""Swati"", …

How to read incoming and outgoing message in wcf

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

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. …