Wednesday, April 16, 2014

Four Timeout Property for Bindings in WCF

Four Timeout Property for Bindings in WCF
sendTimeout: time taken by WCF service to respond to client
openTimeout: Amount of time client willing to wait for WCF service to open connection
closeTimeOut: Amount of time client close the proxy connection to WCF service
receiveTimeout: Time spent by client to process response received from WCF service

 
                 sendTimeout="00:25:00">
   

 


http://stackoverflow.com/questions/1520283/wcf-service-how-to-increase-the-timeout

Powershell Issue: SOAP header Action was not understood. when use wsHttpBinding

Powershell Script Calling WCF Service.

Issue: SOAP header Action was not understood. when use wsHttpBinding

This can be solved if we use basicHttpBinding.

You might want to use the basicHttpBinding to get it working with a .NET 2.0 client. That binding already provides the compatibility required by older clients or other platforms. If you still decide to go with wsHttpBinding, you will have to use Microsoft WSE to create messages in the .NET 2.0 client that are compatible with the wsHttpBinding.
With Above statement from Stackoverflow that means we can run powershell in .net 4 runtime to even overcome above problem.
http://stackoverflow.com/questions/8518282/how-to-communicate-with-a-wcf-service-wshttpbinding-a-net-2-0-client


http://stackoverflow.com/questions/2094694/how-can-i-run-powershell-with-the-net-4-runtime

Simply modify (or create) $pshome\powershell.exe.config so that it contains the following:
xml version="1.0"?> 
 
     useLegacyV2RuntimeActivationPolicy="true"> 
         version="v4.0.30319"/> 
         version="v2.0.50727"/> 
     

Path where this config file must be created :C:\Windows\System32\WindowsPowerShell\v1.0\

Sample Script to call WCF Service with Powershell as client
$uri="http://localhost:59889/Service1.svc"

$proxy = New-WebServiceProxy -Uri $URI -Class newClass1 -Namespace WebService1




$proxy.GetName()
#this is where we have int as input to service and string as output. GetData below ... 
 
$proxy.GetData(5,$true)
 

Wednesday, March 26, 2014

Force Explicit Calling of Static Class-Static constructor

The static constructor loads or called only once when any method of static class is invoked or initialize. There is no need of explicit calling of static constructor. But when there is no static property or method to do so then we can have explicit option to call static constructor as given below
typeof(StaticClassName).TypeInitializer.Invoke(null, null);

Monday, March 24, 2014

C# Cool Code Tips

1.Specialized Collection- NameValueCollection
Real time example
Parse a string such as "p1=6&p2=7&p3=8" into a NameValueCollection

NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);

Results-
qscoll["p1"] , qscoll["p2"] and qscoll["p3"]                

Speciality- This NameValueCollection can hold duplicates Key with different Values.

   // Creates and initializes a new NameValueCollection.
      NameValueCollection myCol = new NameValueCollection();
      myCol.Add( "red", "rojo" );
      myCol.Add( "green", "verde" );
      myCol.Add( "blue", "azul" );
      myCol.Add( "red", "rouge" );


 public static void PrintKeysAndValues2( NameValueCollection myCol )  {
      Console.WriteLine( " [INDEX] KEY VALUE" );
      for ( int i = 0; i < myCol.Count; i++ )
         Console.WriteLine( " [{0}] {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i) );
      Console.WriteLine();
   }

Displays the elements using
GetKey and Get:   
[INDEX] KEY VALUE   
[0]     red rojo,rouge   
[1]     green verde   
[2]     blue azul

2. Array.ConvertAll
Real Time Example
You have string  with comma separated with integer value and you want to have int[] conversion from string [] array.

look at this now!

string commaSep= "1,2,3"
int[] transform= Array.ConvertAll(commaSep.split(','),s=>int.parse(s))
 

Thursday, March 13, 2014

Linq to replace Dictionary values with XML formed tag values.

static void Main(string[] args)
{
 
IDictionary<string, string> placeHolder = new Dictionary<string, string>();

placeHolder.Add("#FirstName#","Phil");

placeHolder.Add("#MiddleName#", "J");

placeHolder.Add("#LastName#", "Haack");
        string parsedXmlString=GetParsedTemplate(@"



#FirstName#

#MiddleName#

#LastName#





", placeHolder);}
 
 
 

public static string GetParsedTemplate(string wellFormedXML, IDictionary<string, string> placeHolder)
{

placeHolder.AsEnumerable().ToList().ForEach(t => wellFormedXML = wellFormedXML.Replace(t.Key, t.Value));

return wellFormedXML;


}

Tuesday, January 7, 2014

Known Issue GetOwinContext() return System.NullReferenceException

HttpContext.Current.GetOwinContext().Authenticate returns System.NullReferenceException when used in web api ie. Apicontroller.

The reason is the original context is not restored after leaving the await block. This is when used with task parallelism await async block.

So switching to .NET 4.5 solved the problem. But why? It seems that in ASP.NET 4.5, a task friendly synchronization context got introduced. This synchronization context ensures that the originel context is restored after leaving the await block.
So make sure that you either:
  • Set httpRuntime.targetFramework to 4.5, or
  • In your appSettings, set aspnet:UseTaskFriendlySynchronizationContext to true.
http://bartwullems.blogspot.se/2013/09/aspnet-web-api-httpcontextcurrent-is.html

http://vegetarianprogrammer.blogspot.se/2012/12/understanding-synchronizationcontext-in.html

Monday, January 6, 2014

Variable Scoping in Javascript

It is always important to make use of VAR to declare variable identifiers. If you miss doing this , there are two implications.

1. It is then by default act as GLOBAL variable.

2. Chances are these variables will be overwritten everytime developers uses same variable name across the modules. This is worst defect and sometimes difficult to figure it out.

/* Declared globally. */

function FindTruth(id) {


}


// Later in your page, another programmer adds…

var FindTruth= $(‘Overwritten-False-Fact’); // The FindTruth function just got

// overwritten.

Namespacing- Best Practice



/* Using a namespace. */

var MyNamespace = {

FindTruth: function(id) {


},

// Other methods can go here as well.

}

// Later in your page, another programmer adds…
var Findtruth= $(‘Overwritten-False-Fact’);// Nothing was overwritten.

In JavaScript Object Oriented programming this is Singleton pattern.

Now one can access method using MyNamespace.Findtruth(Id);

We can even structure it separately and then later add group of methods to decorate as library.

/* Super namespace. */

var SuperLib = {};

SuperLib.Common = {

// A singleton with common methods used by all objects and modules.

};

SuperLib.DataAccess= {

//Hold and transfer data

};

SuperLib.Helper= {

//Html Helper

};