I have a web application that serves users through connections to other third party web services.
I am not sure about the safe/efficient way to create those web service clients in my application.
As of .NET 4.5, I can use client code generated through svcutil and cache channel factories per service by setting the static CacheSetting property.
Example from MSDN site:
class Program
{
static void Main(string[] args)
{
ClientBase<ITest>.CacheSettings = CacheSettings.AlwaysOn;
foreach (string msg in messages)
{
using (TestClient proxy = new TestClient (new BasicHttpBinding(), new EndpointAddress(address)))
{
// ...
proxy.Test(msg);
// ...
}
}
}
}
// Generated by SvcUtil.exe
public partial class TestClient : System.ServiceModel.ClientBase, ITest { }
As a result there is no need to custom implement that functionality as well mentioned in :
- creating WCF ChannelFactory<T>
- https://philmunro.wordpress.com/2012/02/15/creating-a-wcf-service-proxy-with-channelfactory/
Also MSDN states that we should not use the C# "using" statement to automatically clean up resources when using a typed client and handle it with try/catch.
Example from MSDN site:
try
{
...
double result = client.Add(value1, value2);
...
client.Close();
}
catch (TimeoutException exception)
{
Console.WriteLine("Got {0}", exception.GetType());
client.Abort();
}
catch (CommunicationException exception)
{
Console.WriteLine("Got {0}", exception.GetType());
client.Abort();
}
1) Is the above way of calling a service safe to follow? Do I miss something? Or it's better to handle factory creation manually?
2) Am I going to have any problems with faulty states (both factory/channel)?
3) I need to create a new client for each service call right?