我需要構建一個 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”,因為該鏈接對我幫助很大。
我希望它對你有用!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/402856.html
標籤:
下一篇:ASP.NET裁剪影像不會裁剪它
