20 Temmuz 2012 Cuma

Single WCF Service with SOAP & JSON Endpoints Tutorial

We want to write a single web service and to be able to access it both using SOAP and JSON. Also we need to generate a help page and publish WSDL. We don't want code duplication. Single service, simple configuration, and both access methods supported. How? Keep reading.

Step 1: Service Interface

Locate attributes (annotations of C#) properly in your service interface.

namespace CompositeService
    public interface IMyService
        [WebInvoke(UriTemplate = "login"BodyStyle = WebMessageBodyStyle.Wrapped)]
        LoginResponse Login(string username, string password);

        [WebInvoke(UriTemplate = "logout"BodyStyle = WebMessageBodyStyle.Wrapped)]
        ResponseEnum Logout(int userId);
    public class LoginResponse
        public ResponseEnum Durum { getset; }

        public int UserId { getset; }
    public enum ResponseEnum
        [EnumMember]Failed = 0,
        [EnumMember]Success = 1

Step 2: Implementation

Implement logic by yourself.

Step 3: Configuration - Important Part!!

    <!-- Define 2 endpoints (soap and json) -->
        <service name="CompositeService.MyService"
                 behaviorConfiguration="mySoapBehavior" >            <!-- Default service behavior is SOAP -->
            <endpoint address="soap"
                      contract="CompositeService.IMyService" />
            <endpoint address="json"
                      contract="CompositeService.IMyService" />  <!-- Json endpoint behavior is REST -->

    <!-- Define 2 behaviours: Default service behavior (SOAP) and REST endpoint behavior -->
            <behavior name="mySoapBehavior" >
                <serviceMetadata httpGetEnabled="true" />              <!-- Enable WSDL publishing -->
                <serviceDebug includeExceptionDetailInFaults="true" /> <!-- Enabled for debug purposes -->
            <behavior name="myJsonBehavior" >
                <webHttp defaultOutgoingResponseFormat="Json"
                         helpEnabled="true" />                         <!-- Enable REST help page -->


Access URL's

Congrats! If everything OK, you done in this point. Try following URL's:

SOAP Endpoint:
REST Endpoint:
REST Help Page:

1 yorum:

Adsız dedi ki...


WSO2 ESB + ClientAccesPolicy.xml + CrossDomain.xml

ClientAccesPolicy.xml and CrossDomain.xml files have to be found at root of your services Silverlight and Flex clients to be able to acce...