WCF服务返回400错误的请求 [英] WCF Service returns 400 Bad Request

查看:181
本文介绍了WCF服务返回400错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了这个应用程序在本地工作和部署时间和使用的.mdf SQL防爆preSS数据库文件(我通常用于测试目的)。然而,当我将其更改为我们的SQL Server 2008的应用程序工程工作,但服务没有。

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). However, when I change it to work with our SQL Server 2008 the app works but the service doesn't.

例如,如果在一个页面中我的背后code我有一个按钮,将数据添加到一个表像这样的正常工作:

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();
    }       
}

我的web.config是设置为使用模拟该服务器上的所有工作得很好。然而,我的服务查询似乎并不返回任何东西,我得到一个400错误的请求错误。

My web.config is setup to use impersonation on that server and all works perfectly well. However, for my service the query doesn't seem to return anything and I get a 400 Bad Request error.

在code为jQuery是:

The code for the jquery is:

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

和的服务:

[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;
    }

而不是查询数据库。如果我有该服务只返回静态数据,然后它工作正常。 什么会引起我的服务不能够连接到数据库时,后面的应用作品code中的其他人呢?

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?

推荐答案

从托管WCF服务的数据库连接被认为是一个远程连接,因此请确保您连接字符串指定的身份验证方法。因此,尝试在连接字符串中使用集成安全性= SSPI,如果不工作,确保您的应用程序池的标识设置为具有SQL服务器上权限的域帐户。 :)

A database connection from a hosted WCF Service is considered a remote connection so make sure your connection strings specifies the authentication method. 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. :)

这篇关于WCF服务返回400错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆