How to: Host a WCF Service in IIS - WCF (2024)

  • Article

This topic outlines the basic steps required to create a Windows Communication Foundation (WCF) service that is hosted in Internet Information Services (IIS). This topic assumes you are familiar with IIS and understand how to use the IIS management tool to create and manage IIS applications. For more information about IIS see Internet Information Services. A WCF service that runs in the IIS environment takes full advantage of IIS features, such as process recycling, idle shutdown, process health monitoring, and message-based activation. This hosting option requires that IIS be properly configured, but it does not require that any hosting code be written as part of the application. You can use IIS hosting only with an HTTP transport.

For more information about how WCF and ASP.NET interact, see WCF Services and ASP.NET. For more information about configuring security, see Security.

For the source copy of this example, see IIS Hosting Using Inline Code.

To create a service hosted by IIS

  1. Confirm that IIS is installed and running on your computer. For more information about installing and configuring IIS see Installing and Configuring IIS 7.0

  2. Create a new folder for your application files called "IISHostedCalcService", ensure that ASP.NET has access to the contents of the folder, and use the IIS management tool to create a new IIS application that is physically located in this application directory. When creating an alias for the application directory use "IISHostedCalc".

  3. Create a new file named "service.svc" in the application directory. Edit this file by adding the following @ServiceHost element.

    <%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"%>
  4. Create an App_Code subdirectory within the application directory.

  5. Create a code file named Service.cs in the App_Code subdirectory.

  6. Add the following using directives to the top of the Service.cs file.

    using System;using System.ServiceModel;
  7. Add the following namespace declaration after the using directives.

    namespace Microsoft.ServiceModel.Samples{}
  8. Define the service contract inside the namespace declaration as shown in the following code.

    [ServiceContract]public interface ICalculator{ [OperationContract] double Add(double n1, double n2); [OperationContract] double Subtract(double n1, double n2); [OperationContract] double Multiply(double n1, double n2); [OperationContract] double Divide(double n1, double n2);}
    <ServiceContract()> _Public Interface ICalculator <OperationContract()> _ Function Add(ByVal n1 As Double, _ ByVal n2 As Double) As Double <OperationContract()> _ Function Subtract(ByVal n1 As Double, _ ByVal n2 As Double) As Double <OperationContract()> _ Function Multiply(ByVal n1 As Double, _ ByVal n2 As Double) As Double <OperationContract()> _ Function Divide(ByVal n1 As Double, _ ByVal n2 As Double) As DoubleEnd Interface
  9. Implement the service contract after the service contract definition as shown in the following code.

    public class CalculatorService : ICalculator{ public double Add(double n1, double n2) { return n1 + n2; } public double Subtract(double n1, double n2) { return n1 - n2; } public double Multiply(double n1, double n2) { return n1 * n2; } public double Divide(double n1, double n2) { return n1 / n2; }}
    Public Class CalculatorService Implements ICalculator Public Function Add(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Add Return n1 + n2 End Function Public Function Subtract(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Subtract Return n1 - n2 End Function Public Function Multiply(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Multiply Return n1 * n2 End Function Public Function Divide(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Divide Return n1 / n2 End FunctionEnd Class
  10. Create a file named "Web.config" in the application directory and add the following configuration code into the file. At run time, the WCF infrastructure uses the information to construct an endpoint that client applications can communicate with.

    <?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <services> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviors"> <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc --> <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /> <!-- The mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehaviors"> <!-- Add the following element to your service behavior configuration. --> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>

    This example explicitly specifies endpoints in the configuration file. If you do not add any endpoints to the service, the runtime adds default endpoints for you. For more information about default endpoints, bindings, and behaviors see Simplified Configuration and Simplified Configuration for WCF Services.

  11. To make sure the service is hosted correctly, open a browser and browse to the service's URL: http://localhost/IISHostedCalc/Service.svc

Example

The following is a complete listing of the code for the IIS hosted calculator service.

using System;using System.ServiceModel;namespace Microsoft.ServiceModel.Samples{ [ServiceContract] public interface ICalculator { [OperationContract] double Add(double n1, double n2); [OperationContract] double Subtract(double n1, double n2); [OperationContract] double Multiply(double n1, double n2); [OperationContract] double Divide(double n1, double n2); } public class CalculatorService : ICalculator { public double Add(double n1, double n2) { return n1 + n2; } public double Subtract(double n1, double n2) { return n1 - n2; } public double Multiply(double n1, double n2) { return n1 * n2; } public double Divide(double n1, double n2) { return n1 / n2; } }}
Imports System.ServiceModelNamespace Microsoft.ServiceModel.Samples <ServiceContract()> _ Public Interface ICalculator <OperationContract()> _ Function Add(ByVal n1 As Double, _ ByVal n2 As Double) As Double <OperationContract()> _ Function Subtract(ByVal n1 As Double, _ ByVal n2 As Double) As Double <OperationContract()> _ Function Multiply(ByVal n1 As Double, _ ByVal n2 As Double) As Double <OperationContract()> _ Function Divide(ByVal n1 As Double, _ ByVal n2 As Double) As Double End Interface Public Class CalculatorService Implements ICalculator Public Function Add(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Add Return n1 + n2 End Function Public Function Subtract(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Subtract Return n1 - n2 End Function Public Function Multiply(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Multiply Return n1 * n2 End Function Public Function Divide(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Divide Return n1 / n2 End Function End Class
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <services> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviors"> <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc --> <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /> <!-- The mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehaviors"> <!-- Add the following element to your service behavior configuration. --> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>

See also

  • Hosting in Internet Information Services
  • Hosting Services
  • WCF Services and ASP.NET
  • Security
  • Windows Server App Fabric Hosting Features
How to: Host a WCF Service in IIS - WCF (2024)

References

Top Articles
Was sind Hernien? | Die Techniker
FAQ – Rückbau Hertie-Gebäude
Beau Is Afraid Showtimes Near Island 16 Cinema De Lux
Oklahoma Dam Generation Schedule
Www.myschedule.kp.org
Mychart.texaschildrens.org.mychart/Billing/Guest Pay
Creepshot. Org
123Movies The Idol
How to Perform Subdomain Enumeration: Top 10 Tools
Uptown Cheapskate Fort Lauderdale
How To Get Mega Ring In Pokemon Radical Red
‘Sound of Freedom’ Is Now Streaming: Here’s Where to Stream the Controversial Crime Thriller Online for Free
Craigslist Pets Huntsville Alabama
Does Publix Have Sephora Gift Cards
Roadwarden Thais
Word Jam 1302
Parentvue Stma
Is Tql A Pyramid Scheme
R/Skinwalker
Perse03_
Bank Of America.aomc
Gay Cest Com
Starfield PC, XSX | GRYOnline.pl
Forest | Definition, Ecology, Types, Trees, Examples, & Facts
Drys Pharmacy
Diablo 3 Legendary Reforge
My Meet Scores Online Gymnastics
Kaelis Dahlias
Карта слов и выражений английского языка
The Angel Next Door Spoils Me Rotten Gogoanime
Understanding P Value: Definition, Calculation, and Interpretation - Decoding Data Science
Poskes Parts
Megan Montaner Feet
三上悠亜 Thank You For Everything Mikami Yua Special Photo Book
Emily Dealy Obituary
R Mcoc
Www.playgd.mobi Wallet
Wlox Jail Docket
Dust Cornell
Presentato il Brugal Maestro Reserva in Italia: l’eccellenza del rum dominicano
Scarabaeidae), with a key to related species – Revista Mexicana de Biodiversidad
My Perspectives Grade 10 Volume 1 Answer Key Pdf
Molly Leach from Molly’s Artistry Demonstrates Amazing Rings in Acryli
Rub Md Okc
Atlanta Farm And Garden By Owner
Rubrankings Austin
600 Aviator Court Vandalia Oh 45377
Does Lowes Take Ebt
Geico Proof Of Residency
Craigslist High Springs Fl
Raleigh Craigs List
German police arrest 25 suspects in plot to overthrow state – DW – 12/07/2022
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 5943

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.