0

I need to build a wcf service, which uses a custom response, in jsonp format, I should get a response like this example:

calling this example method:

http://localhost:49350/Service1.svc/TestConnectionWCF_new?callback=jQuery22406768180963924506_1639677943363&_=1639677943368

I have to get this answer:

jQuery22406768180963924506_1639677943363( {"d":"<ACCESS><MSG><![CDATA[YES]]><\/MSG><\/ACCESS>"} );

I get this instead:

{"TestConnectionWCF_NEWResult":"<ACCESS><MSG><![CDATA[YES]]><\/MSG><\/ACCESS>"}

I have tried so many ways, but I have not gone beyond this, if anyone can kindly help me in any way, even with some advice to go beyond this.

I did a simple service test wcf, this is the code I am using

Interface:

<ServiceContract>
Public Interface IService1

    <OperationContract>
    <WebGet(ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped)>
    Function TestConnectionWCF_NEW() As Object

End Interface

The service:

Imports Test_Jsonp_Vb
Imports System.ServiceModel.Activation

<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Required)>
Public Class Service1
    Implements IService1
    Dim oReturnObject As Object

    Public Function TestConnectionWCF_NEW() As Object Implements IService1.TestConnectionWCF_NEW
        Try
            oReturnObject = "<ACCESS><MSG><![CDATA[YES]]></MSG></ACCESS>"
            Return oReturnObject
        Catch ex As Exception
            oReturnObject = ex.Message
            Return oReturnObject
        End Try
    End Function
End Class

Service Markup:

<%@ ServiceHost Language="VB" Debug="true" Service="Test_Jsonp_Vb.Service1" CodeBehind="Service1.svc.vb" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

WebConfig:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>
James Z
  • 12,209
  • 10
  • 24
  • 44
  • You can read these articles to find the solution.https://stackoverflow.com/questions/11090835/return-jsonp-in-proper-format-wcf && https://stackoverflow.com/questions/2067472/what-is-jsonp-and-why-was-it-created?rq=1 – Lan Huang Dec 23 '21 at 08:09
  • Hi, thanks for the link, I've seen and tried, but still can't get that answer there. – Ubaldo Formichetti Dec 27 '21 at 10:30

1 Answers1

0

somehow I managed to solve it, I don't know if this is the best way, however I solved my problem, like this:

The service: I removed the interface file, added the MyType class, which has a settable object "d" property, which I then return as a callback from the wcf service.

Imports System.ServiceModel.Activation
<ServiceContract()>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class Service
    Dim oReturnObject As Object
    Public Class MyType
        Public Property d As Object
    End Class
    <WebGet(ResponseFormat:=WebMessageFormat.Json)>
    Public Function TestConnectionWCF_new() As MyType
        Try
            oReturnObject = "<ACCESS><MSG><![CDATA[YES]]></MSG></ACCESS>"
        Catch ex As Exception
            oReturnObject = ex.Message
        End Try
        Return New MyType With {.d = oReturnObject}
    End Function
End Class

Service marking:

<%@ ServiceHost Language="VB" Debug="true" Service="Wcf_Jsonp2.Service" CodeBehind="Service.svc.vb" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>

Web Config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <!-- ...    -->
    <services>
      <service name="Jsonp">
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <!-- ...  -->
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!-- ...  -->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0"/>
    <!-- ...  -->
    <standardEndpoints>
      <webScriptEndpoint>
        <standardEndpoint crossDomainScriptAccessEnabled="true" name=""/>
      </webScriptEndpoint>
    </standardEndpoints>
    <!-- ...  -->
  </system.serviceModel>
    <!-- ...  -->
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

Now calling the service I get the answer like this, and that's exactly what I needed...

jQuery22406768180963924506_1639677943363({"__type":"Service.MyType:#Wcf_Jsonp2","d":"<ACCESS><MSG><![CDATA[YES]]><\/MSG><\/ACCESS>"});

Thanks to "James Z" because the link helped me a lot.

I hope it will be useful to you!