-2

我有一个数据访问类,它使用某些存储过程来选择/插入/更新数据库中的数据。如何将此类转换为 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;
    }
4

1 回答 1

0

要将方法转换为 Web 服务,您需要将所有方法声明为 [Operation Contract],并将所有自定义类声明为 [Data Contract] 属性。您的示例非常简单的答案是

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

为了解决这个问题,请尝试在 WCF 中创建一个单独的 Web 服务项目,以便您获得所有默认配置,然后手动添加具有适当合同和操作的方法应该可以工作。

于 2013-02-03T19:43:42.450 回答