简单的 WCF 服务抛出 404 not found 错误

Posted

技术标签:

【中文标题】简单的 WCF 服务抛出 404 not found 错误【英文标题】:Simple WCF service throwing 404 not found error 【发布时间】:2016-04-10 03:54:21 【问题描述】:

我只是想创建一个非常简单的 WCF 服务,它将与 javascript ajax 调用 (REST) 进行交互,但它对我不起作用,好像我迷路了:

服务接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Wcf.App

    [ServiceContract]
    public interface IService1
    
        [OperationContract]
        [WebGet(
            UriTemplate = "GetData/value", 
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        string getEmpData(string value);
    

实施:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Wcf.App

    public class Clocked
    
        public string date  get; set; 
        public string type  get; set; 
    

    public class DATA
    
        public string firstName  get; set; 
        public string lastName  get; set; 
        public string emailAddress  get; set; 
        public string phoneNum  get; set; 
        public string image  get; set; 
        public string title  get; set; 
        public List<Clocked> clocked  get; set; 
    

    public class RootObject
    
        public DATA DATA  get; set; 
    

    public class Service1 : IService1
    
        public string getEmpData(string test)
        
            try
            
                return " hello " + test;
                
                catch (Exception ex)
                
                    return "Can not open connection! " + ex.Message + "  Connection: " + connetionString;
                
            
            catch (SqlException e)
            
                return e.Message;
            
        
    

现在当我尝试使用 jQuery AJAX 调用此服务时,它会抛出 404 not found 错误。

JS:

 $.ajax(
                url: "Service1.svc/GetData/3046722425",
                type: "GET",
                dataType: "json",
                success: function (data) 
                    console.log(data);
                ,
                error: function (XMLHttpRequest, textStatus, errorThrown) 
                    alert(textStatus);
                
            );

配置:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
    <bindings>
      <customBinding>
        <binding name="basicConfig">
          <binaryMessageEncoding/>
          <httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864" />
        </binding>
      </customBinding>      
      <webHttpBinding>
        <binding name="basicConfig">
          <security mode="None">
            <transport clientCredentialType="Basic"/>
          </security>
        </binding>
        <binding>
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <client/>
    <services>
        <service name="Wcf.App.Service1" behaviorConfiguration="ServiceBehaviour">
          <!--<endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="Wcf.App.IService1"/>-->
          <endpoint address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="Wcf.App.IService1"/>
        </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
        <behavior name="REST">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="ServiceBehaviour">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/>
        </behavior>
        <behavior name="REST">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/>
        </behavior>
      </endpointBehaviors>      
    </behaviors>
  </system.serviceModel>

【问题讨论】:

你能不能在浏览器中点击网址,看看你是否得到了肯定的回应:? 不,我尝试点击此 URL http://localhost/WCFServices/Service1.svc/GetData/3046722425 但它不起作用。 返回 404 【参考方案1】:

请确保您已从 here 激活 WCF 组件。

HTTP Error 404.3 - Not Found" while browsing wcf service on Windows Server 2008(64bit)

【讨论】:

【参考方案2】:

尝试从浏览器访问服务一次(丢弃方法名 Getdata 和参数值)或将其添加到 WCFTestClient 应用程序。

【讨论】:

是的,我正在获取元数据,但它有什么帮助?这不能作为答案。

以上是关于简单的 WCF 服务抛出 404 not found 错误的主要内容,如果未能解决你的问题,请参考以下文章

在 Windows Server 2008(64 位)上浏览 wcf 服务时出现 HTTP 错误 404.3 - Not Found”

IIS8中部署WCF服务出错:HTTP 错误 404.3 - Not Found

在IIS上抛出异常的WCF托管(没有权限和HTTP错误404.3 - 未找到)

iis 网页HTTP 错误 404.3 - Not Found解决方案

Smack API 抛出 item-not-found(404) 异常

HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本 ,请添加处理程序。如果下载文件,请添加 MIME 映射。 IIS站点中添加WCF项目后浏览