我知道使用Autofac
,可以举办WCF
服务。逆转这种方式呢?是否可以使用Autofac
消费WCF
服务?我的意思是客户端。如果是,那该怎么办?如何使用Autofac来使用WCF服务?
4
A
回答
5
看看http://code.google.com/p/autofac/wiki/WcfIntegration#Clients。 你只需要注册一个ChannelFactory<IYourServiceContract>
注册绑定配置,然后注册频道创建。别忘了打电话给UseWcfSafeRelease()。
2
我建议您按照WCF integration wiki page的第一部分说明。
关于落实唯一要注意的是,UseWcfSafeRelease
电话ICommunicationObject.Close()
在服务实例的释放。在我看来,这很糟糕,因为它会阻塞,直到Web调用完全处理所有缓冲区,并且在某些情况下会阻塞UI线程(在Silverlight中)。我最好打电话给ICommunicationObject.Abort()
,因为如果我释放一个组件实例,这意味着我不再需要它的进程。这就是说,我用的是RegistrationExtensions class以下版本:
/// <summary>
/// Extend the registration syntax with WCF-specific helpers.
/// </summary>
public static class RegistrationExtensions
{
/// <summary>
/// Dispose the channel instance in such a way that exceptions
/// </summary>
/// <typeparam name="TLimit">Registration limit type.</typeparam>
/// <typeparam name="TActivatorData">Activator data type.</typeparam>
/// <typeparam name="TRegistrationStyle">Registration style.</typeparam>
/// <param name="registration">Registration to set release action for.</param>
/// <returns>Registration builder allowing the registration to be configured.</returns>
/// <remarks>This will eat exceptions generated in the closing of the channel.</remarks>
public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle>
UseWcfSafeRelease<TLimit, TActivatorData, TRegistrationStyle>(
this IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> registration)
{
if (registration == null) throw new ArgumentNullException("registration");
return registration.OnRelease(CloseChannel);
}
static void CloseChannel<T>(T channel)
{
var disp = (IClientChannel) channel;
disp.Abort();
}
}
但如果你喜欢它更多的,你一定能使用Autofac内置的客户端集成代码。
0
@Pavel加季洛夫 我通过反射
private static void CloseChannel<T>(T channel)
{
IClientChannel channel2 = (IClientChannel) channel;
try
{
if (channel2.State == CommunicationState.Faulted)
{
channel2.Abort();
}
else
{
channel2.Close();
}
}
catch (TimeoutException)
{
channel2.Abort();
}
catch (CommunicationException)
{
channel2.Abort();
}
catch (Exception)
{
channel2.Abort();
throw;
}
}
相关问题
- 1. 如何使用autofac解析WCF数据服务?
- 2. 如何使用WCF服务
- 3. 如何使用WCF服务
- 4. 如何使用本地WSDL文件来使用WCF服务?
- 5. Azure WCF服务使用Azure WCF服务
- 6. 如何使用JMeter调用WCF服务?
- 7. 使用Autofac在WCF服务中实现工作单元
- 8. 使用WCF服务
- 9. 如何使用WebRole在Azure下使用WCF配置autofac
- 10. 如何使用WSDL模拟WCF服务?
- 11. 如何使用json.net WCF REST服务
- 12. 如何使用Silverlight调试WCF服务
- 13. 如何使用xamarin.forms中的wcf服务?
- 14. 如何在C#中使用WCF服务
- 15. 我如何使用WCF数据服务?
- 16. 如何在android中使用WCF服务?
- 17. 如何使用.Net 4.0 REST WCF服务?
- 18. 如何使用Mono创建WCF服务?
- 19. 注入服务为使用Autofac
- 20. 使用Autofac作为服务定位器
- 21. 注射服务为使用Autofac
- 22. 如何使用WCF服务来实现肥皂响应
- 23. 如何使用证书来保护3跳WCF外观服务?
- 24. 如何在.NET 4.5中使用ADFS来保护WCF Web服务?
- 25. 使用Windows服务的WCF
- 26. WP7使用WCF服务
- 27. WCF使用网络服务
- 28. 使用SSL的WCF服务
- 29. 创建使用WCF服务
- 30. WCF服务使用WcfTestClient
你真的需要反思提取? ;-) https://github.com/autofac/Autofac.Wcf/blob/master/src/Autofac.Integration.Wcf/RegistrationExtensions.cs – 2015-04-18 11:33:09