Thursday, July 14, 2011

WCF Bindings summary

Thought to post this summary in here for my and others reference for Bindings

Binding

Security
Default Configurable

Transport Protocol

Encoding
Default Other

Host

basicHttpBinding

None,
Transport, Message, Mixed

HTTP

Text/XML, MTOM

IIS, WAS

wsHttpBinding

Message, Transport, Mixed

HTTP

Text/XML, MTOM

IIS, WAS

netTcpBinding

Transport, Message, Mixed

TCP

Binary

WAS

netNamedPipeBinding

Transport,None

Named Pipe

Binary

WAS

netMsmqBinding

Message, Transport, None

TCP

Binary

WAS

netPeerTcpBinding

Transport

P2P

Binary

 

Tuesday, July 05, 2011

Using Attributes VS elements

Thought to scribble here a little bit. Here is what I learned designing a XML object repository

1. For data use child elements don’t use attributes

2. For quarrying purposes – its ok to use attributes. I hate to write

if(attribute==null) –  Smile

Here are some things that I concluded

  • Attributes cannot contain multiple values, child elements can and so that later addition can be less code breaking
  • Attributes are not easily expandable -for future changes
  • Attributes cannot describe structures / hierarchies for data - child elements can
  • attributes are more difficult to manipulate by program code – LINQ / XQuarry works better with element. – that’s just me
  • Attribute values are not easy to test against a DTD

Tuesday, February 15, 2011

Oracle Connection with Visual Studio 2010

Pretty Simple Huh? not really.

here are the common tasks that you would like to do

(LINQ to SQL and entity framework not supported – Don’t even try it)

a. Connect the DB from VS to run some selects.

b. Create connection in code and get some data on web page.

There are 3 ways to go

1. Client side – System.data.OracleClient –> this is going away soon. so don not build you fortune apps on this. However, Visual studio uses this to connect. here is what you need (learned hard way)

  • Open VS, Data->Add new data source and select Dataset.
  • In add new connection dialog, you need to know the Host name of the Oracle server. (typically “HOST=” in your tnsnames.ora – on Oracle server)
  • you also need to know the SID which is nothing but the service ID. (typically “SERVICE_NAME=” in your tnsnames.ora – on Oracle server)

image

  • make sure that you enter servername/SID and use the test connection. – Success!

2. ODP.NET – oracle Data provider for .NET – a world in itself. More details and downloads here

http://www.oracle.com/technology/sample_code/tech/windows/odpnet/howto/connect/index.html

<- Note that this is the way to go. make sure that you do connection pooling to save money on performance consultant Smile

3. .NET data provider for Oracle – From .NET side. pretty much OLEDB stuff. Didn’t go good on performance front for Obvious reasons, last choice for me.

Thursday, September 30, 2010

Grid View – Ilist & Dataset

we all know that with c# 3.5 & now 4, we have generics and LINQ – which makes it incredibly easy to Bind grid View to ilist that you get from a LINQ query when you use the entity data model.

However, After binding this data to Grid, you want to use the template columns to update the individual rows, also you want to save the data on post backs. I found that Ilist is not the best way to go (very much depends upon situation). Here the old friend – Dataset comes to rescue.

You can bind the dataset, Cache it and update the columns after every update. [ Think about this only for Selects] as you loose entity connections when you go the dataset way (There are ways to keep this dataset and entity in Sync but i will not talk about it now)

 

So, here is a small extension function to Ilist

[Uses Reflection ]

 

public static DataSet GetDataSet<T>( IList<T> list)
    {
        Type elementType = typeof(T);
        DataSet ds = new DataSet();
        DataTable t = new DataTable();
        ds.Tables.Add(t);

        //add a column to table for each public property on T
        foreach (var propInfo in elementType.GetProperties())
        {
            Type ColType = Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType;

            t.Columns.Add(propInfo.Name, ColType);
        }

        //go through each property on T and add each value to the table
        foreach (T item in list)
        {
            DataRow row = t.NewRow();

            foreach (var propInfo in elementType.GetProperties())
            {
                row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value;
            }

            t.Rows.Add(row);
        }

        return ds;
    }

Tuesday, July 20, 2010

REST and SOAP

As we all know that this is more of a Architectural decision, there are few things that I would like to keep handy while deciding among two. These should help me taking the right design decision for right type of end point.

Here I am trying to Simplify the WCF stuff. There are 2 Major type of services and clients that you would need to know for majority of the Services consumers.

- For using WS* and all the high end transactional features of WCF, USE SOAP

Easy to consume – sometimes
Rigid – type checking, adheres to a contract

- For light weight, simple XML, if you can take care of security user REST

Lightweight – not a lot of extra xml markup
Human Readable Results

Examples -

REST - Twitter, Yahoo’s web services , Flickr, del.icio.us, pubsub, bloglines, technorati best is? – Craigslist !

SOAP - Google is consistent in implementing their web services using SOAP, with the exception of Blogger, which uses XML-RPC.

Both? -  eBay and Amazon

Now REST vs SOAP - 

1 API Flexibility & Simplicity – Rest is simple and SOAP is not

2. Rest APIs are lightweight – less bandwidth – SOAP requires Bunch of XML wrapped around where as for 4 digit stock price we dont need this overhead :)

3. Security- Posts using SOAP across organizational boundaries are safe, However RESTs Get are can always be consider safe because technically it can not modify an data. SOAP typically uses the POST to communicate with service. analyzing HTTP command in firewall is simple and REST can benefit out of it. However, to do this with SOAP you have to open the SOAP packet which makes it resource consuming. There is no way to know just by looking at the request if it wants to get or delete etc.

On the other hand, think about sending SSN as parameter in Query string. there rest goes down. Again Large data  becomes difficult

4. Authentication and Authorization – For SOAP its all upto developer. For REST – through use of certificates, common identity mgmt services like LDAP developers can make use of this

Clients – easier to create a Client for SOAPs that REST but this depends on how the framework is. Testing and troubleshooting is very easy with HTTP API than SOAP as building request is far simpler.

Caching – easer with REST as it is consumed with GET, intermediate proxies can cache the responses easily. 

Server Side complexity – SOAP is easier to code on server side, nearly all high level langs and framework makes it easy to create a  SOAP endpoints. With REST you are exposing objects methods as HTTP API and requires serialization of output to XML. Also you need to map the URI path to specific handlers.

Wednesday, June 30, 2010

Async Calls in C# – Should I blog about this?

Well, this may be for me to keep track but thought it would be usefull as I see more more and junior developer who do not understand the importance of Async programming and make blocking calls and it is frustrating especially in Web or Online apps.

There are too many frameworks available that you can take advantage of today. Another great video here I just wanted to give a very very simple steps to get this whole asynchronous business going. I assume that you know all the delegate concepts. Even if you dont know, dont worry, you really dont need to.

------------------------------------------------------------------------------------------------------

Scenario 1- Simplest of all. You want to call a function without blocking the main thread and dont care about return value dont want to know when called function finishes execution

a. Create a delegate –

 public delegate string  GetName (string myname);     –> note that you generally know what function to call and so you can decide on the delegate signature.

b. Then create object of delegate

GetName nameGetter = new GetName(GetMyRealName); <— GetMyRealName  is the actual function we want to call in Async manner.

c. Then Simply do

IAsyncResult result = nameGetter.BeginInvoke(name,null,null); <- Param 1 is parameter to original function, first null is Callback (explained latter) and last is object state (explained later)

A method GetMyRealName will be called without blocking your main thread. This is as simple as that.

*note that if your called function throws exception it will not come up

------------------------------------------------------------------------------------------------------

Scenario 2-  You want to call a function without blocking the main thread, At the same time do something else and then see what is the result of your async function

------------------------------------------------------------------------------------------------------

Scenario 3-  You want to call a function without blocking the main thread, then have another function called when your called function finishes execution

Tuesday, June 22, 2010

Showing HTML as is in Text box for ASP.NET 4.0

1. Simple thing but may get very annoying sometimes :)

2. you stored an HTML in DB. you want to show it as in Text box.

3. Note – Label and literal controls will show them as they encode by default. However Text box will not.

4. Fix – use HTML encode for the text box

ASP.NET Validate Request with .NET 4.0 – some breaking news

From old days -

1.  if you want to store HTML in DB – may be script you encode and store.

2. While post back you will get “potentially dangerous Request.Form value was detected” – To fix this you go ahead and make validateRequest=false on your page.

3. With .NET 4.0 this will not work Because this is now in the BeginRequest phase of a HTTP request, pages with validationRequest=”false”  will still get the dreaded message

Fix?

Set requestValidationMode=”2.0″ in which case the page setting will apply.

Put <httpRuntime requestValidationMode=”2.0″ /> in your web.config’s <syste.web> section

good article can be found here