简体   繁体   English

WCF服务返回400错误请求

[英]WCF Service returns 400 Bad Request

I've got this application that works locally and when deployed and using a .mdf SQL Express database file (which I usually use for testing purposes). 我有这个应用程序,它可以在本地运行,并且在部署时可以使用.mdf SQL Express数据库文件(通常用于测试目的)。 However, when I change it to work with our SQL Server 2008 the app works but the service doesn't. 但是,当我将其更改为可与我们的SQL Server 2008一起使用时,该应用程序可以运行,但该服务无法运行。

For example if in my code behind of a page I have a button that adds data to a table such as this it works fine: 例如,如果在页面后面的代码中,我有一个按钮可以向这样的表中添加数据,则它可以正常工作:

public static string connString = @"Data Source=server1;Initial Catalog=Project;Integrated Security=True";

protected void btnAddProj_Click(object sender, EventArgs e)
{
    using (var sqlc = new SqlConnection(connString))
    {
        sqlc.Open();
        var cmd = sqlc.CreateCommand();
        int intProjectID;

        // Add the project info to the database
        cmd.CommandText = "INSERT INTO tblProject VALUES(@ProjName,@ProjTeam,@ProjStart,@ProjEnd)";
        cmd.Parameters.Add("ProjName", System.Data.SqlDbType.NVarChar).Value = txtProjName.Text;
        cmd.Parameters.Add("ProjTeam", System.Data.SqlDbType.Int).Value = ddlTeamSupported.SelectedValue;
        cmd.Parameters.Add("ProjStart", System.Data.SqlDbType.NVarChar).Value = txtStartDate.Text;
        cmd.Parameters.Add("ProjEnd", System.Data.SqlDbType.NVarChar).Value = txtEndDate.Text;
        cmd.ExecuteNonQuery();
    }       
}

My web.config is setup to use impersonation on that server and all works perfectly well. 我的web.config设置为在该服务器上使用模拟,并且一切运行良好。 However, for my service the query doesn't seem to return anything and I get a 400 Bad Request error. 但是,对于我的服务,查询似乎未返回任何内容,并且收到400 Bad Request错误。

The code for the jquery is: jQuery的代码是:

$.ajax({
    type: "POST",
    async: false,
    url: "Services/ProjectService.svc/test",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        console.log(data);
        }
    });

And for the Service: 对于服务:

[ServiceContract]
public interface IProjectService
{
    [OperationContract]
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    ArrayList test();
}


    public static string connString = @"Data Source=server1;Initial Catalog=Project;Integrated Security=True";

    public ArrayList test()
    {
        var sqlc = new SqlConnection(connString);
        sqlc.Open();
        var cmd = sqlc.CreateCommand();
        cmd.CommandText = "SELECT ProjectID FROM tblProject";
        var reader = cmd.ExecuteReader();

        ArrayList temparray = new ArrayList();


        while (reader.Read())
        {
            temparray.Add(reader[0]);
        }
        sqlc.Close();
        return temparray;
    }

If instead of querying the database I have the service just return static data then it works fine. 如果不是查询数据库而是向我提供服务,而只是返回静态数据,那么它将正常工作。 What could cause my service not to be able to connect to the database when the rest of the code behind for the app works? 当该应用程序后面的其余代码正常工作时,是什么导致我的服务无法连接到数据库?

A database connection from a hosted WCF Service is considered a remote connection so make sure your connection strings specifies the authentication method. 来自托管WCF服务的数据库连接被视为远程连接,因此请确保您的连接字符串指定了身份验证方法。 So try using Integrated Security=SSPI in your connection string and if that doesn't work make sure that your Application Pool's Identity is set to a domain account that has permissions on the SQL server. 因此,请尝试在连接字符串中使用Integrated Security = SSPI,如果该操作不起作用,请确保将您的应用程序池的标识设置为对SQL Server具有权限的域帐户。 :) :)

You're probably not setting HTTP headers exactly as the service is expecting them, or in the order it's expecting them. 您可能未完全按照服务期望的HTTP标头或期望的顺序设置HTTP标头。

Here's how I'd debug it: 这是我的调试方法:

  1. Create a quick test client using SvcUtil .exe. 使用SvcUtil .exe创建一个快速测试客户端。
  2. Install Fiddler , turn it on. 安装Fiddler并将其打开。
  3. Use the thin client to make a call to the service operation. 使用瘦客户机来调用服务操作。
  4. Use your web application to make a call to the same service operation 使用您的Web应用程序来调用相同的服务操作
  5. Look at the full HTTP request in Fiddler for each request. 查看Fiddler中每个请求的完整HTTP请求。 Compare them. 比较它们。 Figure out where your AJAX call is different, and resolve the differences so it matches what the generated client uses. 找出您的AJAX调用的不同之处,并解决差异,使其与生成的客户端使用的匹配。

This could be that you are making a POST but the service is expecting a GET. 这可能是因为您正在进行POST,但服务正在期待GET。

Try specifying that the method should be a POST: 尝试指定该方法应为POST:

[WebInvoke(Method = "POST", UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]

The other this that it could be is that you are using a trusted connection. 另一个可能是您使用的是受信任的连接。 This means that the security context is the identity of the application pool (if defaults are used). 这意味着安全上下文是应用程序池的标识(如果使用默认值)。 Then you will be making a connection to the database using a local account that does not have access on a database on a different machine. 然后,您将使用没有其他计算机上数据库访问权限的本地帐户建立与数据库的连接。

The strange thing is that based on the error message it should be the first explaination, but based on your description it would be the second. 奇怪的是,基于错误消息,它应该是第一个解释,但是根据您的描述,它应该是第二个解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM