我正在尝试编写一个封装WCF调用的类(客户端是Silverlight,如果重要的话).这一切都在游泳,但我不知道如何陷阱连接失败,好像服务器不会响应.似乎在ChannelFactory生成的代码中某处发生了一些工作,但我不确定.也欢迎一般代码审查.:)
围绕创建通道的底线,或try/catch中的begin或async结果委托不会捕获失败的连接.我想让那个catch运行ServiceCallError事件.
public class ServiceCaller : IDisposable
{
private IFeedService _channel;
public ServiceCaller()
{
var elements = new List<BindingElement>();
elements.Add(new BinaryMessageEncodingBindingElement());
elements.Add(new HttpTransportBindingElement());
var binding = new CustomBinding(elements);
var endpointAddress = new EndpointAddress(App.GetRootUrl() + "Feed.svc");
_channel = new ChannelFactory<IFeedService>(binding, endpointAddress).CreateChannel();
}
public void MakeCall(DateTime lastTime, Dictionary<string, string> context)
{
AsyncCallback asyncCallBack = delegate(IAsyncResult result)
{
var items = ((IFeedService)result.AsyncState).EndGet(result);
if (ItemsRetrieved != null)
ItemsRetrieved(this, new ServiceCallerEventArgs(items));
};
_channel.BeginGet(lastTime, context, asyncCallBack, _channel);
}
public event ItemsRetrievedEventHandler ItemsRetrieved;
public event ServiceCallErrorHandler ServiceCallError; …Run Code Online (Sandbox Code Playgroud)