Answered WCF Service for HTML5 and Silverlight

  • Friday, May 04, 2012 11:40 PM
     
     

    I have a WCF service that is working with Silverlight. I want to convert this service to WCF REST so that it works with HTML5.
    Is there a way to have two bindings for this service so that it can work with both Silverlight and HTML5? Or Do I need to have two separate services for this purpose?

    Thanks.

All Replies

  • Saturday, May 05, 2012 3:10 PM
    Moderator
     
     Answered Has Code

    You can definitely have two endpoints for this service. In addition to the one you already have, you can add another endpoint for the same contract, using the webHttpBinding / webHttpBehavior. You can even add the [WebGet] / [WebInvoke] attributes in addition to the the [OperationContract] in the operations of your service - they will be ignored by the "normal" endpoint, and used by the REST endpoint. The config below shows one example of how to add another endpoint to the one added by the "Silverlight-Enabled WCF Service" item template.

      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="Web">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <bindings>
          <customBinding>
            <binding name="SLApp.Web.Service1.customBinding0">
              <binaryMessageEncoding />
              <httpTransport />
            </binding>
          </customBinding>
        </bindings>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
        <services>
          <service name="SLApp.Web.Service1">
            <endpoint address="" 
                      binding="customBinding"
                      bindingConfiguration="SLApp.Web.Service1.customBinding0"
                      contract="SLApp.Web.Service1" />
            <endpoint address="mex" 
                      binding="mexHttpBinding" 
                      contract="IMetadataExchange" />
            <endpoint address="rest"
                      binding="webHttpBinding"
                      contract="SLApp.Web.Service1"
                      behaviorConfiguration="Web" />
          </service>
        </services>
      </system.serviceModel>
    


    Carlos Figueira

    • Proposed As Answer by Dragan Radovac Sunday, May 06, 2012 2:56 PM
    • Marked As Answer by codematrix Wednesday, May 09, 2012 12:40 AM
    • Unmarked As Answer by codematrix Wednesday, May 09, 2012 12:41 AM
    • Marked As Answer by codematrix Wednesday, May 09, 2012 8:55 PM
    •  
  • Wednesday, May 09, 2012 12:14 AM
     
     
    Carlos,

    Thanks. My service now has two endpoints and my Silverlight client application works perfectly without any issues.
    When I open my service in the IE, I am getting HTTP 400 bad request. It looks like REST part of my service has some issue.
    Appreciate any help!

    • Edited by codematrix Wednesday, May 09, 2012 12:39 AM
    •  
  • Wednesday, May 09, 2012 1:54 PM
    Moderator
     
     
    You'll need to enable tracing at the server to find out why the server is rejecting the REST request. The traces should have an exception indicating the reason.

    Carlos Figueira


  • Wednesday, May 09, 2012 8:54 PM
     
     

    Thanks, the log file is created. The file has an exception and here is the message. 'There is a problem with the XML that was received from the network. See inner exception for more details'.  Here is the stack trace.

    System.ServiceModel.Channels.HttpRequestContext.CreateMessage()
    System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
    System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
    System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
    System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
    System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
    System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state)
    System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequestWithFlow(Object state)
    System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
    System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
    System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

    Please let me know what to look for.

  • Wednesday, May 09, 2012 8:56 PM
    Moderator
     
     
    Can you post the contract and the request which is being sent?

    Carlos Figueira

  • Wednesday, May 09, 2012 9:00 PM
     
     

    Here is the request.

    http://localhost:1706/WCF/Authentication/AuthenticationService.svc

  • Wednesday, May 09, 2012 9:02 PM
    Moderator
     
     
    And the contract? Also, the endpoint address you specified for the rest endpoint in web.config is "rest", so the endpoint address should start at .../WCF/Authentication/AuthenticationService.svc/rest.

    Carlos Figueira

  • Wednesday, May 09, 2012 9:04 PM
     
     

    That's it. I missed "rest". This fixed the issue.