从ASMX-webservice C#

时间:2015-05-06 11:41:26

标签: c# web-services model-view-controller asmx

我一直致力于一个小型MVC项目,因为我转换为更多的MVC方法已停止工作。

使用Web服务的程序是一个简单的控制台应用程序,它有自己的控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication.ServiceReference1;

namespace ConsoleApplication
{
class ConsoleController
{
    ServiceReference1.WebService2SoapClient webservice = new ServiceReference1.WebService2SoapClient();

    public List<Employee> GetEmployees()
    {
        return webservice.GetEmployees().ToList();
    }
   }
}

Web服务由Employee-class组成:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace webservice
{
public class Employee
{

    public string _socialSN { get; set; }
    public string _lastName { get; set; }
    public string _firstName { get; set; }

    public Employee(string socialSN, string firstName, string lastName)
    {
        this._firstName = firstName;
        this._lastName = lastName;
        this._socialSN = socialSN;
    }
}

}

数据访问层:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace webservice
{
public class DAL
{
    private SqlConnection con;
    private SqlCommand cmd;

    public void GetConnection()
    {
        con = new SqlConnection("Data Source=****;Initial Catalog=DB2015;Persist Security Info=True;User ID=****;Password=***********");
    }

    public List<Employee> GetEmployees()
    {
        GetConnection();
        con.Open();

        cmd = con.CreateCommand();
        cmd.CommandText = "SELECT SocialSN, Name, Surname FROM Employee";

        SqlDataReader reader = cmd.ExecuteReader();

        List<Employee> employeeList = new List<Employee>();
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                employeeList.Add(new Employee(reader.GetString(0), reader.GetString(1), reader.GetString(2)));
            }
        }
        else
        {
            employeeList = null;
        }

        reader.Close();
        con.Close();

        reader.Dispose();
        con.Dispose();

        return employeeList;
    }
  }
}

BackController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace webservice
{
class BackController
{
    DAL dal = new DAL();

    public List<Employee> GetEmployees()
    {
        return dal.GetEmployees();
    }
  }
}

最后是webservice-code本身:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

namespace webservice
{
/// <summary>
/// Summary description for WebService2
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService2 : System.Web.Services.WebService
{
    BackController bcontroller = new BackController();

    [WebMethod]
    public List<Employee> GetEmployees()
    {
        return bcontroller.GetEmployees();
    }
  }
}

每当我尝试向控制台应用程序添加新的服务引用时,我都会收到此错误:

错误第1部分http://i.imgur.com/UVw1cAO.png 错误第2部分http://i.imgur.com/N0dF159.png

我无法弄清楚出了什么问题。在我改为MVC之前它运行良好。我已经尝试清理项目,重建它们并再次从头开始创建它们,只是简单地工作了。

如果您有任何想法可能出错或者您需要其他信息,请与我们联系。

谢谢!

1 个答案:

答案 0 :(得分:0)

听起来你已经升级了一些组件,但代码仍然试图锁定旧版本。基本上你的代码中有些东西正在寻找说1.1.0.1版,你已经有2.1.0.1版。或类似的东西。您需要在Web配置中重定向到正确的程序集,或者找到另一种方法让您的应用程序接受更新的版本。

以下是一些链接,您可以查看一些提示,但这些可能是难以捉摸的错误。

来自stackoverflow:

The located assembly's manifest definition does not match the assembly reference

该链接中包含此链接:

http://blogs.msdn.com/b/junfeng/archive/2004/03/25/95826.aspx

这是一位独立博主对此的看法:

http://www.codingdefined.com/2014/10/error-located-assemblys-manifest.html