I want to make a static client to call a wcf because the first time I call the wcf it takes too long, even sometimes it throws time out.
I have the following class:
public class Repository : IRepository
{
protected static ServiceClient Client { get; set; }
public Repository()
{
if (Client == null)
{
Client = new ServiceClient();
}
if (Client.ChannelFactory.State != CommunicationState.Opened
|| Client.ChannelFactory.State != CommunicationState.Created)
{
Client = new ServiceClient();
}
}
public virtual async Task<Response> GetPersonsFromAddress(string address)
{
try
{
Request request = new Request();
request.Address = address;
Response response = await Client.GetPersonsAsync(request);
return response;
}
catch (Exception ex)
{
// ...
// Handle exception
// ...
}
}
}
And this class calls the method:
public class Conector : IConector
{
private IRepository Repository { get; }
public Conector()
{
Repository = new Repository();
}
public virtual async Task<Response> GetPersonsFromAddress(string address)
{
Response response = await Repository.GetPersonsFromAddress(address);
return response;
}
}
The object that calls the wcf service is static so I want to know if this is a good practice.
Also if there are a lot of people at the same time using this method, it will return the correct response for each request.