Static wcf client to make multiple request with one object

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
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()
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.
c# multithreading wcf
add a comment |Â
up vote
1
down vote
favorite
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()
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.
c# multithreading wcf
1
Is the variable Cliente and Client meant to be the same?
â BKSpurgeon
Jan 18 at 23:00
@BKSpurgeon Yes! Sorry for that, now it is correct
â Sxntk
Jan 19 at 13:36
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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()
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.
c# multithreading wcf
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()
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.
c# multithreading wcf
edited Jan 19 at 13:36
asked Jan 18 at 20:44
Sxntk
1085
1085
1
Is the variable Cliente and Client meant to be the same?
â BKSpurgeon
Jan 18 at 23:00
@BKSpurgeon Yes! Sorry for that, now it is correct
â Sxntk
Jan 19 at 13:36
add a comment |Â
1
Is the variable Cliente and Client meant to be the same?
â BKSpurgeon
Jan 18 at 23:00
@BKSpurgeon Yes! Sorry for that, now it is correct
â Sxntk
Jan 19 at 13:36
1
1
Is the variable Cliente and Client meant to be the same?
â BKSpurgeon
Jan 18 at 23:00
Is the variable Cliente and Client meant to be the same?
â BKSpurgeon
Jan 18 at 23:00
@BKSpurgeon Yes! Sorry for that, now it is correct
â Sxntk
Jan 19 at 13:36
@BKSpurgeon Yes! Sorry for that, now it is correct
â Sxntk
Jan 19 at 13:36
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
I think you should consider inverting control and moving ServiceClient instantiation outside the repository class. Whether or not ServiceClient is a singleton (in single-instance sense) or not is not something that repository should manage or care about:
public class Conector : IConector
private IRepository Repository get;
public Conector(IRepository repository)
Repository = repository;
...
public class Repository : IRepository
protected ServiceClient Client get;
public Repository(ServiceClient client)
Client = client;
...
Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Likepublic Conector()in the question andpublic Conector(IRepository repository)like yours?
â Sxntk
Jan 19 at 13:43
@Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance ofServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
â Nikita B
Jan 22 at 10:08
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I think you should consider inverting control and moving ServiceClient instantiation outside the repository class. Whether or not ServiceClient is a singleton (in single-instance sense) or not is not something that repository should manage or care about:
public class Conector : IConector
private IRepository Repository get;
public Conector(IRepository repository)
Repository = repository;
...
public class Repository : IRepository
protected ServiceClient Client get;
public Repository(ServiceClient client)
Client = client;
...
Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Likepublic Conector()in the question andpublic Conector(IRepository repository)like yours?
â Sxntk
Jan 19 at 13:43
@Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance ofServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
â Nikita B
Jan 22 at 10:08
add a comment |Â
up vote
2
down vote
accepted
I think you should consider inverting control and moving ServiceClient instantiation outside the repository class. Whether or not ServiceClient is a singleton (in single-instance sense) or not is not something that repository should manage or care about:
public class Conector : IConector
private IRepository Repository get;
public Conector(IRepository repository)
Repository = repository;
...
public class Repository : IRepository
protected ServiceClient Client get;
public Repository(ServiceClient client)
Client = client;
...
Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Likepublic Conector()in the question andpublic Conector(IRepository repository)like yours?
â Sxntk
Jan 19 at 13:43
@Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance ofServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
â Nikita B
Jan 22 at 10:08
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I think you should consider inverting control and moving ServiceClient instantiation outside the repository class. Whether or not ServiceClient is a singleton (in single-instance sense) or not is not something that repository should manage or care about:
public class Conector : IConector
private IRepository Repository get;
public Conector(IRepository repository)
Repository = repository;
...
public class Repository : IRepository
protected ServiceClient Client get;
public Repository(ServiceClient client)
Client = client;
...
I think you should consider inverting control and moving ServiceClient instantiation outside the repository class. Whether or not ServiceClient is a singleton (in single-instance sense) or not is not something that repository should manage or care about:
public class Conector : IConector
private IRepository Repository get;
public Conector(IRepository repository)
Repository = repository;
...
public class Repository : IRepository
protected ServiceClient Client get;
public Repository(ServiceClient client)
Client = client;
...
answered Jan 19 at 11:24
Nikita B
12.3k11652
12.3k11652
Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Likepublic Conector()in the question andpublic Conector(IRepository repository)like yours?
â Sxntk
Jan 19 at 13:43
@Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance ofServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
â Nikita B
Jan 22 at 10:08
add a comment |Â
Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Likepublic Conector()in the question andpublic Conector(IRepository repository)like yours?
â Sxntk
Jan 19 at 13:43
@Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance ofServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
â Nikita B
Jan 22 at 10:08
Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Like
public Conector() in the question and public Conector(IRepository repository) like yours?â Sxntk
Jan 19 at 13:43
Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Like
public Conector() in the question and public Conector(IRepository repository) like yours?â Sxntk
Jan 19 at 13:43
@Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance of
ServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.â Nikita B
Jan 22 at 10:08
@Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance of
ServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.â Nikita B
Jan 22 at 10:08
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185428%2fstatic-wcf-client-to-make-multiple-request-with-one-object%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Is the variable Cliente and Client meant to be the same?
â BKSpurgeon
Jan 18 at 23:00
@BKSpurgeon Yes! Sorry for that, now it is correct
â Sxntk
Jan 19 at 13:36