Need help to write web api wrapper for Silverlight project - c#

Please bear with me as I am new to Silverlight. I need to write a web api wrapper (I've named it WebClientWrapper) which is supposed to consume rest services. The project uses Silverlight 5. I am facing many problems while writing such a wrapper. There are a lot of examples demonstrating rest service consumption in C#. But unfortunately none of them worked for me. It's been a challenge for me to get done with what I need. Below are my requirements:
1) UI should not freeze while I make any GET request,
2) Calling methods from WebClientWrapper should be as easier as possible.
3) .NET framework version of project should not be changed.
I tried following things so far:
1) Use of HttpClient. I referred this link: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client. The problem with this approach is I cannot call ReadAsAsync method. Calling this method requires change in framework (i.e. replacing dlls or changing framework version) which is not feasible.
2) Use of WebClient. http://www.kastory.net/index.php?option=com_content&view=article&id=25:rest-web-service-in-c-with-silverlight-and-asp-net-web-api&catid=32:web-services&Itemid=130.
The problem with this is I will probably (not sure!) have to make changes in the way I call methods from WebClientWrapper. I am trying to accomplish calling method from WebClientWrapper something like this:
var appointments = WebClientWrapper.Get<Appointments>(new Dictionary<string, string>()
{
{"hospitalId", "19465654546"}
}, "appointment");
Below is the code snippet I tried in WebClientWrapper.
private const string BaseUrl = "http://localhost:63455/api";
private static WebClient GetClient()
{
int leadingInt = new Random().Next(10000, 99999);
int trailingInt = new Random().Next(1000, 9999);
string date = DateTime.Now.ToString("ddHHmmMMssMMyyyyyss");
string ticketString = string.Format("{0}{1}{2}", leadingInt, date, trailingInt);
var client = new WebClient();
client.Headers["Accept"] = ticketString;
client.Headers["UserAgent"] = "ReceptionistApp";
return client;
}
private static void DownloadCompletionHandler<T>(object sender, DownloadStringCompletedEventArgs e)
{
Encoding messageEncoding = Encoding.UTF8;
var serializer = new DataContractJsonSerializer(typeof (T));
var memoryStream = new MemoryStream(messageEncoding.GetBytes(e.Result));
var objectToReturn = (T) serializer.ReadObject(memoryStream);
}
public static T Get<T>(Dictionary<string, string> paramDictionary, string controller)
{
string absoluteUrl = BaseUrl + controller + "?";
absoluteUrl = paramDictionary.Aggregate(absoluteUrl,
(current, keyValuePair) => current + (keyValuePair.Key + "=" + keyValuePair.Value + "&"));
absoluteUrl = absoluteUrl.TrimEnd('&');
WebClient client = GetClient();
client.DownloadStringCompleted += DownloadCompletionHandler<T>;
client.DownloadStringAsync(new Uri(absoluteUrl));
}
Below are the things I want to mention regarding the code above:
1) As obvious, compiler throws error for the method Get<T> since I've not returned object of type T. How shall I get that object from DownloadStringAsync? I know I can use DownloadStringTaskAsync. But it is not available with the current framework. What changes do I have to make in my code so that I get appointments as shown in the Get<Appointments> method call?
2) DownloadCompletionHandler<T> is bound to return void but actually I want to return objectToReturn as shown in code.
Help will be greatly appreciated. Any new code snippets that will fulfill my requirements are welcome.

RestSharp helped me instead of WebClient.

Related

RestRequest no longer contains AddJsonBody

I have been using restsharp to make this simple request to a webservice:
string sParmDb = Newtonsoft.Json.JsonConvert.SerializeObject(parmsDb);// parmsDb is some object too large to be passed as a query parameter
sParmDb = System.Web.HttpUtility.UrlEncode(sParmDb);
RestSharp.RestRequest;
RestSharp.RestClient cl = new RestSharp.RestClient();
RestSharp.RestResponse rsp;
string sQueryStr = "http://myWebService";
restCall = new RestSharp.RestRequest(sQueryStr, RestSharp.Method.POST);
restCall.AddJsonBody(sParmDb);
rsp = (RestSharp.RestResponse)cl.Execute(restCall);
this works fine on NuGet version 106.9.0 but when I updated the package to 108.0.1 it doesn't compile.
RestSharp.Method doesn't contain POST, though it does have a Post, so perhaps that's a simple change.
The main problem is that RestRequest no longer contains AddJsonBody.
What would be the simplest, quickest code change that will fix this?
Thanks for your help.
UPDATE 26/7/2022
----------------
I realized that the reason that restCall.AddJsonBody was not available was that I needed to include the RestSharp.Extensions as a using directive.
Now this code compiles under version 108, the current version:
RestSharp.RestClient cl = new RestSharp.RestClient();
RestSharp.RestRequest restCall = new RestSharp.RestRequest("http://localhost:60484/api/db", RestSharp.Method.POST);
restCall.AddJsonBody("abcdefg");
RestSharp.RestResponse rsp = (RestSharp.RestResponse)cl.Execute(restCall);
This reaches the web service method, which I am running in debug mode on localhost, but the string value parameter is null.
Do I need another line(s) of code in the calling client to make it work as it does under version 106?
The web service looks like this:
public string Post([FromBody] string value)
{
//do something with value and return some string
}
string sQueryStr = "http://myWebService";
RestSharp.RestClient cl = new RestSharp.RestClient(sQueryStr);
var restCall = new RestRequest() { Method= Method.Post};
...
restCall.AddStringBody(sParmDb, RestSharp.ContentType.Json);
article https://restsharp.dev/usage.html#request-body

How to call google.apis.dialogflow.v2 in C#

I am new to Google APIs. I want to know how to call Google Dialogflow API in C# to get intent form the input text. But I can't find any example to call Dialogflow using C#.
Please provide some example to call Dialogflow from C#.
If I understand your question correctly you want to call the DialogFlow API from within a C# application (rather than writing fulfillment endpoint(s) that are called from DialogFlow. If that's the case here's a sample for making that call:
using Google.Cloud.Dialogflow.V2;
...
...
var query = new QueryInput
{
Text = new TextInput
{
Text = "Something you want to ask a DF agent",
LanguageCode = "en-us"
}
};
var sessionId = "SomeUniqueId";
var agent = "MyAgentName";
var creds = GoogleCredential.FromJson("{ json google credentials file)");
var channel = new Grpc.Core.Channel(SessionsClient.DefaultEndpoint.Host,
creds.ToChannelCredentials());
var client = SessionsClient.Create(channel);
var dialogFlow = client.DetectIntent(
new SessionName(agent, sessionId),
query
);
channel.ShutdownAsync();
In an earlier version of the DialogFlowAPI I was running into file locking issues when trying to re-deploy a web api project which the channel.ShutDownAsync() seemed to solve. I think this has been fixed in a recent release.
This is the simplest version of a DF request I've used. There is a more complicated version that passes in an input context in this post:
Making DialogFlow v2 DetectIntent Calls w/ C# (including input context)
(Nitpicking: I assume you know DialogFlow will call your code as specified/registered in the action at DialogFlow? So your code can only respond to DialogFlow, and not call it.)
Short answer/redirect:
Don't use Google.Apis.Dialogflow.v2 (with GoogleCloudDialogflowV2WebhookRequest and GoogleCloudDialogflowV2WebhookResponse) but use Google.Cloud.Dialogflow.v2 (with WebhookRequest and WebhookResponse) - see this eTag-error. I will also mention some other alternatives underneath.
Google.Cloud.Dialogflow.v2
Using Google.Cloud.Dialogflow.v2 NuGet (Edit: FWIW: this code was written for the beta-preview):
[HttpPost]
public dynamic PostWithCloudResponse([FromBody] WebhookRequest dialogflowRequest)
{
var intentName = dialogflowRequest.QueryResult.Intent.DisplayName;
var actualQuestion = dialogflowRequest.QueryResult.QueryText;
var testAnswer = $"Dialogflow Request for intent '{intentName}' and question '{actualQuestion}'";
var dialogflowResponse = new WebhookResponse
{
FulfillmentText = testAnswer,
FulfillmentMessages =
{ new Intent.Types.Message
{ SimpleResponses = new Intent.Types.Message.Types.SimpleResponses
{ SimpleResponses_ =
{ new Intent.Types.Message.Types.SimpleResponse
{
DisplayText = testAnswer,
TextToSpeech = testAnswer,
//Ssml = $"<speak>{testAnswer}</speak>"
}
}
}
}
}
};
var jsonResponse = dialogflowResponse.ToString();
return new ContentResult { Content = jsonResponse, ContentType = "application/json" }; ;
}
Edit: It turns out that the model binding may not bind all properties from the 'ProtoBuf-json' correctly (e.g. WebhookRequest.outputContexts[N].parameters),
so one should probably use the Google.Protobuf.JsonParser (e.g. see this documentation).
This parser may trip over unknown fields, so one probably also wants to ignore that. So now I use this code (I may one day make the generic method more generic and thus useful, by making HttpContext.Request.InputStream a parameter):
public ActionResult PostWithCloudResponse()
{
var dialogflowRequest = ParseProtobufRequest<WebhookRequest>();
...
var jsonResponse = dialogflowResponse.ToString();
return new ContentResult { Content = jsonResponse, ContentType = "application/json" }; ;
}
private T ParseProtobufRequest<T>() where T : Google.Protobuf.IMessage, new()
{
// parse ProtoBuf (not 'normal' json) with unknown fields, else it may not bind ProtoBuf correctly
// https://github.com/googleapis/google-cloud-dotnet/issues/2425 "ask the Protobuf code to parse the result"
string requestBody;
using (var reader = new StreamReader(HttpContext.Request.InputStream))
{
requestBody = reader.ReadToEnd();
}
var parser = new Google.Protobuf.JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true));
var typedRequest = parser.Parse<T>(requestBody);
return typedRequest;
}
BTW: This 'ProtoBuf-json' is also the reason to use WebhookResponse.ToString() which in turn uses Google.Protobuf.JsonFormatter.ToDiagnosticString.
Microsoft's BotBuilder
Microsoft's BotBuilder packages and Visual Studio template.
I havent't used it yet, but expect approximately the same code?
Hand written proprietary code
A simple example of incoming request code (called an NLU-Response by Google) is provided by Madoka Chiyoda (Chomado) at Github. The incoming call is simply parsed to her DialogFlowResponseModel:
public static async Task<HttpResponseMessage> Run([...]HttpRequestMessage req, [...]CloudBlockBlob mp3Out, TraceWriter log)
...
var data = await req.Content.ReadAsAsync<Models.DialogFlowResponseModel>();
Gactions
If you plan to work without DialogFlow later on, please note that the interface for Gactions differs significantly from the interface with DialogFlow.
The json-parameters and return-values have some overlap, but nothing gaining you any programming time (probably loosing some time by starting 'over').
However, starting with DialogFlow may gain you some quick dialog-experience (e.g. question & answer design/prototyping).
And the DialogFlow-API does have a NuGet package, where the Gactions-interface does not have a NuGet-package just yet.

"vshost32.exe has stopped working" OR "fatal execution engine error" in Visual Studio

I'm encountering some odd errors in Visual Studio and can't find head or tail. I'm coding some backend in C# which contacts a third party API to retrieve data. The code in question, a single class, is part of a larger solution, but must be the problem as the errors encountered does not occur when not using this class.
Computer setup:
Visual Studio 2013, update 4
Windows 10, Preview Build 10041
Errors encountered
Yesterday the application started behaving weird when debugging it.
The first error I don't remember exactly, but it was something along the lines of "bad" or "corrupted memory".
Without altering the program, I could also encounter a FatalExecutionEngineError exception, which would be thrown immediately after trying to run the program (It didn't make it to the first breakpoint, which was on the first line in the Main entry of the program. Weird!
EDIT: Looks like this:
Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'PathRedacted\whatsfordinner\whatsfordinner\bin\Debug\whatsfordinner.vshost.exe'.
Additional information: The runtime has encountered a fatal error. The address of the error was at 0x613e4379, on thread 0x11ac. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
In the end I rebooted my computer since it was all very strange. Problem solved until today.
Now I can't seem to run the program at all; Upon running the program, vshost32.exe just crashes. I don't get any error messages or anything to hint at where the issue is.
Troubleshooting steps
Rebooted my computer - No change, vshost32.exe crashes upon execution
Outcommented the two lines where the class in question was used - Program runs fine.
Tried starting the program as "Release" rather than "Debug". - Program seems to run fine, although I can't test it to the end. (The class is not entirely done yet, and I don't want to spam the API in question)
Tried running the program on another computer running Windows 7 and Visual Studio 2012. - Program seemed to run fine.
At this point I'm pretty lost. I have little idea as to where the issue might be. The source code unfortunately consists of nearly 200 lines, but as I have no clue, I am posting it all.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Specialized;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace whatsfordinner {
public class eTilbudRetriever {
//Web method names
readonly String Get = "GET";
readonly String Post = "POST";
readonly String Update = "UPDATE";
readonly String Delete = "DELETE";
//Parameter identifiers
readonly String ParamApiKey = "api_key";
readonly String ParamLatitude = "r_lat";
readonly String ParamLongitude = "r_lng";
readonly String ParamRadius = "r_radius";
readonly String ParamLimit = "limit";
readonly String ParamOffset = "offset";
//Parameter values
String Latitude = "57.051188"; //Aalborg coordinates
String Longitude = "9.922371";
String Radius = "800000"; //Radius in meters (800km)
String Limit = "48"; // Results per query
//Custom header identifiers
readonly String HeaderXToken = "X-Token";
readonly String HeaderXSignature = "X-Signature";
//Custom header values
readonly String ContentType = "application/json";
//Web Addresses
readonly String HostAddress = "https://api.etilbudsavis.dk/v2/";
readonly String Sessions = "sessions";
readonly String Stores = "stores";
readonly String Offers = "offers";
readonly String Dealers = "dealers";
//Keys
readonly String ApiKey = "<Redacted>";
readonly String ApiSecret = "<Redacted>";
String XToken; //Same as a Session Token in documentation
String XSignature; //Same as a Session Signature in documentation
public eTilbudRetriever() {
//Create a body consisting of the API key
List<KeyValuePair<String, String>> body = new List<KeyValuePair<String, String>>();
body.Add(new KeyValuePair<String, String>(ParamApiKey, ApiKey));
//Send request to create a new session
String response = SendWebRequest(Post, Sessions, body);
//Get the Session Token from the response
dynamic json = JObject.Parse(response);
XToken = json.token;
//Save the Session Signature as well (SHA256 version of API Secret combined with Session Token)
XSignature = ConvertToSha256(ApiSecret + XToken);
}
public void GetDealersList() {
GetList(Dealers);
}
public void GetStoresList() {
GetList(Stores);
}
public void GetOffersList() {
GetList(Offers);
}
private void GetList(string target) {
List<String> resultSet = new List<String>();
String result;
int offset = 0;
//Add desired parameters as headers for the eTilbudsavisen API
List<KeyValuePair<String, String>> query = new List<KeyValuePair<String, String>>();
query.Add(new KeyValuePair<String, String>(ParamLatitude, Latitude));
query.Add(new KeyValuePair<String, String>(ParamLongitude, Longitude));
query.Add(new KeyValuePair<String, String>(ParamRadius, Radius));
query.Add(new KeyValuePair<String, String>(ParamLimit, Limit));
query.Add(new KeyValuePair<String, String>(ParamOffset, offset.ToString()));
//Retrieve a result through the request
result = SendWebRequest(Get, target, query);
/*
* If result is valid, add it to the set of valid results.
* Keep sending requests and increase the offset to avoid duplicated results
* Stop when returned results are no longer valid
*/
while (!String.IsNullOrEmpty(result)) {
resultSet.Add(result);
offset += Int32.Parse(Limit);
query[query.Count-1] = new KeyValuePair<String, String>(ParamOffset, offset.ToString());
result = SendWebRequest(Get, target, query);
}
}
private String SendWebRequest(String method, String extension, List<KeyValuePair<String, String>> arguments) {
try {
String finalAddress = HostAddress + extension;
//Add query to Address (if applicable)
if (method.Equals(Get)) {
finalAddress += '?';
finalAddress += arguments[0].Key + '=' + arguments[0].Value;
for (int i = 1; i < arguments.Count; i++) {
finalAddress += '&' + arguments[i].Key + '=' + arguments[i].Value;
}
}
//Create request and set mandatory header properties
var request = (HttpWebRequest)WebRequest.Create(finalAddress);
request.Method = method;
request.ContentType = ContentType;
request.Accept = ContentType;
//If a Session Token and Signature are available (= After session create), add as headers
if (!String.IsNullOrEmpty(XToken)) {
request.Headers.Add(HeaderXToken, XToken);
request.Headers.Add(HeaderXSignature, XSignature);
}
//Create JSON string containing the desired body arguments (if applicable)
if (method.Equals(Post)) {
//Write body to API
using (var writer = new StreamWriter(request.GetRequestStream())) {
writer.Write(MakeJsonBody(arguments));
}
}
//get response as a JSON object in string format
var response = (HttpWebResponse)request.GetResponse();
return new StreamReader(response.GetResponseStream()).ReadToEnd();
} catch (UriFormatException e) {
Console.WriteLine(e.ToString());
return null;
} catch (WebException e) {
Console.WriteLine(e.ToString());
return null;
}
}
private String ConvertToSha256(String text) {
byte[] bytes = Encoding.UTF8.GetBytes(text);
SHA256Managed hashstring = new SHA256Managed();
byte[] hash = hashstring.ComputeHash(bytes);
string hashString = string.Empty;
foreach (byte x in hash) {
hashString += String.Format("{0:x2}", x);
}
return hashString;
}
private String MakeJsonBody(List<KeyValuePair<String, String>> arguments) {
String json = "{";
foreach (KeyValuePair<String, String> kv in arguments) {
json += "\"" + kv.Key + "\": \"" + kv.Value + "\"";
if (arguments.IndexOf(kv) != arguments.Count() - 1) {
json += ", ";
}
}
json += "}";
return json;
}
}
}
In Main, this is what is executed in relation to the class. The program runs fine when removing these lines from the solution.
eTilbudRetriever retriever = new eTilbudRetriever();
retriever.GetDealersList();
Windows 10, Preview Build 10041
That's the only cue to the possible reason why your program is crashing like this. There are no other ones, your code doesn't do anything dangerous and Newtonsoft.Json has been slammed every possible way by millions of programs. You are using beta versions of both the .NET Framework (v4.6) and the operating system. Thanks on behalf of all Microsoft customers to help debug this new software, your problem is not one that we'll have to troubleshoot. Hopefully, FEEE crashes are exceedingly nasty and hard to debug.
What you are supposed to do is submit a minidump of the crashed process to Microsoft so they can fix the underlying bug. Whatever it might be, there are no cues in your question. Maybe it is the complete rewrite of the x64 jitter (project code name RyuJit). The odds that it has no bugs right now are very slim and such a bug can certainly crash your program like this. That's just a wild guess though.
Microsoft makes these previews available at no cost. Their underlying intention is to get the bugs out before the product ships. Should happen somewhere around the summer. Only real way they can have some confidence that their Support phone lines are not going to get overloaded once they ship the product. The beta updates come fast and furious, there have been 6 CTP versions of .NET 4.6. Quite unprecedented, there usually are no more than 3. At least part of it is the beta for VS2015, lots and lots of new stuff in that release. Which you are not using, that doesn't help either.
Your role in this is one as an unpaid beta-tester. This tends to be rather incompatible with your other role, a programmer that makes a living writing and debugging code. Your code, not somebody else's. When you can't afford to be bogged-down like this then the only sensible thing to do is to unsubscribe from that beta program. Restore your machine to a known-good version of the framework and the operating system. Right now that's .NET 4.5.2 and Windows 8.1

How to work with the BING REST Api

How exactly do you use the BING REST api (specifically the ROUTES part) to get a driving distance in ASP.NET.
I have searched high and low on Google for this answer and none is forthcoming.
I have found url strings such as:
http://dev.virtualearth.net/REST/v1/Routes/Driving?waypoint.0=redmond&heading=90&waypoint.1=seattle&du=mi&key=BingMapsKey
That's great! But how to call it from ASP?
I have also found this code:
private void GetResponse(Uri uri, Action<HttpResponse> callback)
{
WebClient wc = new WebClient();
wc.OpenReadCompleted += (o, a) =>
{
if (callback != null)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(HttpResponse));
callback(ser.ReadObject(a.Result) as HttpResponse);
}
};
wc.OpenReadAsync(uri);
}
Which is a "generic method to make web requests". But, again, how do you call it? I find it confusing that it doesn't require a return type.
In order to call it, I have found code like this:
string key = "YOUR_BING_MAPS_KEY or SESSION_KEY";
string query = "1 Microsoft Way, Redmond, WA";
Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key));
GetResponse(geocodeRequest, (x) =>
{
Console.WriteLine(x.ResourceSets[0].Resources.Length + " result(s) found.");
Console.ReadLine();
});
But when I add this to the project, I get every error under the sun coming up. So, I am stuck.
I am a total ASP beginner and haven't found any online documentation any help at all.
p.s. I do have a BING api key and do use it in the code above.
I am not an expert in this, but the below compiles for me. Also make sure to add the data constructs as mentioned in the BING documentation:
protected void Page_Load(object sender, EventArgs e)
{
string key = "YOUR KEY";
string query = "ADDRESS";
Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key));
GetResponse(geocodeRequest, (x) =>
{
Console.WriteLine(x.ResourceSets[0].Resources.Length + " result(s) found.");
Console.ReadLine();
});
}
Quoting from another stackoverflow question:
The bottom of the documentation you are using points to the Data contracts you need for the REST services which are available here: http://msdn.microsoft.com/en-us/library/jj870778.aspx
Simply create a empty C# file and copy in paste in the C# Data Contracts. Then add the namespace to this class:
using BingMapsRESTService.Common.JSON;

using c# to create node in drupal

i need to make a ("webservice") c# app that can create/update/delete nodes for/from drupal 7 using xmlrpc. everytime i run my app i get errors from the xmlrpc files(library). I tried to find code/documentation for C# using xmlrpc to connect to drupal, but in vain.
I would be nice if you could point me in the right direction, or share some c# code with me.
{
[XmlRpcUrl("http://testing/testserver")]
public interface IDrupalServices
{
[XmlRpcMethod("node.get")]
XmlRpcStruct NodeLoad(int nid, string[] field);
[XmlRpcMethod("node.save")]
void NodeSave(XmlRpcStruct node);
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
IDrupalServices drupal = XmlRpcProxyGen.Create<IDrupalServices>();
int nid = 227;
string[] fields = new string[] { };
XmlRpcStruct node = drupal.NodeLoad(nid, fields);
string teaser = node["teaser"].ToString();
welcomeTxt.Text = teaser;
}
private void button1_Click(object sender, EventArgs e)
{
string title = txtTitle.Text;
string body = txtBody.Text;
IDrupalServices drupal = XmlRpcProxyGen.Create<IDrupalServices>();
XmlRpcStruct node = new XmlRpcStruct();
node["id"] = 1001;
node["title"] = title;
node["body"] = body;
node["teaser"] = body;
node["format"] = 1;
node["type"] = "webservice";
node["promote"] = false;
drupal.NodeSave(node);
MessageBox.Show("The post was been created!");
}
}
}
After i run this i get the error: Server returned a fault exception: [-32601] Server error. Requested method node.get not specified. - in the XmlRpcSerializer.cs
Thank you,
Florin
If you're using Drupal 7 you must be using Services 3 which doesn't have a node.get method (or node.save as it happens). They've been replaced with node.retrieve and node.create & node.update respectively.
You can view all of the available methods in the resources/node_resource.inc file in the Services module folder.
UPDATE
Internally the node is submitted using drupal_execute which is the function used to submit a form. Since the body is a field in Drupal it's expected to be a multi-dimensional array in this format (PHP version):
$data["body"][$language][0]["value"]
The $language will either be the specific language for the node, or und for undefined language (unless you're dealing with a multi-lingual site und is usually the way to go). You'd need to build an array similar to that in your C# code and Drupal should save it.
ANOTHER UPDATE
The Java XML-RPC client example for Services uses a HashMap type to do this so my best guess is you could use a Dictionary (albeit one that seems unnecessarily complicated):
var innerValue = new Dictionary<string, string>();
innerValue.Add("value", txtBody.Text);
var language = new Dictionary<int, Dictionary<string, string>>();
language.Add(0, innerValue);
var body = new Dictionary<string, Dictionary<int, Dictionary<string, string>>>();
body.Add("und", language);
node["body"] = body;
It's been a few years since I've coded in C# so forgive any errors in there. Also I'm pretty sure it could be declared more efficiently but I've forgotten most of the language to be honest!
Jan's answer is not quite right. If you are using the cook xmlrpc library all you would need to do is this:
XmlRpcStruct postStruct = new XmlRpcStruct();
postStruct.Add("type", "article");
postStruct.Add("title", "wohoo another test");
XmlRpcStruct postBodyStructParams = new XmlRpcStruct();
postBodyStructParams.Add("value", "My body yaaay");
postBodyStructParams.Add("format", "filtered_html");
XmlRpcStruct[] postBodyStructParamsArr = new XmlRpcStruct[1];
postBodyStructParamsArr[0] = postBodyStructParams;
XmlRpcStruct postBodyStruct = new XmlRpcStruct();
postBodyStruct.Add("und", postBodyStructParamsArr);
postStruct.Add("body", postBodyStruct);
Unfortunately, the body params need to be an array of struct under the "und" key with only one value. I blame this on the sloppiness of the drupal xmlrpc API.

Categories