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!

  • 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. – Jeff Jul 22 '13 at 23:20
  • http://msdn.microsoft.com/en-us/library/ff649585.aspx – Jeff Jul 22 '13 at 23:21
  • Jeff, can you, please, explain, how to use DTO to make web service and jquery work together in my case? – Vjacheslav Ravdin Jul 23 '13 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 select(MyDTOClass params)`. You would need to give WCF a little help in figuring out how to serialize the class - http://msdn.microsoft.com/en-us/library/ms733811.aspx – Jeff Jul 23 '13 at 14:08
  • Oh, that is what you were talking about... But why is it safer??? – Vjacheslav Ravdin Jul 23 '13 at 15:14
  • It actually isn't inherently safer. However, since you need to explicitly define every member that goes on the DTO, you would be white-listing everything that gets transferred through the service. For example, if you had a service that returned users that are registered with your site, you might accidentally return a user with his/her password or other sensitive information to a client that through a dictionary, but if there is no password member on the DTO, then you will certainly not send the password back. – Jeff Jul 23 '13 at 15:20
  • Thanks, Jeff! 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. – Vjacheslav Ravdin Jul 23 '13 at 19:16

2 Answers2

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.

PostureOfLearning
  • 3,481
  • 3
  • 27
  • 44
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.

  • See http://stackoverflow.com/questions/15001755/pass-a-javascript-map-to-json-wcf-service – Vas Aug 31 '13 at 15:52