4

I have a service with several methods in it. I set my Global.asax file to handle the CORS, and everything used to work perfectly, CORS included... That is until I added the following to the interface file:

[OperationContract]
[WebInvoke(UriTemplate = "/Transform", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
bool XXXTransform(string user, string x);

and the following to the service class...

bool XXXTransform(string user, string x)
{
    return true;
}

Now, here is the really interesting thing. The error happens when I call the GetRecipes method. Even more interesting is that if I only have a single parameter specified for XXXTransform for both the interface and the service class, it doesn't throw the error - as in bool XXXTransform(string user)

Has anyone ever seen anything like this before? Why would using one parameter work, but two parameters will cause the origin error when calling a totally different method?

IChomp.cs:

namespace NomNomNotebookService
{
    // May need to mod service behavior for upload...
    [ServiceContract]
    public interface IChomp
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "/{userId}/Recipes", Method = "GET", ResponseFormat = WebMessageFormat.Json)]
        List<Recipe> GetRecipes(string userId);

        [OperationContract]
        [WebInvoke(UriTemplate = "/{userId}/Recipes", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Recipe AddRecipe(string userId, Recipe recipe);

        [OperationContract]
        [WebInvoke(UriTemplate = "/{userId}/Recipes/{recipeId}", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Recipe UpdateRecipe(string userId, string recipeId, Recipe recipe);

        [OperationContract]
        [WebInvoke(UriTemplate = "/{userId}/Recipes/{recipeId}", Method = "DELETE", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
        bool DeleteRecipe(string userId, string recipeId);

        [OperationContract]
        [WebInvoke(UriTemplate = "/{userId}/Recipes/{recipeId}", Method = "GET", ResponseFormat = WebMessageFormat.Json)]
        Recipe GetRecipe(string userId, string recipeId);

        [OperationContract]
        [WebInvoke(UriTemplate = "/Categories", Method = "GET", ResponseFormat = WebMessageFormat.Json)]
        List<Category> GetCategories();

        [OperationContract]
        [WebInvoke(UriTemplate = "/Upload", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        bool XXXUpload();

        [OperationContract]
        [WebInvoke(UriTemplate = "/Transform", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        bool XXXTransform(string user, string x); // WILL work if I have bool XXXTransform(string user)
    }
}

Chomp.cs

namespace NomNomNotebookService
{
    public class Chomp : IChomp
    {
        private DBConnector _dbCon = new DBConnector();

        public List<Recipe> GetRecipes(string userId)
        {
            _dbCon.Parameters.Add(new CustSqlParam("@userId", Int16.Parse(userId)));
            DataTable dt = _dbCon.RunProc("J_NNN_GetRecipesForUser");

            List<Recipe> recipes = new List<Recipe>();
            foreach (DataRow row in dt.Rows)
            {
                recipes.Add(new Recipe((int)row["Uid"], (string)row["Name"], (string)row["Description"], (string)row["Category"]));
            }

            _dbCon.Kill();

            return recipes;
        }

        public Recipe GetRecipe(string userId, string recipeId)
        {
            _dbCon.Parameters.Add(new CustSqlParam("@uid", Int16.Parse(recipeId)));
            DataTable dt = _dbCon.RunProc("J_NNN_GetRecipe");

            Recipe recipe = new Recipe(dt.Rows[0]);
            _dbCon.Kill();

            return recipe;
        }

        // USER ID IS HERE FOR VALIDATION ONCE SESSIONS ARE ROLLING
        public Recipe AddRecipe(string userId, Recipe recipe)
        {
            _dbCon.Parameters.Add(new CustSqlParam("@userId", Int16.Parse(userId)));
            _dbCon.Parameters.Add(new CustSqlParam("@name", recipe.Name));
            _dbCon.Parameters.Add(new CustSqlParam("@description", recipe.Description));
            _dbCon.Parameters.Add(new CustSqlParam("@categoryId", recipe.Category.Uid));
            _dbCon.Parameters.Add(new CustSqlParam("@prepTime", recipe.PrepTime));
            _dbCon.Parameters.Add(new CustSqlParam("@cookTime", recipe.CookTime));
            _dbCon.Parameters.Add(new CustSqlParam("@readyTime", recipe.ReadyTime));

            DataTable dt = _dbCon.RunProc("J_NNN_AddRecipe");
            _dbCon.Kill();
            return new Recipe(dt.Rows[0]);
        }

        public Recipe UpdateRecipe(string userId, string recipeId, Recipe recipe)
        {
            _dbCon.Parameters.Add(new CustSqlParam("@uid", Int16.Parse(recipeId)));
            _dbCon.Parameters.Add(new CustSqlParam("@name", recipe.Name));
            _dbCon.Parameters.Add(new CustSqlParam("@description", recipe.Description));
            _dbCon.Parameters.Add(new CustSqlParam("@categoryId", recipe.Category.Uid));
            _dbCon.Parameters.Add(new CustSqlParam("@prepTime", recipe.PrepTime));
            _dbCon.Parameters.Add(new CustSqlParam("@cookTime", recipe.CookTime));
            _dbCon.Parameters.Add(new CustSqlParam("@readyTime", recipe.ReadyTime));

            DataTable dt = _dbCon.RunProc("J_NNN_UpdateRecipe");
            _dbCon.Kill();
            return new Recipe(dt.Rows[0]);
        }

        public bool DeleteRecipe(string userId, string recipeId)
        {
            try
            {
                _dbCon.Parameters.Add(new CustSqlParam("@uid", Int16.Parse(recipeId)));
                DataTable dt = _dbCon.RunProc("J_NNN_DeleteRecipe");
                _dbCon.Kill();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return false;
            }
            return true;
        }

        public List<Category> GetCategories()
        {
            DataTable dt = _dbCon.RunProc("J_NNN_GetCategories");
            _dbCon.Kill();

            List<Category> categories = new List<Category>();
            foreach (DataRow row in dt.Rows)
            {
                categories.Add(new Category((int)row["Uid"], (string)row["Name"]));
            }

            return categories;
        }

        // Upload shit....
        public bool XXXUpload()
        {
            return false;
        }

        public bool XXXTransform(string user, string x) // WILL work if I have bool XXXTransform(string user)
        {

            return true;
        }
    }
}

Global.asax

namespace NomNomNotebookService
{
    public class Global : System.Web.HttpApplication
    {
        protected void Session_Start(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }
    }
}
abatishchev
  • 92,232
  • 78
  • 284
  • 421
Jim Elrod
  • 121
  • 6
  • Can we see the actual code OP? – Krythic Dec 24 '14 at 04:47
  • Also, if any clarification is needed on the class files i didn't include, feel free to indicate - it should be mostly straightforward, though. – Jim Elrod Dec 24 '14 at 04:56
  • check this link if it talks about a similar issue in the Rest based Web API: http://stackoverflow.com/questions/27373165/issues-with-cors-in-asp-net – Mrinal Kamboj Dec 24 '14 at 04:58
  • Seems like a security issue. I am looking into it still. – Krythic Dec 24 '14 at 04:59
  • @MrinalKamboj - I have my Global.asax file handling the verbs for all requests, and it worked fine... until I started using 2 parameters with the last method. – Jim Elrod Dec 24 '14 at 05:08
  • @Jim it also talks about additional code in Application_BeginRequest for specific verbs which were causing the Cors Issue. You may want to do the same for the verbs causing issue in this case. – Mrinal Kamboj Dec 24 '14 at 05:12

1 Answers1

1

I've have had a similar error in the past. Do you have your request headers set in your config?

'Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'
'Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'
snowYetis
  • 1,327
  • 13
  • 28