carol1287
carol1287

Reputation: 395

ServiceStack return response syntax error

I am just quickly upgrading ServiceStack to version 4.5.4 and I am having a problem trying to convert my service. Basically I cannot longer return the sql response as a string. Does anyone know the correct syntax for this?

I have managed to change to the new syntax, however I am not able to find the correct way to change the following line:

return new PostAccountResponse() { message = string.Format("{0}", message.Value };

public object Post(Account.Model.Account request)
        {
            switch (request.Accounttype)
            {
                case "dview":
                    try
                    {
                        return dbFactory.Exec(dbCmd =>

                        {
                            dbCmd.CommandType = System.Data.CommandType.StoredProcedure;
                            dbCmd.CommandText = "stored procedure...";
                            dbCmd.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("@mobile", request.mobile));
                            MySql.Data.MySqlClient.MySqlParameter message = new MySql.Data.MySqlClient.MySqlParameter("@message", "");
                            message.Direction = System.Data.ParameterDirection.Output;
                            dbCmd.Parameters.Add(message);
                            gender.Direction = System.Data.ParameterDirection.Output;
                            r = dbCmd.ExecuteReader();
                            return new PostAccountResponse() { message = string.Format("{0}", message.Value };
                        });
                    }
                    catch (Exception ex)
                    {
                       //
                    }
                    break;
            }
            return new object();

        }

Does anyone know how to correctly return the response for my service? Thank you so much. I experimented with the following but also didn't work:

    var rdr = dbCmd.ExecuteReader(CommandBehavior.CloseConnection);
                            var results = new List<Account.Model.PostAccountResponse>();
                            while (rdr.Read())
                            {
                                results.Add(new Account.Model.PostAccountResponse { message = string.Format("{0}", message.Value) });
                            }
return new PostAccountResponse { message = results };

UPDATED 1

I mam trying to upgrade a service to the latest version and the code doesn't compile anymore with this new version. That's what I mean by doesn't work. The compiler throws these errors on the return new PostAccountResponse line:

1.- Anonymous function converted to a void returning delegate cannot return a value.

2.- Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type.

3.- Cannot implicitly convert type 'Account.Model.PostAccountResponse' to 'System.Data.IDbCommand'. An explicit conversion exists (are you missing a cast?)

4.- Cannot implicitly convert type 'Account.Model.PostAccountResponse' to 'System.Threading.Tasks.Task'

Upvotes: 2

Views: 151

Answers (1)

mythz
mythz

Reputation: 143319

Instead of using dbFactory.Exec() you should use a DB connection in a using statement like:

using (var db = dbFactory.OpenDbConnection())
using (var dbCmd = db.CreateCommand())
{
    dbCmd.CommandType = System.Data.CommandType.StoredProcedure;
    dbCmd.CommandText = "stored procedure...";
    dbCmd.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("@mobile", request.mobile));
    MySql.Data.MySqlClient.MySqlParameter message = new MySql.Data.MySqlClient.MySqlParameter("@message", "");
    message.Direction = System.Data.ParameterDirection.Output;
    dbCmd.Parameters.Add(message);
    gender.Direction = System.Data.ParameterDirection.Output;
    r = dbCmd.ExecuteReader();
    return new PostAccountResponse() { message = string.Format("{0}", message.Value };
}

You're trying to use a raw ADO.NET connection here to call a MySql Stored procedure, but normally when you're accessing your database using OrmLite APIs you can just use base.Db ADO.NET Connection directly, e.g:

return Db.Select<Table>();

Upvotes: 3

Related Questions