Click here to Skip to main content
15,177,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am testing my WCF service via WCFTestClient. My methods run for me and return the correct values. However, when I execute from the WCFTestClient my entities are not persisting. Here is the method I am testing:

C#
public PlayerActionResult DoAction(PlayerActionType type, int uid, string pparam = null, int amount = 0, string svalue = null) 
        {
            Player player;
            PlayerActionResult result;
            PlayerAction action = (PlayerAction)Activator.CreateInstance(null, string.Concat("Conquest.Players.Actions.", type.ToString())).Unwrap();
            action.Logic = (ActionLogic)Activator.CreateInstance(null, string.Concat("Conquest.Players.Actions.", type.ToString(), "Logic")).Unwrap();
            action.Logic.Action = action;
            // Verify executor exists
            if (!PlayerExists(uid))
            {
                return new PlayerActionResult(false, ResultType.NotFound);
            }
            else { player = GetPlayer(uid); }

            if (pparam != null & !PlayerExists(pparam))
            {
                if (!PlayerExists(pparam))
                {
                    return new PlayerActionResult(false, ResultType.NotFound);
                }
                action.playerparam = GetPlayer(pparam);
            }

                result = player.DoAction(action);
                new PlayersRepositoryManager(entities).SavePlayer(player);


            return result;
        }


And here is the persistence logic in the PlayerRepositoryManager:

C#
public class PlayersRepositoryManager : IPlayersRepository
    {
        #region Member Variables
        private GameDataEntities entity = null;
        #endregion

        #region Constructor
        public PlayersRepositoryManager(GameDataEntities entity)
        {
            this.entity = entity;

        }
       public Player SavePlayer(Player player)
        {
            var updated = entity.Players.Attach(player);
            entity.Entry(player).State = System.Data.EntityState.Modified;
            return updated;
        }
}


The "entities" parameter is a property of the WCF service "ConquestService" which contains the DoAction method:

C#
GameDataEntities entities = new GameDataEntities();


The only value that is changing in the Player object is Movement, which when I test in WcfTestClient returns that is succeeded, here is the logic of that particular action.

C#
public class PrayLogic : ActionLogic
{
    public PlayerActionResult Execute()
    {
        if (!CheckMP())
        {
            return new PlayerActionResult(false, ResultType.NoMovement);
        }

        Kingdom kingdom = Action.player.CurrentKingdom;

        if (Helper.GetRandom(100, 1) <= 25 + (kingdom.Honor + 1))
            kingdom.Honor += Helper.GetRandom(3, 1);
        return new PlayerActionResult(true, ResultType.Success);
    }
}


And the ActionLogic base class contains the CheckMP() method where the actual subtraction of the movement is done:

C#
public abstract class ActionLogic
    {
        public PlayerAction Action { get; set; }

        internal bool CheckMP()
        {
            if (Action.player.Movement < Action.Movement) { return false; }
            Action.player.Movement -= Action.Movement;
            return true;
        }
    }


I have tried the approach above and I have tried calling SaveChanges() on the player object from the service method and from a method within the player object and none seem to work to persist the deduction of 2 movement from the player. ARGH. Any help would be greatly appreciated I'm eager to learn what I'm doing wrong, thanks!
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900