Search Chirayu Software Solutions Blog

Wednesday, January 30, 2013

wcf application


Creating simple application using WCF

First open Visual Studio and click file --> Select New --> Website Under that select WCF Service and give name for WCF Service and click OK 



Once you created application you will get default class files including Service.cs and IService.cs



Here IService.cs is an interface it does contain Service contracts and Data Contracts and Service.cs is a normal class inherited by IService where you can all the methods and other stuff.

Now open IService.cs write the following code

[ServiceContract]
public interface IService
{
[OperationContract]
string SampleMethod(string Name);
}

After that open Service.cs class file and write the following code 

public class Service : IService
{
public string SampleMethod(string Name)
{
return "First WCF Sample Program " + Name;
}
}


Here we are using basicHttpBinding for that our web.config file system.serviceModel code should be like this and I hope no need to write any code because this code already exists in your web.config file insystem.serviceModel

<system.serviceModel>
<services>
<service name="Service" behaviorConfiguration="ServiceBehavior">
<!-- Service Endpoints -->
<endpoint address="" binding="wsHttpBinding" contract="IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

Our WCF service ready to use with basicHttpBinding. Now we can call this WCF Service method console applications

After completion of WCF service creation publish or deploy your WCF Service in your system. If you don’t’ have idea on deploy tell me

After completion of deploy webservice now we can see how to use WCF Service in our console application

Calling WCF Service using Console Application

To call WCF service we have many ways like using console app, windows app and web app but here I am going for console application.

Create new console app from visual studio select project type as console application gives some name as you like.



After Creation Console application now we need to add WCF reference to our console application for that right click on your windows application and select Add Service Reference


Now one wizard will open in that give your WCF service link and click Go after add your service click OK button.


After completion of adding WCF Service write the following code in Program.cs class file Main method

static void Main(string[] args)
{
ServiceReference1.ServiceClient objService = new ServiceClient();
Console.WriteLine("Please Enter your Name");
string Message = objService.SampleMethod(Console.ReadLine());
Console.WriteLine(Message);
Console.ReadLine();
}
After that open your app.config file and check your endpoint connection for WCF Service reference that should be like this

<endpoint address=" http://localhost/WCFServiceSample/Service.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
contract="ServiceReference1.IService" name="WSHttpBinding_IService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
Now everything is ready run your application that output should be like this


Friday, January 25, 2013

wcf example


 [ServiceContract()]
    public interface IService1
    {
        [OperationContract]
        string MyOperation1(string myValue);
        [OperationContract]
        string MyOperation2(DataContract1 dataContractValue);
        [OperationContract]
        string HelloWorld(string str);
    }

    public class service1 : IService1
    {
        public string MyOperation1(string myValue)
        {
            return "Hello: " + myValue;
        }
        public string MyOperation2(DataContract1 dataContractValue)
        {
            return "Hello: " + dataContractValue.FirstName;
        }
        public string HelloWorld(string str)
        {
            return "Helloworld from " + str;
        }
    }

    [DataContract]
    public class DataContract1
    {
        string firstName;
        string lastName;

        [DataMember]
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }
        [DataMember]
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
    }
}

Thursday, January 24, 2013

WCF Basis-2


  1. Service Contract
    This contract describes all the available operations that a client can perform on the service.
    .Net uses "System.ServiceModel" Name space to work with WCF services.

    ServiceContract attribute is used to define the service contract. We can apply this attribute on class or interface. ServiceContract attribute exposes a CLR interface (or a class) as a WCF contract.
    OperationContract attribute, is used to indicate explicitly which method is used to expose as part of WCF contract. We can apply OperationContract attribute only on methods, not on properties or indexers. 
    [ServiceContract] applies at the class or interface level. [OperatiContract] applies at the method level.
  2. Data ContractThis contract defines the data types that are passed into and out of the service.
    [DataContract] attribute is used at the custom data type definition level, i.e. at class or structure level.[DataMember] attribute is used for fields, properties, and events.
  3. Fault Contract
    This contract describes about the error raised by the services.
    [FaultContract(<<type of Exception/Fault>>)] attribute is used for defining the fault contracts.
  4. Message Contracts
    This contract provides the direct control over the SOAP message structure. This is useful in inter-operability cases and when there is an existing message format you have to comply with.
    [MessageContract] attribute is used to define a type as a Message type. [MessageHeader] attribute is used for those members of the type we want to make into SOAP headers[MessageBodyMember] attribute is used for those members we want to make into parts of the SOAP body of the message.

Thursday, January 17, 2013

wcf basics


Introduction

This article is an introduction to the Windows Communication Foundation (WCF). We will try to see the basic concepts behind WCF and will try to implement a small WCF service. We will also work out some small examples of how a WCFservice can be consumed.

Background

Web services provide an efficient way of facilitating communication between applications. But there are limitations with web services too. The major limitation with web services is that the communication can happen over HTTP only. A second limitation with web services is that it provides simplex communication and there is no way to have half duplex or full duplex communication using web services. 
Windows Communication Foundation (WCF) comes to the rescue when we find ourselves not able to achieve what we want to achieve using web services, i.e., other protocols support and even duplex communication. With WCF, we can define our service once and then configure it in such a way that it can be used via HTTP, TCP, IPC, and even Message Queues. We can consume Web Services using server side scripts (ASP.NET), JavaScript Object Notations (JSON), and even REST (Representational State Transfer).
The following table illustrates the differences between a web service and a WCF service:
Web ServiceWCF Service
Communication can happen over HTTP onlyCommunication can happen over HTTP, TCP, IPC, or even MSMQ.
Only simplex and request-response communication is possibleIt can be configured to have simplex, request-response, or even full duplex communication.
They work in an stateless fashion over HTTP and are hosted inside a web server like IISThese can be hosted in many ways inside IIS, inside a Windows service, or even self hosted.
Note: Apart from these differences, there are other differences related to instance management, sessions, and data representation and serialization. We will not discuss these here as they tend to be digressing for beginners.
WCF service can be visualized as:
wcf artcile image

Understanding the basics

When we say that a WCF service can be used to communicate using different protocols and from different kinds of applications, we will need to understand how we can achieve this. If we want to use a WCF service from an application, then we have three major questions:
  1. Where is the WCF service located from a client's perspective?
  2. How can a client access the service, i.e., protocols and message formats?
  3. What is the functionality that a service is providing to the clients?
Once we have the answer to these three questions, then creating and consuming the WCF service will be a lot easier for us. The WCF service has the concept of endpoints. A WCF service provides endpoints which client applications can use to communicate with the WCF service. The answer to these above questions is what is known as the ABC of WCFservices and in fact are the main components of a WCF service. So let's tackle each question one by one.
Address: Like a webservice, a WCF service also provides a URI which can be used by clients to get to the WCF service. This URI is called as the Address of the WCF service. This will solve the first problem of "where to locate the WCFservice?" for us.
Binding: Once we are able to locate the WCF service, we should think about how to communicate with the service (protocol wise). The binding is what defines how the WCF service handles the communication. It could also define other communication parameters like message encoding, etc. This will solve the second problem of "how to communicate with the WCF service?" for us.
Contract: Now the only question we are left up with is about the functionalities that a WCF service provides. Contract is what defines the public data and interfaces that WCF service provides to the clients.