Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 29 - Hosting wcf service in IIS

Suggested Videos
Part 26 - What is a windows service
Part 27 - Hosting a wcf service in a windows service
Part 28 - Advantages and disadvantages of hosting a wcf service in a windows service



In this video we will discuss, hosting wcf service in IIS. This is continuation to Part 28. Please watch Part 28, before proceeding.

To host a wcf service in IIS, create a file with .svc extension. This file contains ServiceHost directive. The Service attribute of ServiceHost directive, specifies which service this file points to. The service code can reside in
1. The .svc file
2. A separate assembly
3. A file in App_Code folder

The configuration for the wcf service goes in web.config file.

The ServiceHost directive in .svc file is responsible for creating an instance of ServiceHost when required. There is no need to write code to instantiate and start ServiceHost, as we did with self hosting.



Here are the steps to host a wcf service in IIS
Step 1: Right click on HelloService solution in Solution Explorer and select Add - New Web Site. Fill in the details as shown in the image below.
Hosting wcf service in iis

Step 2: Delete IService.cs and Service.cs files that are present under App_Code folder.

Step 3: Add a reference to HelloService WCF library. To do this, right click on HelloService\HelloServiceIISHost project in Solution Explorer and select "Add Reference" option and add a reference to HelloService project.

Step 4: Rename Service.svc file to HelloService.svc. Copy and paste the following code in HelloService.svc file.
<%@ ServiceHost Language="C#" Debug="true" Service="HelloService.HelloService"%>

Step 5: Copy and paste the following code in web.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehavior"
               name="HelloService.HelloService">
        <endpoint address="HelloService" binding="basicHttpBinding"
            contract="HelloService.IHelloService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

</configuration>

Step 6: Configure IIS
a) Open IIS. In the run window, type INETMGR and press enter key.
b) Expand Sites folder. Right click on "Default Web Site" and select "Add Application" option.
c) In "Add Application" make sure you have the following settings and click OK.
    Alias = HelloService
    Application pool = ASP.NET v4.0
    Physical path = C:\HelloService\HelloServiceIISHost
Hosting wcf in iis

Step 7: Navigate to http://localhost/HelloService/HelloService.svc. On this page you should have link to view the WCF service WSDL.

Step 8: Test if the service works as expected using a client application.

In our next video, we will discuss the advantages and disadvantages of hosting WCF service in IIS.

wcf tutorial

7 comments:

  1. Navigating to "http://localhost/HelloService/HelloService.svc" gives me error that reads

    "The type 'HelloService.HelloService,HelloService', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found."

    I could not figure it out even after trying & googling for hrs..Please help( I am using IIS 8.5 in Win 8.1)

    Here are the details:
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: The type 'HelloService.HelloService,HelloService', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:


    [InvalidOperationException: The type 'HelloService.HelloService,HelloService', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.]
    System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +62739
    System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +1429
    System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity) +52
    System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +598

    [ServiceActivationException: The service '/HelloService/HelloService.svc' cannot be activated due to an exception during compilation. The exception message is: The type 'HelloService.HelloService,HelloService', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found..]
    System.Runtime.AsyncResult.End(IAsyncResult result) +485948
    System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +174
    System.ServiceModel.Activation.ServiceHttpModule.EndProcessRequest(IAsyncResult ar) +345998
    System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +9683501

    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34212

    ReplyDelete
  2. When I try to browse the .svc file in iis I am getting the following error. So kindly help me.

    Error Summary
    HTTP Error 500.19 - Internal Server Error
    The requested page cannot be accessed because the related configuration data for the page is invalid.
    Detailed Error Information
    Module IIS Web Core
    Notification BeginRequest
    Handler Not yet determined
    Error Code 0x80070005
    Config Error Cannot read configuration file due to insufficient permissions
    Config File \\?\C:\Users\User\Desktop\wcf\IISHost\web.config
    Requested URL http://localhost:80/Fish/FishService.svc
    Physical Path C:\Users\User\Desktop\wcf\IISHost\FishService.svc
    Logon Method Not yet determined
    Logon User Not yet determined
    Config Source
    -1:
    0:
    Links and More Information
    This error occurs when there is a problem reading the configuration file for the Web server or Web application. In some cases, the event logs may contain more information about what caused this error.
    View more information »

    ReplyDelete
  3. HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.

    ReplyDelete
  4. please go to windows activation turn on and go to .Net framework 4.5 advaned services expand it select WCF Services expand it and check the HTTP Activation, that should work

    ReplyDelete
  5. I want to consume this iis hosted service with remote client... It cannot add service reference like this. how i should add service reference with remote client??? plz help. Thank u.

    ReplyDelete

  6. why wcf is running from vs 2015 and sql 2014 but when i put on local iis can not run

    ReplyDelete
  7. In the svc file we have Service1 implementing Iservice1 interface. As per the steps we have removed those files and added reference to our wcf service. But now while compiling the code written in svc file creates problem as reference to service1.cs has been removed. How to resolve this ?

    ReplyDelete

It would be great if you can help share these free resources