News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. ***
  May 30 2015 1:04PM     John
  0 Comments    868 Views  
In this article John demonstrates, how to create a simple WCF Service in C#. The following is the procedure.
Create Service Contract
1. Launch any edition of Visual Studio 2010 or 2012.

2. Create a project by choosing WCF Service Application project template from WCF tab.

3. Let us give the WCF Service Name "MySampleWCFService".

4. Ensure the attribute of the Interface is set as "[ServiceContract]".

5. Define the functions you want to create as part of the Contract Set the Attribute of the functions as "[OperationContract]"
You will create an IService1 Service Contract with the four basic calculator operations as the following.
IService1.cs
namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        double AddNumbers(double number1, double number2);

        [OperationContract]
        double SubstractNumbers(double number1, double number2);

        [OperationContract]
        double MultiplyNumbers(double number1, double number2);

        [OperationContract]
        double DivisionNumbers(double number1, double number2);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}
You have created a Service Contract above with four Operation Contracts. These contracts will be part of the Service Contract and exposed to clients. By this step you have created the ICalculator Service Contract.
Implement the Service
IService1.svc.cs
Replace the getdata function with the following code.
namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public double AddNumbers(double number1, double number2)
        {
            double result = number1 + number2;
            return result;
        }
 
        public double SubstractNumbers(double number1, double number2)
        {
            double result = number1 - number2;
            return result;
        }
 
        public double MultiplyNumbers(double number1, double number2)
        {
            double result = number1 * number2;
            return result;
        }
 
        public double DivisionNumbers(double number1, double number2)
        {
            double result = number1 / number2;
            return result;
        }
    }
}
Output
Now you need to host the service.
There are many processes in which a WCF Service can be hosted ASP.Net Web Server.
To host it press F5 in Visual Studio.
In the browser you will see the Service as in the following.
I hope this page will helps you to create first WCF service in C#.Thanks.
BackToTop
Comments



 
Search
1&1 Hosting
Recent Posts
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2016 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2016 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2016 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2016 10:20AM Posted By Micheal
Read excel file and convert to List in C#
Feb 25 2016 6:26AM Posted By Michael
Encryption and Decryption for Password in C#
Feb 17 2016 3:52AM Posted By Michael
How to create Chart in ASP.Net C#
Feb 16 2016 8:03AM Posted By Michael Ryan
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: