Search code examples
c#.netwcfjsonpwcf-rest

how to return JsonP from a WCP request?


I want to build a WCF service that will return a JsonP request. I want it to return the following,

jsonpCallback({"fileNames":"IDR023.T.201705201412.png, IDR023.T.201705201418.png"});

So I created the following WCF Service

IBOM.cs

  [ServiceContract]
    public interface IBOM
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json)]
        string GetData();
    }

BOM.cs

 public class BOM : IBOM
    {

        public string GetData()
        {
            return "jsonpCallback({\"fileNames\":\"IDR023.T.201705201412.png, IDR023.T.201705201418.png\"});";


        }
    }

Webconfig

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="BomService.BOM" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="webHttpBinding" contract="BomService.IBOM" behaviorConfiguration="web">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

But I am getting back

"jsonpCallback({\"fileNames\":\"IDR023.T.201705201412.png, IDR023.T.201705201418.png\"});"

I am returning a string and trying to set it up to return what I need. Is this the best way for JSONP? or is there a better way to achieve what I need

Thanks


Solution

  • For anyone who is interested, I worked it out. Here is the code.

    IBOM

    [ServiceContract] public interface IBOM {

    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json)]
    Stream GetData();
    

    }

    BOM

    public class BOM : IBOM
        {
          public Stream GetData()
            {
    
    
                string jsCode = "jsonpCallback" + "({\Test:\"" + fileNames +   "\"});";
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/javascript";
                return new MemoryStream(Encoding.UTF8.GetBytes(jsCode));
    
            }