我需要构建一个 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>
uj5u.com热心网友回复:
不知何故,我设法解决了它,我不知道这是否是最好的方法,但是我解决了我的问题,如下所示:
服务: 我洗掉了界面档案,添加了 MyType 类,该类具有可设定的物件“d”属性,然后我将其作为 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”,因为该链接对我帮助很大。
我希望它对你有用!

 
							 
										
										 
										
										 
										
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										
0 评论