What is the difference between WCF and Web Services?


Let me tell you the definition of two and then explain the main difference.


Windows Communication Foundation(WCF):
       
         WCF stands for Windows Communication Foundation. WCF is a unified programming model for building service oriented application. WCF is the next generation of distributed applications and Web Services. It consist of a runtime and classes in System.ServiceModel namespace.

Web Services:
       
         Web Services are business logic compnents, which provide functionality via the internet using standard protocols such as HTTP. Web Services uses Simple Object Access Protocol (SOAP) in order to expose the business functionality. SOAP defines a standardized format in XML.

Differences:


WCF

Web Services
While WCF Service or a WCF component can be invoked by any protocol (like http, tcp etc.) and any transport type.

Web services can only be invoked by HTTP (traditional webservice with .asmx).

WCF are flexible.

Web Services not are flexible.
WCF service can also maintain state and session.
WCF service can not maintain state and session.

What is WCF and WPF?


Follow are the definition of Windows Communication Foundation & Windows Presentation Foundation.


Windows Communication Foundation(WCF):
            
         WCF stands for Windows Communication Foundation. WCF is a unified programming model for building service oriented application. WCF is the next generation of distributed applications and Web Services. It consist of a runtime and classes in System.ServiceModel namespace.

Windows Presentation Foundation(WPF):

       WCF stands for Windows Presentation Foundation. WPF provides developers with a unified programming model for building rich Windows smart client user experiences that incorporate UI, media, and documents. It is a next-generation presentation system for building Windows client applications with visually stunning user experiences.

Sessions in Windows Communication Foundation Applications


When a service contract specifies that it requires a session, that contract is specifying that all calls (that is, the underlying message exchanges that support the calls) must be part of the same conversation. If a contract specifies that it allows sessions but does not require one, clients can connect and either establish a session or not establish a session. If the session ends and a message is sent through the same channel an exception is thrown.

WCF sessions have the following main conceptual features:

* They are explicitly initiated and terminated by the calling application.

* Messages delivered during a session are processed in the order in which they are received.

* Sessions correlate a group of messages into a conversation. The meaning of that correlation is an abstraction. For instance, one session-based channel may correlate messages based on a shared network connection while another session-based channel may correlate messages based on a shared tag in the message body. The features that can be derived from the session depend on the nature of the correlation.

* There is no general data store associated with a WCF session.

If you are familiar with the System.Web.SessionState.HttpSessionState class in ASP.NET applications and the functionality it provides, you might notice the following differences between that kind of session and WCF sessions:

* ASP.NET sessions are always server-initiated.

* ASP.NET sessions are implicitly unordered.

* ASP.NET sessions provide a general data storage mechanism across requests.

This topic describes:

* The default execution behavior when using session-based bindings in the service model layer.

* The types of features that the WCF session-based, system-provided bindings provide.

* How to create a contract that declares a session requirement.

* How to understand and control the creation and termination of the session and the relationship of the session to the service instance.

Calling WCF service to reads from a DataBase and returns generic lists of custom objects to the client.


 _
Public Interface IRepositorio

     _
    Function ListarAlmacen() As List(Of BE.Almacen)

End Interface

 _
 _
Public Class Repositorio
    Implements IRepositorio

    Public Function ListarAlmacen() As List(Of BE.Almacen) Implements IRepositorio.ListarAlmacen
        Dim result As List(Of BE.Almacen)
        result = BL.Repositorio.Almacen.listar()
        Return result
    End Function

End Class

'some code ...

            Dim oAlmacenWCF As New repositorio.RepositorioClient
            Dim oListaAlmacenBE As List(Of BE.Almacen)
            oListaItemAlmacenBE = oAlmacenWCF.ListarAlmacen(item_id)

'some code



    
 
           
             
             
             
                
                
             
          
       
    
      

    
     
    
   
      
 

    
 
           
             
             
             
                
                
             
          
       
    
      

    
     
    
   
      
 

How to create Windows Communicatin Foundation (WCF) Service Library?


To create Windows Communicatin Foundation (WCF) Service Library you need have Microsoft Visual Studio 2008 or grater version. You cannot find WCF, WPF or WWF in Microsoft Visual Studio 2005. Now that you have Microsoft Visual Studio 2008 you need to start the application by Start - Programs - Microsoft Visual Studio 2008- Microsoft Visual Studio 2008.



When you fire Microsoft Visual Studio 2008 you will get the above image. Now you need to open "New Project" dialog box. To do that click File - New - Project. Now select the WCF node after selecting the language C# or VB or C++.


 
Before clicking OK please ensure that you have selected correct .NET Framework on top, Correct Location and the Solution Name. When you hit OK you will get a default Service1.asmx file which is the WCF Service file in Solution Explorer. Service1.asmx.cs file is the code behind file which contains the actual code of you service. In this file you will write the Service logic.


 Now you are ready to create you own WCF service.

Microsoft .NET Framework 3.0 = .NET 2.0 + Windows Communication Foundation + Windows Presentation Foundation + Windows Workflow Foundation + Windows Card Space.


Yes, Now we are into Microsoft .NET Framework 3.0 which is nothing but Microsoft .NET Framework .NET 2.0 + Windows Communication Foundation + Windows Presentation Foundation + Windows Workflow Foundation + Windows Card Space.

Microsoft ASP.NET 2.0 includes a number of services that store state in databases and other storage media. For example, the session state service manages per-user session state by storing it in-process, in memory in an external process , or in a Microsoft SQL Server database, whereas the membership service stores user names, passwords, and other membership data in Microsoft SQL Server or Microsoft Active Directory.

These and other state management services in ASP.NET 2.0 use the provider model pictured in Figure 1-1 to maximize storage flexibility. Providers abstract storage media in much the same way that device drivers abstract hardware devices. The membership service is equally at home using SQL Server or Active Directory, because ASP.NET 2.0 includes providers for each. Moreover, ASP.NET 2.0 can be extended with custom providers to add support for Web services, Oracle databases, SQL Server databases with custom schemas, and other media not supported by the built-in providers.


The Windows Communication Foundation (or WCF) is an application programming interface in the .NET Framework for building connected, service-oriented applications. or “WCF is a unification of .NET framework communication technologies “. WCF is a unification technology, which unites the following technologies:-

• NET remoting
• MSMQ
• Web services
• COM+.

The Windows Presentation Foundation (or WPF) is a graphical subsystem for rendering user interfaces in Windows-based applications. It is the new presentation API in WinFX. WPF is a two and three dimensional graphics engine. It has the following capabilities:-

• Has all equivalent common user controls like buttons, check boxes sliders etc.
• Fixed and flow format documents
• Has all of the capabilities of HTML and Flash
• 2D and 3D vector graphics
• Animation
• Multimedia
• Data binding

WCF - Hello, World


WCF stands for Windows Communication Foundation. WCF is a unified programming model for building service oriented application. WCF is the next generation of distributed applications and Web Services. It consist of a runtime and classes in System.ServiceModel namespace.

To Create a WCF Hello World, Here are the steps you need to do:
1. Fire up Microsoft Visual Studio 2008.
2. Go to File - New - Project and you can see New Project dialog box open.
3. Select Project Type Ex. Visual Basic or Visual C# and then select WCF underneath.
4. Select Wcf Service Library in Templates.
5. Type Name: HelloWorldService, Select location where you want to save your service.
6. You can verify by looking to the below image and then click OK


7. Now you need to edit the IService1.cs file. Remove the unwanted code that is automatically created by the Microsoft Visual Studio and make it as the image below:


8. Now edit the Service1.cs file and type the code as shown in the below image:
 
Step-I

Step-II

Step-III

9. Now you need to Build and Run your application. If your WCF application is error free you will get the next image screen.


10. Now double click the SayHello() method and write you name in Value area as I have add Robert and then press invoke button as shown in below image.


11. You will receive a small Pop-Up. Just click OK and your Done. In the Response area you will find the output of your Service.

How to Implementing WCF Callback in ASP.NET 3.5?


After searching here I found a method to implement WCF Callback in asp.net 3.5. I was running into similar problems where my service would make an async call to the Business layer an then wait for an event to fire back on the service. Whenver the event fired the Callback context was lost. I have not delved into the details of why this is but i ended up implementing a workaround of essentially storing a reference to the currentcontext and firing off a seperate Thread to call the to call the business layer and once it is complete fire the callback with the reference i have stored.

1) Create new class that would contain both my input request and a refence to the callback eg.
public struct MyCallbackDetails {
     public MyCallbackDetails(IMyServiceCallback callback, RequestType request) : this() 
         Callback = callback;
         Request = request;
}

public IMyServiceCallback Callback { get; set; }

public RequestType request { get; set; }
} 
2) Then i would fire off a seperate thread passing a MyCallbackDetails object instead of just the request:
public ResponseType MyServiceMethod(RequestType request) {
        //...Do Some Stuff
        //Create MyCallbackDetails object to store reference to the callback and keep channel open
        MyCallDetails callDetails = new MyCallDetails(OperationContext.Current.GetCallbackChannel(), request);
        //Fire off a new thread to call the BL and do some work
        Thread processThread = new Thread(RunCallbackMethod);
        processThread.Start(callDetails);
}
3) And my RunCallbackMethod would make the BL call and respond with a callback.
void RunCallBackMethod(Object requestDetails)
{
        //Use callbackdetails to make BL calls
        MyCallbackDetails callDetails = (MyCallbackDetails)requestDetails;
        // Make BL call - all code under here is syncrhonous
        ResponseType response = BusinessLayer.BusinessMethod(callDetails.Request);
        //NB: If your responsetype is a business object you will need to convert it to a service object
        callDetails.Callback.SomeMethod(results);
}
Yes i have now done away with having an event from my Business Layer fire back to the Service Layer however as i am firing off a seperate thread for the Business Layer it still runs asynchronously and acts the same as if i was calling the BL directly in an ASync manner and waiting for an event to notify its completion.

How to Read XML String into dataset?


I'm grabbing an XML string from a database and trying to pass it into a
dataset. Please correct me if i'm wrong

Private Function GetDataSet1() As DataSet
Dim ds As DataSet = New DataSet

Dim sb As New System.Text.StringBuilder
sb.Append("<?xml version=""1.0""?><items>")
sb.Append("<item>")
sb.Append("<itemid>100</itemid>")
sb.Append("<friendlyname1>Vegetables</friendlyname1>")
sb.Append("<friendlyname2>VG</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>200</itemid>")
sb.Append("<friendlyname1>Fruits</friendlyname1>")
sb.Append("<friendlyname2>FR</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>1001</itemid>")
sb.Append("<friendlyname1>Potatoes</friendlyname1>")
sb.Append("<friendlyname2>PO</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>100</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>1002</itemid>")
sb.Append("<friendlyname1>Spinach</friendlyname1>")
sb.Append("<friendlyname2>SP</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>100</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>2001</itemid>")
sb.Append("<friendlyname1>Apples</friendlyname1>")
sb.Append("<friendlyname2>AP</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>200</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>2002</itemid>")
sb.Append("<friendlyname1>Bananas</friendlyname1>")
sb.Append("<friendlyname2>BN</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>200</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>20001</itemid>")
sb.Append("<friendlyname1>Granny Smith</friendlyname1>")
sb.Append("<friendlyname2>GS</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>2001</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>20002</itemid>")
sb.Append("<friendlyname1>Macintosh</friendlyname1>")
sb.Append("<friendlyname2>MA</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>2001</parentid>")
sb.Append("</item>")
sb.Append("</items>")
Dim ms As New System.IO.MemoryStream
Dim writer As New System.IO.StreamWriter(ms)
writer.Write(sb.ToString())
writer.Flush()
ms.Position = 0
ds.ReadXml(ms)
Return ds
End Function 'GetDataSet1

private static EmpDataDS GetDataSet1()
{
EmpDataDS ds = new EmpDataDS();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<?xml version=\"1.0\"?><EmpDataDS>");
sb.Append("<Emp>");
sb.Append("<EmpID>123</EmpID>");
sb.Append("<LastName>Smith</LastName>");
sb.Append("<FirstName>John</FirstName>");
sb.Append("<SSN>222-22-2222</SSN>");
sb.Append("<DateOfBirth>" + DateTime.Now.ToLongDateString() +
"</DateOfBirth>");
sb.Append("<Litho1>litho123</Litho1>");
sb.Append("<Litho2>litho1 3</Litho2>");
sb.Append("</Emp>");
sb.Append("<Emp>");
sb.Append("<EmpID>456</EmpID>");
sb.Append("<LastName>Jones</LastName>");
sb.Append("<FirstName>Mary</FirstName>");
sb.Append("<SSN>333-33-3333</SSN>");
sb.Append("<DateOfBirth>01/01/1972</DateOfBirth>");
sb.Append("</Emp>");

for(int i = 0; i < 21 ; i++)
{

sb.Append("<Emp>");
sb.Append("<EmpID>" + (i+1000) + "</EmpID>");
sb.Append("<LastName>"+"EmpLastName"+"</LastName>");

sb.Append("<FirstName>"+"EmpFirstName:"+Convert.To String(i)+"</FirstName>");
sb.Append("<SSN>" + Convert.ToString(( Convert.ToString(i+1000) +
"000000000" )).Substring(0,9) + "</SSN>");
sb.Append("<DateOfBirth>01/01/1972</DateOfBirth>");
sb.Append("</Emp>");
}
sb.Append("</EmpDataDS>");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write(sb.ToString());
writer.Flush();
ms.Position = 0;
ds.ReadXml(ms);
return ds;
}

Recent Posts