Wcf jsonp ajax jquery 回调 - 如何自定义响应、回调函数的名称和包装器?

Wcf jsonp ajax jquery callback - how to customize the response, the name of the callback function, and the wrapper?

我需要构建一个 wcf 服务,它使用自定义响应,采用 jsonp 格式,我应该得到如下示例的响应:

调用此示例方法:

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

我必须得到这个答案:

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

我得到这个:

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

我已经尝试了很多方法,但我还没有超越这个,如果有人能以任何方式帮助我,即使有一些建议可以超越这个。

我做了一个简单的服务测试wcf,这是我正在使用的代码

接口:

<ServiceContract>
Public Interface IService1

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

End Interface

服务:

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

服务标记:

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

网络配置:

<?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>

我设法解决了它,我不知道这是否是最好的方法,但是我解决了我的问题,如下所示:

服务: 我删除了接口文件,添加了 MyType class,它有一个可设置的对象“d”属性,然后我 return 作为来自 wcf 服务的回调。

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

服务标记:

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

网络配置:

<?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>

现在调用服务我得到这样的答案,这正是我需要的...

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

感谢“James Z”,因为 link 对我帮助很大。

I hope it will be useful to you!