对于基于 ajax 的 WCF 服务,我遇到了上述错误。代码如下所示。
代码
<ServiceContract()>
Public Interface IEditInitiatives
<OperationContract()> _
<WebInvoke(BodyStyle:=WebMessageBodyStyle.WrappedRequest, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="GetGridData")> _
Function GetGridData(session As String) As List(Of InitiativeData)
<OperationContract()> _
<WebInvoke(Method:="POST", BodyStyle:=WebMessageBodyStyle.WrappedRequest, ResponseFormat:=WebMessageFormat.Json)> _
Function SaveGridData(input As String) As String
End Interface
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class EditInitiatives
Implements IEditInitiatives
Function GetGridData(session As String) As List(Of InitiativeData) Implements IEditInitiatives.GetGridData
'Business logice
End Function
Function SaveGridData(ByVal input As String) As String Implements IEditInitiatives.SaveGridData
Return "ok"
End Function
End Class
网络配置
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="MyCompany.CostReduction.EditInitiativesAspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="MyCompany.CostReduction.EditInitiatives" behaviorConfiguration="metadataBehavior">
<endpoint address="" behaviorConfiguration="MyCompany.CostReduction.EditInitiativesAspNetAjaxBehavior" binding="webHttpBinding" contract="MyCompany.CostReduction.IEditInitiatives" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="UsernameWithTransport">
<security mode="Transport">
<transport clientCredentialType="Basic" />
</security>
</binding>
</webHttpBinding>
</bindings>