Click here to Skip to main content
15,886,693 members
Articles / Web Development / ASP.NET
Tip/Trick

Hosting WCF Service III

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Aug 2015CPOL3 min read 7.6K   83   6  
Hosting WCF Service - IIS Hosting

Introduction

WCF service can be hosted in different ways.

I recommend you to read the previous post : Self Hosting, Windows Hosting

In this example, I will show, IIS-Hosting method to consume a WCF service.

Using the code

Our sample application performs the simple integer arithmetic operations.

  1. Create a WCF Service
  2. Publish WCF Service
  3. IIS Hosting
  4. Consume IIS Hosted WCF Service

Part 1 : Create WCF service

To create a wcf service refer the Self Hosting post -> Part 1 : Create WEF service

Part 2 : Publish WCF Service

1. Right click on the project, then select Publish

Image 1

2. Create profile to publish the application.

Image 2

3. Click on Custom, it will ask to enter the profile name. Enter a profile name, then click Ok button.

Image 3

4. After clicking Next it will go to Connection. In Publish Method dropdown, select "File System" and give a valid path.

Image 4

5. After clicking Next, it will go to Settings. Set configuration as "Release" and enable "Delete all the existing files prior to publish"

Image 5

6. After clicking publish, PublishProfiles folder included to the project and publish summay also shown.

Image 6

7. After successful publish, published files will available in the give path.

Image 7

 

Part 3 : IIS Hosting

1. Open Internet Information Services (IIS)

Image 8

2. Expand the IIS name and right-click on Sites then select Add Web Site.

Image 9

3. Give a site name and set the physical path of the published wcf service. If IIS running any application with port 80, assign some other port number.

Image 10

4. Now view the service in a browser. Right click on the site ->Manage Web Site ->Browse

Image 11

5. In the viewing browser, if you are getting Configuration Error at targetFramework="4.6", go the physical path of the published web service, open web.config file and change it to targetFramework="4.5".

Image 12

6. After the above change, refresh the browser. Again if you are getting "The service cannot be activated because......" error, we need to do a modification in our WCF service application.

Image 13

The above error occurs because in order to make the WCF Service work in ASP.Net compatibility mode, you also need to decorate the WCF Service class with the AspNetCompatibilityRequirements attribute.

Steps to make the WCF Service work in ASP.Net compatibility mode :

i . Set aspNetCompatibilityEnabled attribute to true for the serviceHostingEnvironment tag in system.serviceModel section of the Web.Config file.

C++
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />

ii . Import the following namespaces in the WCF Service class.

C++
using System.Web;
using System.ServiceModel.Activation;

iii . Decorate the WCF Service class with the AspNetCompatibilityRequirements attribute.

C++
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Calculator : ICalculator
{
    public int Addition(int number1, int number2)
    {
        try
        {
            return number1 + number2;
        }
        catch (Exception exception)
        {
            throw new FaultException<exceptionmessage>(new ExceptionMessage(exception.Message));
        }
    }
}</exceptionmessage>

7. After ASP.Net compatibility mode setup, publish the WCF service again, then refresh the browser. We can view the service in browser.

Image 14

 

Part 4 : Consume IIS Hosted WCF Service

1. Create a console application.

2. Add the "System.ServiceModel" reference assembly.

3. Add a service reference to the console application. In this example our service is running in http://localhost:8089/Calculator.svc

Image 15

4. Open Console Application Progarm.cs file and add the following codes.

  • Include the following using statements.
C++
using System.ServiceModel;
using IISServiceConsume.CalculatorServiceReference;
  • Replace the below code.
C++
class Program
{
    static void Main(string[] args)
    {
        WCFServiceConsume();

    }

    public static void ConsoleClose()
    {
        Console.WriteLine("Are you sure to close the application? (1/0)\n");
        string close = Console.ReadLine();
        if (close == "1")
        {
            Console.Clear();
            WCFServiceConsume();
        }
    }
    public static void WCFServiceConsume()
    {
        try
        {
            Console.WriteLine("IIS-Hosting a WCF Service in Console Application\n");
            Console.WriteLine("-------------------------------------------------\n");
            Console.WriteLine("Enter first integer number : ");
            int number1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter second integer number : ");
            int number2 = Convert.ToInt32(Console.ReadLine());

            CalculatorServiceReference.CalculatorClient cal = new CalculatorServiceReference.CalculatorClient();

            try
            {
                int addition = cal.Addition(number1, number2);
                Console.WriteLine("Addition Result : " + addition);
            }
            catch (FaultException<exceptionmessage> exceptionFromService)
            {
                Console.WriteLine("Addition Service Error : " + exceptionFromService.Detail.errorMessageOfAction);
            }

            try
            {
                int subtraction = cal.Subtraction(number1, number2);
                Console.WriteLine("Subtraction Result : " + subtraction);
            }
            catch (FaultException<exceptionmessage> exceptionFromService)
            {
                Console.WriteLine("Subtraction Service Error : " + exceptionFromService.Detail.errorMessageOfAction);
            }

            try
            {
                int multiplication = cal.Multiplication(number1, number2);
                Console.WriteLine("Multiplication Result : " + multiplication);
            }
            catch (FaultException<exceptionmessage> exceptionFromService)
            {
                Console.WriteLine("Multiplication Service Error : " + exceptionFromService.Detail.errorMessageOfAction);
            }

            try
            {
                int division = cal.Division(number1, number2);
                Console.WriteLine("Division Result : " + division);
            }
            catch (FaultException<exceptionmessage> exceptionFromService)
            {
                Console.WriteLine("Division Service Error : " + exceptionFromService.Detail.errorMessageOfAction);
            }
            Console.WriteLine("*********************************\n");
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
            Console.ReadKey();
        }
        finally
        {
            ConsoleClose();
        }
    }
}
</exceptionmessage></exceptionmessage></exceptionmessage></exceptionmessage>

5. Run the console application.

 

Output

Image 16

History

1st Version : 2015-08-06

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
-- There are no messages in this forum --