WCF Restful Web 服务端点公开但方法所有 return 未找到 http 404 c#

WCF Restful Web Service Endpoint exposed but methods all return http 404 not found c#

我已经编写了我的第一个 Web 服务,当我在我的本地开发机器上从 visual studio 测试 运行 它时,一切都按预期工作。我去客户端部署服务,发现部署后我可以到达端点,但我的所有方法 return HTTP 404 未找到。

Web 服务是使用 WCF 在 Visual Studio 中编写的,并设置为 return Json。 Web 服务的目标是 .Net framework 4.5。该站点配置为使用 HTTPS 协议并具有有效的 SSL 证书。我已经使用最新版本的 .Net framework 4.5 更新了服务器,并相应地向站点应用了一个应用程序池。

当我从外部浏览器转到客户端服务器上的 Endpoint 位置时(我修改了屏幕截图并 link 删除了真实域):

link 看起来像这样:

https://www.somedomain.co.uk/WorksWebService/WorksWebService.svc

我获得了向 WSDL 和 SingleWSDL 页面显示 Web 服务 link 的页面,它们正确显示了端点方法和各种其他配置信息。

在我看来,一切都如预期的那样,我有点不确定下一步该去哪里寻找问题。

现在我很不确定 Web.config 文件的要求是什么。下面是 Web 服务的当前 Web.config 文件。可能是我在这里遗漏了有关服务部署的重要信息,但我想知道为什么服务 运行 会在 Visual Studio 中成功运行?唯一需要注意的区别是 visual studio 我 运行 使用 http 而不是 https 的服务。

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <connectionStrings>
    <add name="BAXISQL"
         connectionString="Database=SomeDatabase;Server=SomeServer;User ID=user;Password=password"
         providerName="System.Data.SqlClient"
    />
  </connectionStrings>
  <system.serviceModel>
    <services>
      <service name="WorksWebService.WorksWebService">
        <endpoint address="" behaviorConfiguration="restful" binding="webHttpBinding"
          contract="WorksWebService.IWorksWebService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restful">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <startup>
    <supportedRuntime version="4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

我想我的问题来自 Web.config 文件或我的服务接口和方法声明中缺少一些元数据。

以下代码显示了我的接口(修改后仅包含一个方法):

using System.IO;
using System.ServiceModel;
using System.Web.Services;

namespace WorksWebService
{
    [ServiceContract]
    interface IWorksWebService
    {
        [OperationContract]
        [WebMethod]
        string GetTrainingStatus(string urn, string pastdays, string futuredays);   
    }
}

下面是WebService.svc.cs文件中相应的方法调用(也修改为仅显示单一方法):

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

namespace WorksWebService
{
    public class WorksWebService: IWorksWebService
    {
        // key that all methods must receive in the header to validate that the request is from a valid source
        private string baxiSMSKey = "C5A75B32-5BC9-4D89-AB78-F8FE0CF58806";
        //BAXI_SMS_KEY: C5A75B32-5BC9-4D89-AB78-F8FE0CF58806

        #region TrainingStatus

        // test url string
        // WorksWebService.svc/rest/member/C211292/trainingstatus?country=GB
        /// <summary>
        /// Method to find the training status dates for a specific Customer
        /// </summary>
        /// <param name="urn"></param>
        /// <param name="country"></param>
        /// <returns>Last valid training status date or future date the customer will be attending training</returns>
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json
            , UriTemplate = "rest/member/{urn}/trainingstatus?pastdays={pastdays}&futuredays={futuredays}")]
        public string GetTrainingStatus(string urn, string pastdays, string futuredays)
        {
            string dateString = string.Empty;
            WebOperationContext context = WebOperationContext.Current;
            // check the headers for the BAXI_SMS_KEY
            if (CheckAPIKey())
            {
                // validate the url parameters
                if (ValidateURLStringParameters_GetTrainingStatus(urn, pastdays, futuredays))
                {
                    // find the training status
                    dateString = GetTrainingDate(urn, pastdays, futuredays);

                    if (dateString.Contains("Error: "))
                        context.OutgoingResponse.StatusCode = HttpStatusCode.InternalServerError; // HTTP Code 500
                    else if (dateString.Equals(string.Empty))
                        context.OutgoingResponse.StatusCode = HttpStatusCode.NotFound; // HTTP Code 404
                    else
                        context.OutgoingResponse.StatusCode = HttpStatusCode.OK; // HTTP Code 200
                }
                else
                    context.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest; // HTTP Code 400
            }
            else
                context.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden; // HTTP Code 403

            return dateString;
        }

        /// <summary>
        /// method to validate the parameters for GetTrainingStatus Web method
        /// </summary>
        /// <param name="urn"></param>
        /// <param name="pastdays"></param>
        /// <param name="futuredays"></param>
        /// <returns>True or False</returns>
        private bool ValidateURLStringParameters_GetTrainingStatus(string urn, string pastdays, string futuredays)
        {
            if (string.IsNullOrWhiteSpace(urn))
                return false;

            if (string.IsNullOrWhiteSpace(pastdays))
                return false;

            if (string.IsNullOrWhiteSpace(futuredays))
                return false;

            try
            {
                Convert.ToInt32(pastdays);
                Convert.ToInt32(futuredays);
            }
            catch (Exception e)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Method to find the training status of the member
        /// </summary>
        /// <param name="urn"></param>
        /// <returns>the date of the users training past of future</returns>
        private string GetTrainingDate(string urn, string pastdays, string futuredays)
        {
            string dateSt = string.Empty;
            try
            {
                // first search against customers
                var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["BAXISQL"].ConnectionString);
                connection.Open();
                var sqlCommand = connection.CreateCommand();
                sqlCommand.CommandText = "SELECT dbo.WORKS_GetTrainingDate_fn(@URN, @Past, @Future)";
                sqlCommand.Parameters.AddWithValue("@URN", urn);
                sqlCommand.Parameters.AddWithValue("@Past", pastdays);
                sqlCommand.Parameters.AddWithValue("@Future", futuredays);

                string date = sqlCommand.ExecuteScalar().ToString();
                connection.Close();

                if (!string.IsNullOrWhiteSpace(date))
                {                    
                    string[] parts = date.ToString().Split('/');
                    dateSt = parts[2] + parts[1] + parts[0];
                }
            }
            catch (Exception e)
            {
                dateSt = "Error: " + e.Message + "<br />Source: " + e.Source + "<br />Stacktrace: " + e.StackTrace;
            }

            return dateSt;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Method to determine if the API key has been passed in succesfully
        /// </summary>
        /// <returns>true or false</returns>
        private bool CheckAPIKey()
        {
            bool matchedKey = false;
            IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest;
            WebHeaderCollection headers = request.Headers;

            System.Diagnostics.Debug.Write("\r\n-------------------------------------------------------");
            System.Diagnostics.Debug.Write("\r\n" + request.Method + " " + request.UriTemplateMatch.RequestUri.AbsolutePath);
            foreach (string headerName in headers.AllKeys)
            {
                System.Diagnostics.Debug.Write("\r\n" + headerName + ": " + headers[headerName]);
                if (headerName.Equals("BAXI_SMS_KEY"))
                {
                    if (baxiSMSKey.ToString().ToUpper().Equals(headers[headerName]))
                    {
                        matchedKey = true;
                    }
                }
            }
            System.Diagnostics.Debug.Write("\r\n-------------------------------------------------------");

            return matchedKey;
        }

        #endregion

    }
}

这些 404 消息是在我尝试使用 Fiddler 和浏览器访问服务方法时出现的。我想证明这些方法在允许正在构建将充当 Web 服务客户端的 Web 门户的第 3 方进行访问之前能够正常工作。

如有任何意见或建议,我们将不胜感激。

在此先感谢伊恩

经过一番搜索和与几位同事的讨论后,我找到了解决方案。我怀疑我的 web.config 文件中缺少一些部分。

我没有为 http 和 https 添加端点声明。

<services>
  <service name="WorksWebService.WorksWebService">
    <endpoint address="" behaviorConfiguration="restful" binding="webHttpBinding" bindingConfiguration="HttpBinding"
      contract="WorksWebService.IWorksWebService" />
    <endpoint address="" behaviorConfiguration="restful" binding="webHttpBinding" bindingConfiguration="HttpsBinding"
      contract="WorksWebService.IWorksWebService" />
  </service>
</services>

我还错过了定义安全性和所需凭据类型所需的相关绑定部分。

<bindings>
  <webHttpBinding>
    <binding name="HttpBinding">
      <security mode="None">
        <transport clientCredentialType="None"/>
      </security>
    </binding>
    <binding name="HttpsBinding">
      <security mode="Transport">
        <transport clientCredentialType="None"/>
      </security>
    </binding>
  </webHttpBinding>
</bindings>

所以现在我的 web.config 文件完整如下所示:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <connectionStrings>
    <add name="BAXISQL" connectionString="Database=SomeDatabase;Server=SomeServer;User ID=User;Password=Password" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.serviceModel>
    <services>
      <service name="WorksWebService.WorksWebService">
        <endpoint address="" behaviorConfiguration="restful" binding="webHttpBinding" bindingConfiguration="HttpBinding"
          contract="WorksWebService.IWorksWebService" />
        <endpoint address="" behaviorConfiguration="restful" binding="webHttpBinding" bindingConfiguration="HttpsBinding"
          contract="WorksWebService.IWorksWebService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restful">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <bindings>
      <webHttpBinding>
        <binding name="HttpBinding">
          <security mode="None">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
        <binding name="HttpsBinding">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <startup>
    <supportedRuntime version="4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

我希望这对遇到类似问题的其他人有所帮助。

此致,

漫画编码器