如何将现有数据访问类转换为Web服务

时间:2013-02-03 19:16:55

标签: c# asp.net web-services

我有一个数据访问类,它使用某些存储过程来选择/插入/更新数据库中的数据。如何将此类转换为Web服务和所有方法作为Web方法?我正在使用vs 2012和c#。此数据集课的部分示例是

namespace HPS.Thesaurus.Data
{
public class TermDB : DbObjectStatic
{
    #region Public Methods

    #region CRUD Methods                

    public static int Add(Term term, string useString, string useForString, string broaderTermString, string narrowerTermString, string relatedTermString, string userName, ref int historyTermId)
    {
        int rowsAffected = 0;
        SqlParameter[] parameter = 
        { 
            new SqlParameter("@name", SqlDbType.VarChar, 200),
            new SqlParameter("@type", SqlDbType.Int),
            new SqlParameter("@scope", SqlDbType.VarChar, 2000),
            new SqlParameter("@hpspublic", SqlDbType.Bit),
            new SqlParameter("@additionalnotes",SqlDbType.VarChar,2000),
            new SqlParameter("@usestring", SqlDbType.VarChar, 2000),
            new SqlParameter("@useforstring", SqlDbType.VarChar, 2000),
            new SqlParameter("@broaderstring", SqlDbType.VarChar, 2000),
            new SqlParameter("@narrowerstring", SqlDbType.VarChar, 2000),
            new SqlParameter("@relatedstring", SqlDbType.VarChar, 2000),
            new SqlParameter("@changedby", SqlDbType.VarChar, 15),
            new SqlParameter("@id", SqlDbType.Int),
            new SqlParameter("@historytermid", SqlDbType.Int)
        };

        parameter[0].Value = term.Name;
        parameter[1].Value = term.Type;
        parameter[2].Value = term.Scope;
        parameter[3].Value = term.HPSPublic;
        parameter[4].Value = term.AdditionalNotes;

        if (useString.Trim().Length > 0)
        {
            parameter[5].Value = useString;
        }

        if (useForString.Trim().Length > 0)
        {
            parameter[6].Value = useForString;
        }

        if (broaderTermString.Trim().Length > 0)
        {
            parameter[7].Value = broaderTermString;
        }

        if (narrowerTermString.Trim().Length > 0)
        {
            parameter[8].Value = narrowerTermString;
        }

        if (relatedTermString.Trim().Length > 0)
        {
            parameter[9].Value = relatedTermString;
        }

        if (userName.Trim().Length > 0)
        {
            parameter[10].Value = userName;
        }

        parameter[11].Direction = ParameterDirection.Output;
        parameter[12].Direction = ParameterDirection.Output;

        try
        {
            RunProcedure("Term_Add", parameter, out rowsAffected, AppConfiguration.ConnectionString());
        }
        catch
        {
            return -1;
        }

        historyTermId = (int)parameter[12].Value;
        return (int)parameter[11].Value;
    }

    public static bool Delete(Term term)
    {
        int rowsAffected = 0;

        SqlParameter[] parameter = 
        { 
            new SqlParameter("@id", SqlDbType.Int) 
        };

        parameter[0].Value = term.Id;

        try
        {
            RunProcedure("Term_Delete", parameter, out rowsAffected, AppConfiguration.ConnectionString());
        }
        catch
        {
            return false;
        }

        return rowsAffected > 0;
    }

    public static Term GetTerm(int termId)
    {
        SqlParameter[] parameters = 
        { 
            new SqlParameter("@id", SqlDbType.Int)
        };

        parameters[0].Value = termId;

        using (DataTable dt = RunProcedure("Term_Get", parameters, "terms", AppConfiguration.ConnectionString()))
        {
            if (dt.Rows.Count > 0)
            {
                return FillData(dt.Rows[0]);
            }
            else
            {
                return null;
            }
        }
    }

    public static Term GetTerm(string termName)
    {
        SqlParameter[] parameters = 
        { 
            new SqlParameter("@name", SqlDbType.VarChar, 1000)
        };

        parameters[0].Value = termName;

        using (DataTable dt = RunProcedure("Term_GetByName", parameters, "terms", AppConfiguration.ConnectionString()))
        {
            if (dt.Rows.Count > 0)
            {
                return FillData(dt.Rows[0]);
            }
            else
            {
                return null;
            }
        }
    }

    public static bool Update(Term term, string useString, string useForString, string broaderTermString, string narrowerTermString, string relatedTermString, string changedBy, ref int historyTermId)
    {
        int rowsAffected = 0;
        SqlParameter[] parameter = 
        {   
            new SqlParameter("@id", SqlDbType.Int),
            new SqlParameter("@name", SqlDbType.VarChar, 200),
            new SqlParameter("@type", SqlDbType.Int),
            new SqlParameter("@scope", SqlDbType.VarChar, 2000),
            new SqlParameter("@hpspublic", SqlDbType.Bit),
            new SqlParameter("@additionalnotes",SqlDbType.VarChar,2000),
            new SqlParameter("@usestring", SqlDbType.VarChar, 2000),
            new SqlParameter("@useforstring", SqlDbType.VarChar, 2000),
            new SqlParameter("@broaderstring", SqlDbType.VarChar, 2000),
            new SqlParameter("@narrowerstring", SqlDbType.VarChar, 2000),
            new SqlParameter("@relatedstring", SqlDbType.VarChar, 2000),
            new SqlParameter("@changedBy", SqlDbType.VarChar, 15),
            new SqlParameter("@historyTermId", SqlDbType.Int),      
        };

        parameter[0].Value = term.Id;
        parameter[1].Value = term.Name;
        parameter[2].Value = term.Type;
        parameter[3].Value = term.Scope;
        parameter[4].Value = term.HPSPublic;
        parameter[5].Value = term.AdditionalNotes;
        if (useString.Trim().Length > 0)
        {
            parameter[6].Value = useString;
        }

        if (useForString.Trim().Length > 0)
        {
            parameter[7].Value = useForString;
        }

        if (broaderTermString.Trim().Length > 0)
        {
            parameter[8].Value = broaderTermString;
        }

        if (narrowerTermString.Trim().Length > 0)
        {
            parameter[9].Value = narrowerTermString;
        }

        if (relatedTermString.Trim().Length > 0)
        {
            parameter[10].Value = relatedTermString;
        }

        if (changedBy.Trim().Length > 0)
        {
            parameter[11].Value = changedBy;
        }

        parameter[12].Direction = ParameterDirection.Output;

        try
        {
            RunProcedure("Term_Update", parameter, out rowsAffected, AppConfiguration.ConnectionString());
        }
        catch
        {
            return false;
        }

        historyTermId = (int)parameter[12].Value;
        return rowsAffected > 0;
    }

    #endregion

    public static TermList GetAll()
    {
        TermList tl = null;

        using (DataTable dt = RunProcedure("Term_GetAll", new IDataParameter[] { }, "terms", AppConfiguration.ConnectionString()))
        {
            tl = new TermList();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    tl.Add(FillData(dr));
                }
            }
        }

        return tl;
    }

1 个答案:

答案 0 :(得分:0)

将方法转换为Webservice您需要将所有方法声明为[Operation Contract],将所有自定义类声明为[Data Contract] Attribute。你的例子非常简单的答案是

[Operation Contract]
public static int Add(Term term, string useString, string useForString, string broaderTermString, string narrowerTermString, string relatedTermString, string userName, ref int historyTermId);

此外,尝试在WCF中创建一个单独的Webservice项目,以便获得所有默认配置,然后手动添加具有适当合同的方法,并且操作应该起作用。