How to use Generic Repository for SOAP in Xamarin Form

Mohammad Ali Echreshavi 101 Reputation points
2021-10-20T11:09:59.087+00:00

hi,I used Generic Repository in my sample project in Xamrin form to get data from api. so my question is how can I make the following code Repository for soap webervice to become generic. so that the If you know of an example or a blog post please point me to the right direction

it is my Sample code:

public interface IGenericRepository

{
Task<T> GetAsync<T>(string uri, string authToken = "");

}

and impeliment interface:

public class GenericRepository: IGenericRepository

{
public async Task<T> GetAsync<T>(string uri, string authToken = "")
{
try
{
HttpClient httpClient = CreateHttpClient(uri);
string jsonResult = string.Empty;

        var responseMessage = await Policy
            .Handle<WebException>(ex =>
            {
                Debug.WriteLine($"{ex.GetType().Name + " : " + ex.Message}");
                return true;
            })
            .WaitAndRetryAsync
            (
                5,
                retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
            )
            .ExecuteAsync(async () => await httpClient.GetAsync(uri));

        if (responseMessage.IsSuccessStatusCode)
        {
            jsonResult =
                await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
            var json = JsonConvert.DeserializeObject<T>(jsonResult);
            return json;
        }

        if (responseMessage.StatusCode == HttpStatusCode.Forbidden ||
            responseMessage.StatusCode == HttpStatusCode.Unauthorized)
        {
            throw new ServiceAuthenticationException(jsonResult);
        }

        throw new HttpRequestExceptionEx(responseMessage.StatusCode, jsonResult);

    }
    catch (Exception e)
    {
        Debug.WriteLine($"{ e.GetType().Name + " : " + e.Message}");
        throw;
    }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-10-20T21:09:03.033+00:00