How to: Expose a Contract to SOAP and Web Clients - WCF (2024)

  • Article

By default, Windows Communication Foundation (WCF) makes endpoints available only to SOAP clients. In How to: Create a Basic WCF Web HTTP Service, an endpoint is made available to non-SOAP clients. There may be times when you want to make the same contract available both ways, as a Web endpoint and as a SOAP endpoint. This topic shows an example of how to do this.

To define the service contract

  1. Define a service contract using an interface marked with the ServiceContractAttribute, WebInvokeAttribute and the WebGetAttribute attributes, as shown in the following code:

    [ServiceContract]public interface IService{ [OperationContract] [WebGet] string EchoWithGet(string s); [OperationContract] [WebInvoke] string EchoWithPost(string s);}
    <ServiceContract()> _Public Interface IService <OperationContract()> _ <WebGet()> _ Function EchoWithGet(ByVal s As String) As String <OperationContract()> _ <WebInvoke()> _ Function EchoWithPost(ByVal s As String) As StringEnd Interface

    Note

    By default WebInvokeAttribute maps POST calls to the operation. You can, however, specify the method to map to the operation by specifying a "method=" parameter. WebGetAttribute does not have a "method=" parameter and only maps GET calls to the service operation.

  2. Implement the service contract, as shown in the following code:

    public class Service : IService{ public string EchoWithGet(string s) { return "You said " + s; } public string EchoWithPost(string s) { return "You said " + s; }}
    Public Class Service Implements IService Public Function EchoWithGet(ByVal s As String) As String Implements IService.EchoWithGet Return "You said " + s End Function Public Function EchoWithPost(ByVal s As String) As String Implements IService.EchoWithPost Return "You said " + s End FunctionEnd Class

To host the service

  1. Create a ServiceHost object, as shown in the following code:

    ServiceHost host = new ServiceHost(typeof(Service), new Uri("http://localhost:8000"));
    Dim host As New ServiceHost(GetType(Service), New Uri("http://localhost:8000"))
  2. Add a ServiceEndpoint with BasicHttpBinding for the SOAP endpoint, as shown in the following code:

    host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "Soap");
    host.AddServiceEndpoint(GetType(IService), New BasicHttpBinding(), "Soap")
  3. Add a ServiceEndpoint with WebHttpBinding for the non-SOAP endpoint and add the WebHttpBehavior to the endpoint, as shown in the following code:

    ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "Web");endpoint.Behaviors.Add(new WebHttpBehavior());
    Dim endpoint As ServiceEndpointendpoint = host.AddServiceEndpoint(GetType(IService), New WebHttpBinding(), "Web")endpoint.Behaviors.Add(New WebHttpBehavior())
  4. Call Open() on a ServiceHost instance to open the service host, as shown in the following code:

    host.Open();
    host.Open()

To call service operations mapped to GET in a browser

  1. In a web browser, browse to "http://localhost:8000/Web/EchoWithGet?s=Hello, world!". The URL contains the base address of the service (http://localhost:8000/), the relative address of the endpoint (""), the service operation to call ("EchoWithGet"), and a question mark followed by a list of named parameters separated by an ampersand (&).

To call service operations on the Web endpoint in code

  1. Create an instance of WebChannelFactory<TChannel> within a using block, as shown in the following code.

    using (WebChannelFactory<IService> wcf = new WebChannelFactory<IService>(new Uri("http://localhost:8000/Web")))
    Using wcf As New WebChannelFactory(Of IService)(New Uri("http://localhost:8000/Web"))

Note

Close() is automatically called on the channel at the end of the using block.

  1. Create the channel and call the service, as shown in the following code.

    IService channel = wcf.CreateChannel();string s;Console.WriteLine("Calling EchoWithGet by HTTP GET: ");s = channel.EchoWithGet("Hello, world");Console.WriteLine(" Output: {0}", s);Console.WriteLine("");Console.WriteLine("This can also be accomplished by navigating to");Console.WriteLine("http://localhost:8000/Web/EchoWithGet?s=Hello, world!");Console.WriteLine("in a web browser while this sample is running.");Console.WriteLine("");Console.WriteLine("Calling EchoWithPost by HTTP POST: ");s = channel.EchoWithPost("Hello, world");Console.WriteLine(" Output: {0}", s);
    Dim channel As IService = wcf.CreateChannel()Dim s As StringConsole.WriteLine("Calling EchoWithGet by HTTP GET: ")s = channel.EchoWithGet("Hello, world")Console.WriteLine(" Output: {0}", s)Console.WriteLine("")Console.WriteLine("This can also be accomplished by navigating to")Console.WriteLine("http://localhost:8000/Web/EchoWithGet?s=Hello, world!")Console.WriteLine("in a web browser while this sample is running.")Console.WriteLine("")Console.WriteLine("Calling EchoWithPost by HTTP POST: ")s = channel.EchoWithPost("Hello, world")Console.WriteLine(" Output: {0}", s)

To call service operations on the SOAP endpoint

  1. Create an instance of ChannelFactory within a using block, as shown in the following code.

    using (ChannelFactory<IService> scf = new ChannelFactory<IService>(new BasicHttpBinding(), "http://localhost:8000/Soap"))
    Using scf As New ChannelFactory(Of IService)(New BasicHttpBinding(), "http://localhost:8000/Soap")
  2. Create the channel and call the service, as shown in the following code.

    IService channel = scf.CreateChannel();string s;Console.WriteLine("Calling EchoWithGet on SOAP endpoint: ");s = channel.EchoWithGet("Hello, world");Console.WriteLine(" Output: {0}", s);Console.WriteLine("");Console.WriteLine("Calling EchoWithPost on SOAP endpoint: ");s = channel.EchoWithPost("Hello, world");Console.WriteLine(" Output: {0}", s);
    Dim channel As IService = scf.CreateChannel()Dim s As StringConsole.WriteLine("Calling EchoWithGet on SOAP endpoint: ")s = channel.EchoWithGet("Hello, world")Console.WriteLine(" Output: {0}", s)Console.WriteLine("")Console.WriteLine("Calling EchoWithPost on SOAP endpoint: ")s = channel.EchoWithPost("Hello, world")Console.WriteLine(" Output: {0}", s)

To close the service host

  1. Close the service host, as shown in the following code.

    host.Close();
    host.Close()

Example

The following is the full code listing for this topic:

using System;using System.Collections.Generic;using System.ServiceModel;using System.ServiceModel.Description;using System.ServiceModel.Web;using System.Text;namespace Microsoft.ServiceModel.Samples.BasicWebProgramming{ [ServiceContract] public interface IService { [OperationContract] [WebGet] string EchoWithGet(string s); [OperationContract] [WebInvoke] string EchoWithPost(string s); } public class Service : IService { public string EchoWithGet(string s) { return "You said " + s; } public string EchoWithPost(string s) { return "You said " + s; } } class Program { static void Main(string[] args) { ServiceHost host = new ServiceHost(typeof(Service), new Uri("http://localhost:8000")); host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "Soap"); ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "Web"); endpoint.Behaviors.Add(new WebHttpBehavior()); try { host.Open(); using (WebChannelFactory<IService> wcf = new WebChannelFactory<IService>(new Uri("http://localhost:8000/Web"))) { IService channel = wcf.CreateChannel(); string s; Console.WriteLine("Calling EchoWithGet by HTTP GET: "); s = channel.EchoWithGet("Hello, world"); Console.WriteLine(" Output: {0}", s); Console.WriteLine(""); Console.WriteLine("This can also be accomplished by navigating to"); Console.WriteLine("http://localhost:8000/Web/EchoWithGet?s=Hello, world!"); Console.WriteLine("in a web browser while this sample is running."); Console.WriteLine(""); Console.WriteLine("Calling EchoWithPost by HTTP POST: "); s = channel.EchoWithPost("Hello, world"); Console.WriteLine(" Output: {0}", s); Console.WriteLine(""); } using (ChannelFactory<IService> scf = new ChannelFactory<IService>(new BasicHttpBinding(), "http://localhost:8000/Soap")) { IService channel = scf.CreateChannel(); string s; Console.WriteLine("Calling EchoWithGet on SOAP endpoint: "); s = channel.EchoWithGet("Hello, world"); Console.WriteLine(" Output: {0}", s); Console.WriteLine(""); Console.WriteLine("Calling EchoWithPost on SOAP endpoint: "); s = channel.EchoWithPost("Hello, world"); Console.WriteLine(" Output: {0}", s); Console.WriteLine(""); } Console.WriteLine("Press [Enter] to terminate"); Console.ReadLine(); host.Close(); } catch (CommunicationException cex) { Console.WriteLine("An exception occurred: {0}", cex.Message); host.Abort(); } } }}
Imports System.Collections.GenericImports System.ServiceModelImports System.ServiceModel.DescriptionImports System.ServiceModel.WebImports System.Text<ServiceContract()> _Public Interface IService <OperationContract()> _ <WebGet()> _ Function EchoWithGet(ByVal s As String) As String <OperationContract()> _ <WebInvoke()> _ Function EchoWithPost(ByVal s As String) As StringEnd InterfacePublic Class Service Implements IService Public Function EchoWithGet(ByVal s As String) As String Implements IService.EchoWithGet Return "You said " + s End Function Public Function EchoWithPost(ByVal s As String) As String Implements IService.EchoWithPost Return "You said " + s End FunctionEnd ClassModule Program Sub Main() Dim host As New ServiceHost(GetType(Service), New Uri("http://localhost:8000")) host.AddServiceEndpoint(GetType(IService), New BasicHttpBinding(), "Soap") Dim endpoint As ServiceEndpoint endpoint = host.AddServiceEndpoint(GetType(IService), New WebHttpBinding(), "Web") endpoint.Behaviors.Add(New WebHttpBehavior()) Try host.Open() Using wcf As New WebChannelFactory(Of IService)(New Uri("http://localhost:8000/Web")) Dim channel As IService = wcf.CreateChannel() Dim s As String Console.WriteLine("Calling EchoWithGet by HTTP GET: ") s = channel.EchoWithGet("Hello, world") Console.WriteLine(" Output: {0}", s) Console.WriteLine("") Console.WriteLine("This can also be accomplished by navigating to") Console.WriteLine("http://localhost:8000/Web/EchoWithGet?s=Hello, world!") Console.WriteLine("in a web browser while this sample is running.") Console.WriteLine("") Console.WriteLine("Calling EchoWithPost by HTTP POST: ") s = channel.EchoWithPost("Hello, world") Console.WriteLine(" Output: {0}", s) Console.WriteLine("") End Using Using scf As New ChannelFactory(Of IService)(New BasicHttpBinding(), "http://localhost:8000/Soap") Dim channel As IService = scf.CreateChannel() Dim s As String Console.WriteLine("Calling EchoWithGet on SOAP endpoint: ") s = channel.EchoWithGet("Hello, world") Console.WriteLine(" Output: {0}", s) Console.WriteLine("") Console.WriteLine("Calling EchoWithPost on SOAP endpoint: ") s = channel.EchoWithPost("Hello, world") Console.WriteLine(" Output: {0}", s) Console.WriteLine("") End Using Console.WriteLine("Press <Enter> to terminate") Console.ReadLine() host.Close() Catch cex As CommunicationException Console.WriteLine("An exception occurred: {0}", cex.Message) host.Abort() End Try End SubEnd Module

Compiling the code

When compiling Service.cs, reference System.ServiceModel.dll and System.ServiceModel.Web.dll.

See also

  • WebHttpBinding
  • WebGetAttribute
  • WebInvokeAttribute
  • WebServiceHost
  • ChannelFactory
  • WebHttpBehavior
  • WCF Web HTTP Programming Model
How to: Expose a Contract to SOAP and Web Clients - WCF (2024)
Top Articles
Things to Negotiate when Remodeling a Kitchen | MLM Inc. Custom Homes and Renovations
Law Insider - Chrome Web Store
Automated refuse, recycling for most residences; schedule announced | Lehigh Valley Press
St Thomas Usvi Craigslist
Bleak Faith: Forsaken – im Test (PS5)
Undergraduate Programs | Webster Vienna
Mail Healthcare Uiowa
Wal-Mart 140 Supercenter Products
Culos Grandes Ricos
The Rise of Breckie Hill: How She Became a Social Media Star | Entertainment
Sams Early Hours
Spartanburg County Detention Facility - Annex I
This Modern World Daily Kos
Les Schwab Product Code Lookup
Wilmot Science Training Program for Deaf High School Students Expands Across the U.S.
Wisconsin Women's Volleyball Team Leaked Pictures
Velocity. The Revolutionary Way to Measure in Scrum
Att.com/Myatt.
Providence Medical Group-West Hills Primary Care
If you have a Keurig, then try these hot cocoa options
Vernon Dursley To Harry Potter Nyt Crossword
Costco Gas Hours St Cloud Mn
Vivaciousveteran
Sherburne Refuge Bulldogs
Doctors of Optometry - Westchester Mall | Trusted Eye Doctors in White Plains, NY
Gen 50 Kjv
Table To Formula Calculator
Great ATV Riding Tips for Beginners
3 Ways to Drive Employee Engagement with Recognition Programs | UKG
His Only Son Showtimes Near Marquee Cinemas - Wakefield 12
Account Now Login In
Craigs List Tallahassee
Ripsi Terzian Instagram
Flaky Fish Meat Rdr2
Craigslist In Myrtle Beach
Texas Baseball Officially Releases 2023 Schedule
Family Fare Ad Allendale Mi
Domino's Delivery Pizza
CVS Near Me | Somersworth, NH
Babbychula
Levothyroxine Ati Template
Bianca Belair: Age, Husband, Height & More To Know
Casamba Mobile Login
All-New Webkinz FAQ | WKN: Webkinz Newz
Dragon Ball Super Super Hero 123Movies
All Weapon Perks and Status Effects - Conan Exiles | Game...
Bmp 202 Blue Round Pill
John Wick: Kapitel 4 (2023)
877-552-2666
What your eye doctor knows about your health
Chitterlings (Chitlins)
Naughty Natt Farting
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 6506

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.