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
{
    [ServiceContract]
    public interface IMyService
    {
        [WebInvoke(UriTemplate = "login"BodyStyle = WebMessageBodyStyle.Wrapped)]
        [OperationContract]
        LoginResponse Login(string username, string password);

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

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

Step 2: Implementation

Implement logic by yourself.

Step 3: Configuration - Important Part!!

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

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

</system.serviceModel>

Access URL's

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

SOAP Endpoint:
SOAP WSDL:
REST Endpoint:
REST Help Page:




1 yorum:

Adsız dedi ki...

teşekkürler

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...