WCF Restful Web Service端点已公开,但方法都返回未找到的http 404 c#

时间:2015-08-07 11:25:16

标签: c# asp.net web-services wcf https

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

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

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

链接如下所示:

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

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

Works Web Service Endpoint

在我看来,一切都如预期的那样,并且我不确定在哪里寻找问题。

现在我不确定Web.config文件的要求是什么。下面是Web Service的当前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文件或来自我的服务接口和方法声明中的一些缺少的Meta数据。

以下代码显示了我的界面(仅修改为包含单个方法):

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

    }
}

当我尝试使用Fiddler和浏览器访问Service Mthods时,会发生这404条消息。在允许构建Web Portal的第三方作为Web服务的客户端进行访问之前,我想证明这些方法是否正常工作。

非常感谢任何意见或建议。

提前感谢Iain

1 个答案:

答案 0 :(得分:2)

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

我没有为http和https包含端点语句。

<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>

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

<?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>

所以现在我的web.config文件完整地看起来像这样:

$(function(){
    $('#iframe').load(function(){

        var iframe = $('#iframe').contents();

        iframe.find("#btnSubmit").click(function(){
               alert("test");
        });
});

});

我希望这有助于其他有类似问题的人。

此致

Comic Coder

相关问题