开发者

Implementing an interface using an asynchronous WCF service?

I am trying to开发者_StackOverflow figure out if it is possible to implement a .NET interface, where the interface method has to return something and you are implementing that particular interface method with an asynchronous WCF service?

Hopefully you should already see the issue I am encountering.

Here is the interface:

    public interface IDataService
    {
        IDataServiceResponse SendDataRequest();
    }

IDataServiceResponse is supposed to represent a simple container that holds the result of my asynchronous WCF callback.

Here is the code snippet where I am implementing the interface method, SendDataRequest()

        public IDataServiceResponse SendDataRequest()
        {
            InitializeDataServiceParameters();

            // Call the web service asynchronously, here....
            _client.BeginQueryJobs(_parameters, DataServiceQueryCallBack,  _client);

            // How do I return _dataServiceResponse, if I am calling the WCF service
            // asynchronously??

            return _dataServiceResponse;
        }

And the callback method signature:

void DataServiceQueryCallBack(IAsyncResult result)
{
  // ... implementation code
}

Thanks in advance, John


Apparently I misunderstood the original question, which was about how to use the asynchronous methods on a client proxy.

The answer is that you don't return from an asynchronous method call. It wouldn't be asynchronous if you could. If you are trying to encapsulate an asynchronous method on a WCF client, then you should encapsulate a callback instead of a return value:

public interface IDataService
{
    void SendDataRequest(Action<IDataServiceResponse> callback);
}

public class DataService
{
    public void SendDataRequest(Action<IDataServiceResponse> callback)
    {
        InitializeDataServiceParameters();
        _client.BeginQueryJobs(_parameters, DataServiceQueryCallBack, callback);
    }

    private void DataServiceQueryCallBack(IAsyncResult result)
    {
        var response = _client.EndQueryJobs(ar);
        // Convert the actual WCF response into your custom interface
        var dataServiceResponse = TranslateResponse(response);
        Action<IDataServiceResponse> callback = 
            (Action<IDataServiceResponse>)result.AsyncState;
        if (callback != null)
            callback(dataServiceResponse);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜