You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.4 KiB
41 lines
1.4 KiB
using System;
|
|
using System.ServiceModel;
|
|
|
|
namespace ConsoleTest.PTZApi
|
|
{
|
|
public class WcfUtil
|
|
{
|
|
public static void UseService<TChannel>(Action<TChannel> action, EndpointAddress address = null)
|
|
{
|
|
ChannelFactory<TChannel> channelFactory = new ChannelFactory<TChannel>("*");
|
|
TChannel val = ((!(address == null)) ? channelFactory.CreateChannel(address) : channelFactory.CreateChannel());
|
|
try
|
|
{
|
|
((IClientChannel)(object)val).Open();
|
|
action(val);
|
|
((IClientChannel)(object)val).Close();
|
|
}
|
|
catch
|
|
{
|
|
((IClientChannel)(object)val).Abort();
|
|
}
|
|
}
|
|
|
|
public static TReturn UseService<TChannel, TReturn>(Func<TChannel, TReturn> func, EndpointAddress address = null)
|
|
{
|
|
ChannelFactory<TChannel> channelFactory = new ChannelFactory<TChannel>("*");
|
|
TChannel val = ((!(address == null)) ? channelFactory.CreateChannel(address) : channelFactory.CreateChannel());
|
|
((IClientChannel)(object)val).Open();
|
|
TReturn result = func(val);
|
|
try
|
|
{
|
|
((IClientChannel)(object)val).Close();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
((IClientChannel)(object)val).Abort();
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|