Web Services - Implementation of Web Services

There are various ways of implementation of the web services.

Remote Procedure Call:

The earliest implementation of web service are called remote procedure call (RPC) these API's are implemented using existing languages like C. Later W3C standards has been established for XML based RPC called as XML-RPC. Basically in RPC client calls remote function and parameters being passed and returned in predefined order.

SOAP web service:

SOAP stands for Simple Object Access Protocol is used for structured approach rather than calling the remote function and expecting returns this client server communicate with the messages(SOAP Packets). This approach gives loosely coupled interface and less tied with the particular language. SOAP implementation is very easy as it has got named parameters rather than passing parameters in the particular order, this will lead SOAP web services easier to debug and read. To use SOAP web service you need to know three things URL of the web service, method exposed and names and data types of message parameters which needs to be passed to the method. WSDL stands for "Web Service Description Language" is often used along with SOAP to access the web service. WSDL is XML document that describes remote services available including list of available operation, parameters and data types. The client application will use SOAP to call method listed in the WSDL.

HTTP (REST) Web Services:

REST stands for "Representable State Transfer" a representation which is being returned which places client application in state and because of the traversal to the another link a new representation, so the client transfers state with each representation. REST is not standard it is an architectural style so we dont see any W3C standard defined for the REST. The REST captures chractristics of web. It uses following standers HTTP, URL, XML/HTML/JPG/PNG etc (Resource Representation) and text/xml, text/html, image/gif, image/jpeg, etc (MIME Types). Client can use the HTTP request like GET,POST,DELETE to call this web service and the web service can return the HTTP response as XML,HTML, JPG, PNG etc

JSON Web Services:

JSON stands for "Javascript Object Notation" is the lightweight data interchange format. The basic difference between JSON and XML is XML is document oriented and JSON is data oriented so JSON is more suitable for the lightweight data transfer. JSON is suited with the AJAX for the better performance. JSON is subset of object literal notation of the JavaScript. We can have web service with returned JSON data format.

Web Services - Learn the XML

XML stands for "Extensible Markup Language" which is designed to transport and store the data. Following are some of main advantages of XML because of which XML fit for using in the Web Service data transport

Advantages Of XML:
  • The First Benefit of XML is because we are writing you own markup there is no limited set of tags and user can choose appropriate names for the tags.
  • As it is text based it is platform independent.
  • Using XML anybody can design tags and have specific schema imposed on the XML to follow specific tags. In a way anybody can define the stranded for the XML.
Primary advantages of the XML for Web services that it allows programs written in different languages on different platforms to communicate with each other in a standards-based way. SOAP stands for "Simple Object Access Protocol" is the protocol which is written completely using the XML because of the above advantages, which is used in the communication using web services.

Simple Object Access Protocol:
SOAP once stood for "Simple Object Access Protocol", sometimes gets confused with Service Oriented Architecture (SOA) protocol which is not true.SOAP is rely on the XML and SOAP envelope consists of SOAP header and SOAP body which is used for communicating with the web services.
Following is the example of the SOAP envelope

WCF Interview Questions part 2

In the WCF Interview Question part 1 we have seen very basic interview question on the WCF for the beginners. In this article I have tried to have interview question answers for the advanced WCF topics

Q. Can we overload methods in the WCF?
A. Yes we can overload the methods in the WCF for that we need to use the Name property of the OperationContract to give different method name in the proxy.
[ServiceContract]
Public Interface Sample
{
[OperationContract(Name = "IntergerOverload")]
Int OverloadMethod(int paramA, int paramB)

[OperationContract(Name = "DoubleOverload")]
double OverloadMethod(double paramA, double paramB)
}
Q. What is host factory in the WCF?
A. Using Service host factory we can create service host dynamically as request comes in and we need to use ServiceFactory class for this purpose.

Q.
In the WCF service can we pass.Net exception information to the client?
A. Service will not return the exception information directly to the client but WCF service can pass it as the SOAP message.

Q. How can we set concurrency mode in the WCF?
A. Concurrency mode can be specified by ServiceBehavior attribute on the implementation class
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Single)]
Public class SamplServiceImpl : ISample{
//Implementation Code
}
There are 3 possible values of ConcurrencyMode enumeration
  • Single
  • Reentrant
  • Multiple

Q. Which protocol used for the platform independent communication in WCF?
A. SOAP(Simple Object Access Protocol) is used in WCF for the platform independent communication in the WCF.

WCF Interview Questions part 1

Q. What is WCF?
A. WCF stands for Windows Communication Foundation. It is a Software development kit for developing services on Windows. WCF is introduced in .NET 3.0. in the System.ServiceModel namespace. WCF is based on basic concepts of Service oriented architecture (SOA)

Q. What is endpoint in WCF service?
A. The endpoint is an Interface which defines how a client will communicate with the service. It consists of three main points: Address,Binding and Contract.(Also knows as ABC of WCF)

Q. Explain Address,Binding and contract for a WCF Service?
A.
Address:Address defines where the service resides.
Binding:Binding defines how to communicate with the service.
Contract:Contract defines what can be done with the service.

Q. What are the types of binding available in WCF?
A. A binding is identified by the transport it supports and the encoding it uses. Transport may be HTTP,TCP etc and encoding may be text,binary etc. The popular types of binding may be as below:
a)BasicHttpBinding
b)NetTcpBinding
c)WSHttpBinding
d)NetMsmqBinding

Q. What are the types of contract available in WCF?
A.
a)Service Contract:Describes what operations the client can perform.
b)Operation Contract : Defines the method inside Interface of Service.
c)Data Contract:Defines what data types are passed
d)Message Contract:Defines wheather a service can interact directly with messages


Q. What is the proxy for WCF Service?
A. A proxy is a class by which a service client can Interact with the service by calling different methods.We can create proxy by using svcutil.exe

Q. What is the basic difference between WCF Service and Web Service?
A. WCF service support both http and tcp protocol while web service support only http protocol.

SQL Replication Problem- Missing primary keys

Problem:
There was problem with SQL 2005 replication some tables are not replicated at all.. these tables was empty first thought came in the mind that. This might be because no data is there but other tables have replicated even though they don't have any data. Later realized that these tables are not replicated as they don't have primary key, that's why they have not replicated.

Solution:
1) Find out tables with missing primary key:
Following query can be used to find out missing primary in SQ 2005
SELECT c.name, b.name
FROM sys.tables b
INNER JOIN sys.schemas c ON b.schema_id = c.schema_id
WHERE b.type = 'U'
AND NOT EXISTS
(SELECT a.name
FROM sys.key_constraints a
WHERE a.parent_object_id = b.OBJECT_ID
AND a.schema_id = c.schema_id
AND a.type = 'PK' )
2) Add primary key to missing primary key table.
3) Do database replication initialization again.



WCF Syndication Service - RSS not redering on firefox

While working on WCF Syndication Service I have come across the interesting issue of RSS not working in Firefox.

Issue Detail:
Firefox is not able to display RSS feed published by WCF syndication service. When the RSS link is opened in the firefox it shows XML and not able to render the HTML for the RSS feed.

Issue Casue:
This issue is caused because URL is missing in the RSS feed generated via WCF Syndication Service.

Issue Fix:
This isssue is fixed just by added a Syndicationlink in syndication feed object.

SyndicationFeed feed = new SyndicationFeed();
feed.Links.Add(SyndicationLink.CreateAlternateLink(new Uri("Any Valid URL")));


C# Language 4.0

With Visual Studio 2010 we will see .Net Framework 4.0 and C# Language 4.0. In this blog post I have tried to list out features of C# Language 4.0

C# Language History:

Microsoft Launched .Net Platform and C# programming language in year 2000 from there C# become most popular language among developers.Initial version of C# is type safe,object oriented, simple programming language.
Version 2.0: Support for generics, anonymous methods, iterators, partial types and nullable types
Version 3.0: In version 3.0 more emphasis on LINQ(Language integrated query) along with that other important things are Implicitly Typed Local Variable,Extension method, Lambda expressions, Object and collection initializers, Anonymous types,Implicitly typed arrays and query expression and expression trees.

Version 4.0: Following are the features of C# version 4.0

Dynamically Typed Objects:

If class is not statically typed it may be written in COM,Ruby,Python or JavaScript. Or the class is dot net object but we don't know which type it is at compile time.In this case we will need dynamically typed objects then given type will get resolved at run time and c# compiler will not through any error at compile time.

Example:
We want to invoke Add method on Calculator class at run time we will possibly write code below
object calc = GetCalculator();
Type type = calc.GetType();
object result = type.InvokeMember("Add", BindingFlags.InvokeMethod,null,new object[]{1000,2000});
int sum = Convert.ToInt32(result);
With C# 4.0 this code will become
dynamic calc = GetCalculator();
int sum = calc.Add(1000,2000);
Optional and Named Parameters:

Optional and named parameters feature of c# 4.0 allow us to pass parameters to methods optionally we can specify the default value for optional parameters in method signature and it also allow us pass named parameter specifying name of the parameter.

Example:
We commonly use overloading of method we will possibly write code below
public int OverloadedMethod(sting requiredParam) { }
public int OverloadedMethod(sting requiredParam, string optionalParam1) { }
public int OverloadedMethod(sting requiredParam, string optionalParam1, string optionalParam2) { }
public int OverloadedMethod(sting requiredParam, string optionalParam1, string optionalParam2, string optionalParam3) { }
With C# 4.0 above method will get re factored as
public int OverloadedMethod(sting requiredParam
, string optionalParam1="SomeDefaultValue"
, string optionalParam2=null
, string optionalParam3="SomeDefaultValue")
To call above method simply use
OverloadedMethod("value1");
//Overloded method
OverloadedMethod("value1","Value2");
//Using named Parameters.
OverloadedMethod("value1",optionalParam3: "Value3");
Improved COM Interoperability:

With optional and named parameters the C# 4.0 provides significant improvement in COM interoperability.You can omit ref modifier while performing COM interoperability, although the use of ref modifier is required when not performing COM interoperability. With the previous version it is necessary to ship a Primary Interop Assembly(PIA) with your managed code.This is not necessary when using C#4.0 because compiler will inject all the types which you are using directly in the managed assembly.

Example:
While performing COM interop we possibly write
object fileName = "test.docx";
object missing = System.Reflection.Missing.Value;
doc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
With c# 4.0 above code will become
doc.SaveAs("test.docx") ;
Safe Co-Variance and Contra-Variance:

Generics in C# 4.0 support safe Co-Variance and Contra-Variance through the use of in and out keywords.

Example:
Co-Variant type is signaled using out keyword.
IEnumerable<out T>
Contra-Variant is signaled using in keyword.
IComparer<in T>