Search code examples
c#odataodata-v4

How to call an OData function/action generated by a OData client generator?


Linked to my other question, if I have an OData function with the following definitions

builder.EntitySet<Ent>("Ent");
var companyActionConfig = builder.EntityType<Ent>().Action("MethodX");
entActionConfig.Parameter<int>("SomeParam1");
entActionConfig.Parameter<string>("SomeParam2");
entActionConfig.Returns<bool>();

which results into the following service definition (with regard to this action)

<Action Name="MethodX" IsBound="true">
    <Parameter Name="bindingParameter" Type="Ent"/>
    <Parameter Name="SomeParam1" Type="Edm.Int32" Nullable="false"/>
    <Parameter Name="SomeParam2" Type="Edm.String" Unicode="false"/>
    <ReturnType Type="Edm.Boolean" Nullable="false"/>
</Action>

Then how I could call the function from the OData v4 T4 generated code?

I see there's a class named ExtensionsMethods with the following function int it

public static class ExtensionMethods
{
    public static global::Microsoft.OData.Client.DataServiceActionQuerySingle<bool> MethodX(this global::Microsoft.OData.Client.DataServiceQuerySingle<global::Ent> source, int SomeParam1, string SomeParam2)
    {
        if (!source.IsComposable)
        {
            throw new global::System.NotSupportedException("The previous function is not composable.");

        }

        return new global::Microsoft.OData.Client.DataServiceActionQuerySingle<bool>(source.Context, source.AppendRequestUri("MethodX"), new global::Microsoft.OData.Client.BodyOperationParameter("SomeParam1", SomeParam1),
                new global::Microsoft.OData.Client.BodyOperationParameter("SomeParam2", SomeParam2));
    }

}

But I don't have an idea how to make the call in the code. That is, if I'd like to write something like

//How to call MethodX here?
//var entContainer = new Container("http://someurl").Ents...

it doesn't seem to be possible easily. How could I call the function?


Solution

  • You can refer client delayed query.

    For your case:

    var entContainer = new Container(new Uri("http://someurl"));
    bool result = entContainer.Ents.ByKey(IdOfEnt).MethodX(p1, p2).GetValue();