locked
Consume WCF from Javascript?

Answers

  • What I did is expose a JSON endpoint for my WCF service.

    The basicHttpBinding was my existing endpoint.  

     <system.serviceModel>
        <services>
          <service behaviorConfiguration="serviceBehavior" name="MyService">
            <endpoint address="" binding="basicHttpBinding" contract="IMyService"/>
            <endpoint address="json" binding="webHttpBinding" behaviorConfiguration="EndpointBehaviour" contract="IMyService"/>
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="EndpointBehaviour">
              <enableWebScript/>
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="serviceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
            <behavior name="">
              <serviceMetadata httpGetEnabled="false"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
      </system.serviceModel>

    You will also need to add the following attributes to the methods on your service:

      
    [WebGet(RequestFormat = WebMessageFormat.Json,             ResponseFormat = WebMessageFormat.Json)]
    public MyData GetMyData()
    {
      //Method implementation
    
    }

    Once that is in place, you can do simple get/put requests to your services.  For example, http://www.yourdomain.com/yourservice.svc/json/GetMyData

    I am using WinJS.xhr to get data from my service.

    I hope that helps.

    -David Paquette


    Wednesday, June 13, 2012 9:32 PM

All replies

  • You can use REST services and JSon.  Otherwise you could create a C# WinMD component and consume the service, and pass the data to WinJS.

    Jeff Sanders (MSFT)

    Wednesday, June 13, 2012 9:12 PM
    Moderator
  • What I did is expose a JSON endpoint for my WCF service.

    The basicHttpBinding was my existing endpoint.  

     <system.serviceModel>
        <services>
          <service behaviorConfiguration="serviceBehavior" name="MyService">
            <endpoint address="" binding="basicHttpBinding" contract="IMyService"/>
            <endpoint address="json" binding="webHttpBinding" behaviorConfiguration="EndpointBehaviour" contract="IMyService"/>
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="EndpointBehaviour">
              <enableWebScript/>
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="serviceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
            <behavior name="">
              <serviceMetadata httpGetEnabled="false"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
      </system.serviceModel>

    You will also need to add the following attributes to the methods on your service:

      
    [WebGet(RequestFormat = WebMessageFormat.Json,             ResponseFormat = WebMessageFormat.Json)]
    public MyData GetMyData()
    {
      //Method implementation
    
    }

    Once that is in place, you can do simple get/put requests to your services.  For example, http://www.yourdomain.com/yourservice.svc/json/GetMyData

    I am using WinJS.xhr to get data from my service.

    I hope that helps.

    -David Paquette


    Wednesday, June 13, 2012 9:32 PM