// Cache ChannelFactory if possible until no longer required.
// repeatedly creating it can be expensive.
// TODO - Don't forget to dispose this factory when you're finished with it.
private ChannelFactory factory =
new ChannelFactory<IService1>("WsHttpBinding_IService1");
...
ICommunicationObject commsObject = null;
bool success;
try
{
var proxy = factory.CreateChannel();
commsObject = (ICommunicationObject) proxy;
var request = new GetDataRequest();
var result = proxy.GetDataUsingDataContract(request);
// Message pattern - a single request type and a
// response type specific for the operation. This
// allows independent evolution of operation types.
commsObject.Close();
success = true;
return result;
} catch (TimeoutException ex)
{
// TODO - what to do if timeout occurs?
}
catch (FaultException ex)
{
// TODO - optional - if the service has its own Fault Exceptions defined.
}
catch (CommunicationException ex)
{
// TODO - Any other WCF related exceptions.
}
finally
{
if (!success) proxy.Abort();
}
References:
Stack Overflow - Channel FactoryMicrosoft MSDN Article
Stack Overflow - Generated Client Proxy
No comments:
Post a Comment