question

Michael Urvan avatar image
Michael Urvan asked

Passing objects to Azure Function

What is the correct way to pass full objects to the Azure Function from the client?

I was able to get it to work using the code below, but you can see that I am doing something funky with the incoming FunctionArgument data to get it to work. It has double braces in it even after narrowing it down to the JObject value. If you know of a better way, let me know.

		[FunctionName("CreateOrUpdateGuild")]
		public static async Task<IActionResult> CreateOrUpdateGuild([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log)
		{
			log.LogInformation("CreateOrUpdateGuild()");
			
			FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
			string guildName = string.Empty;
			
			try {
				string json = ((JObject)context.FunctionArgument)["guild"].ToString();
				var guild = JsonConvert.DeserializeObject<Guild>(json.Replace("{
                {", "").Replace("}}",""));
				log.LogInformation("NewGuild() - guildName: {guildName}");


on the client I can use this method. I have my own Guild class and it works fine.

        PlayFabCloudScriptAPI.ExecuteFunction(
			new ExecuteFunctionRequest()
			{
				Entity = new PlayFab.CloudScriptModels.EntityKey()
				{
					Id = PlayFabSettings.staticPlayer.EntityId, //Get this from when you logged in,
					Type = PlayFabSettings.staticPlayer.EntityType, //Get this from when you logged in
				},
				FunctionName = "CreateOrUpdateGuild",
				FunctionParameter = new Dictionary<string, object>() { { "guild", guild } },
				GeneratePlayStreamEvent = false //Set this to true if you would like this call to show up in PlayStream
			}, (ExecuteFunctionResult result) =>


CloudScript
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sarah Zhang avatar image Sarah Zhang commented ·

Thanks for your sample code, we are researching for the method of passing object from client to Azure Functions. And could you please also provide the definition of Guild for our reference?

0 Likes 0 ·
Michael Urvan avatar image Michael Urvan Sarah Zhang commented ·
[System.Serializable]

public class Guild
{
public string id;
System.Guid guid;
public System.Guid GuildId
{
get
{
if (guid == default)
{
if (System.Guid.TryParse(id, out guid))
return guid;
return System.Guid.Empty;
}
return guid;
}
}
public System.Guid GameId;
public string OwnerPlayFabId;
public System.DateTime CreatedOn;
public string Name;
public string Location;
public string WebsiteUrl;
public string LogoUrl;
}
0 Likes 0 ·

1 Answer

·
JayZuo avatar image
JayZuo answered

According to your code, it seems what you want is to send a Guild as the FunctionParameter. If so, the simplest way is just set FunctionParameter to guild in client like:

//FunctionParameter = new Dictionary<string, object>() { { "guild", guild } },
FunctionParameter = guild  },

And then, in Azure Function, use Guild instead of dynamic:

//FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
FunctionExecutionContext<Guild> context = JsonConvert.DeserializeObject<FunctionExecutionContext<Guild>>(await req.ReadAsStringAsync());

Then you can get the guild and use it easily as the following:

var guild = context.FunctionArgument;
log.LogInformation($"NewGuild() - guildName: {guild.Name}");
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.