Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, 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><\/MSG><\/ACCESS>"} );

I get this instead:
{"TestConnectionWCF_NEWResult":"<access><msg><\/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 ...

What I have tried:

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


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>



Any help idea is welcome, i am stuck here, thanks bro.!
Posted
Updated 2-Jan-22 3:23am

1 solution

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

VB.NET
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:

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


Web Config:

XML
<?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>"});


I hope it will be useful to you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900