3

I'm trying to pass JavaScript object to C# WCF service as Dictionary and I don't know how to do it...

I have a WCF service:

[OperationContract]
[WebInvoke]
public List<psy_trance_fm_genre> select(SortedDictionary<string, object> parameters)
{
    ...
}

And I have some JavaScript / JQuery code:

$.ajax({
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ '@genre': '', '@start_row_index': 0, '@maximum_rows': 100 }),
    error: function (jqXHR, textStatus, errorThrown) {
        ...
    },
    success: function (data, textStatus, jqXHR) {
        ...
    },
    type: 'POST',
    url: 'svc/psy_trance_fm_genres.svc/select'
});

Please help me to make them work together! Thanks in advance!

7
  • Do you really need the service to be a dictionary? That seems like it would be too open, too insecure, or too susceptible to ineffeciencies. I usually create a DTO for my services which would work great for your situation. Commented Jul 22, 2013 at 23:20
  • msdn.microsoft.com/en-us/library/ff649585.aspx Commented Jul 22, 2013 at 23:21
  • Jeff, can you, please, explain, how to use DTO to make web service and jquery work together in my case? Commented Jul 23, 2013 at 12:26
  • A DTO is basically just a custom class with the sole purpose of transferring data from one place to another. You would create whatever properties you need on that object and use that as your parameter in WCF e.g. public List<psy_trance_fm_genre> select(MyDTOClass params). You would need to give WCF a little help in figuring out how to serialize the class - msdn.microsoft.com/en-us/library/ms733811.aspx Commented Jul 23, 2013 at 14:08
  • Oh, that is what you were talking about... But why is it safer??? Commented Jul 23, 2013 at 15:14

2 Answers 2

1

Maybe this will help you or put you in the right direction:

An example of an Ajax call I'm using:

    var data2Send = {
        "CategoryID": CatID, "AccountID": AccID, "RegionID": RegID, "PersonID": PerID, "BudgetID": BudID,
        "AccountDetailTypeID": AdtID
    }
//data2Send could also contain i.e. an array of arrays, list of lists, etc


    $.ajax({
        type: "GET",
        url: '../../Service/myservice.svc/GetAccountDetails',
        dataType: "json",
        data: data2Send,
        contentType: "application/json; charset=utf-8",
        success: function (data) { var accountDetails = JSON.parse(data.d); },
        error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); }
    });

The WCF contract:

    [WebInvoke(Method = "GET")]
    [OperationContract]
    string GetAccountDetails(int AccountID = 0, int RegionID = 0, int PersonID = 0, int CategoryID = 0, int BudgetID = 0, string AccountDetailTypeID = "");

Note that the the contract returns a string. I do this using:

        var accountDetailList = db.AccountDetails
            .Select(ad => new
            {
                AccountDetailTypeID = ad.AccountDetailTypeID,
                Reference = ad.Reference,
                Description = ad.Description,
        AccountDetailID = ad.AccountDetailID,
            })
            .ToList();

return JsonConvert.SerializeObject(accountDetailList);

I hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, but it is too complex. I'm sure there's a way to achieve my goal much easier...
0

I decided to rewrite my service to use only strings and int as parameters.

As far as I understand, it's impossible to pass the dictionary in a simple straightforward way.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.