由于EndpointDispatcher上的AddressFilter不匹配,无法在接收器处理

时间:2013-01-19 09:37:23

标签: asp.net wcf c#-4.0 asp.net-ajax wcf-binding

我正在使用支持Ajax的WCF,当我在网络浏览器中打开网址时,我收到此错误。

  

由于以下原因,无法在接收方处理带有操作“http://localhost:22219/MobileService.svc/GetProductCategories”的消息   EndpointDispatcher上的ContractFilter不匹配。这可能是   因为合同不匹配(两者之间不匹配的行为)   发件人和收件人)或发件人之间的绑定/安全性不匹配   和接收器。检查发件人和收件人是否一样   合同和相同的约束(包括安全要求,例如   消息,传输,无)。

MobileService代码如下所示

namespace MobileService
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MobileService
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        public void DoWork()
        {
            // Add your operation implementation here
            return;
        }
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetProductCategories")]
        public List<String> GetProductCategories()
        {
            List<String> categoryList = new List<string>();

            categoryList.AddRange(new String[] { "Electronics", "Housewares", "Computers", "Software", "Music" });

            return categoryList;
        }
        // Add more operations here and mark them with [OperationContract]
    }
}

和 服务web.config文件是

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

    <behaviors>
      <endpointBehaviors>
        <behavior name="MobileService.MobileServiceAspNetAjaxBehavior">

        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="MobileService.MobileService">
        <endpoint address="" behaviorConfiguration="MobileService.MobileServiceAspNetAjaxBehavior"
          binding="webHttpBinding" contract="MobileService.MobileService"  />
      </service>
    </services>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

任何人都可以在我犯错的地方帮助我。

1 个答案:

答案 0 :(得分:14)

您的端点需要<webHttp/>端点行为。如果你添加它(见下文)它应该工作。

  <endpointBehaviors>
    <behavior name="MobileService.MobileServiceAspNetAjaxBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>