开发者

WCF "Fire and Forget" method is not allowing host execution to continue as expected

开发者 https://www.devze.com 2023-03-04 07:58 出处:网络
I have a windows service hosting a singleton WCF service which caches a large amount of data. On start up of the windows service I am doing the following:

I have a windows service hosting a singleton WCF service which caches a large amount of data. On start up of the windows service I am doing the following:

// start client service
        wcfService= new ServiceHost(typeof(MyWcfService));
        wcfService.Open();

        using (HostedServiceReference.WcfServiceProxy wcfServiceProxy = new HostedServic开发者_Python百科eReference.WcfClientServiceProxy())
        {
            wcfServiceProxy.RefreshDisplayCacheFromSource();
            // 1st echo to console
            Console.WriteLine("Display Cache Refreshed"));
        }
        // 2nd echo to console
        Console.WriteLine("Begin other processing"))

and I have the method configured as follows in the service contract:

[OperationContract(IsOneWay=true)]
    void RefreshDisplayCacheFromSource();

I had expected to immediately see the 1st and 2nd echos displayed in the console, but what I am actually seeing is just the 1st echo. The 2nd line is not displayed until my "fire and forget" method has completed it's long operation.

Can anyone explain what is going on in the background?

My theories so far:

Is the operation of the wcf service in singleton mode blocking the service hosting it?

Is it something to do with the using statement?


The proxy object for the WCF Service will be disposed of at the end of the using block, but this cannot happen until the client receives acknowledgement that the service received its message. It seems that the service does not send the acknowledgement until it had executed all the code in the method body (including processing the large amount of data), therefore execution on the client waits at the end of the using block until the service has finished its processing.

There are 2 ways I was able to use to get around this issue:

Make the proxy object a global variable in the client.

Spin off a thread in the server to handle the long processing, allowing the server method to return immediately with the acknowledgement the client is waiting for.


If your goal is to just be able to continue on with other work while the WCF call does its thing, right click on your service reference and hit "Update Service Reference." Check the option to generate Asynchronous Methods.

Then call it as wcfServiceProxy.RefreshDisplayCacheFromSourceAsync();

If you want to be notified when it finishes you can add an event handler for it too.

0

精彩评论

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