簡體   English   中英

實現雙工時WCF中的超時異常

[英]TimeOut exception in WCF while implementing duplex

我的服務合同和回調合同如下所示:

[ServiceContract(CallbackContract = typeof(IWebshopCallback))]
interface IWebshop
{
    [OperationContract]
    string GetWebshopName ();
    [OperationContract]
    string GetProductInfo (Item i);
    [OperationContract]
    List<Item> GetProductList ();
    [OperationContract]
    bool BuyProduct (string i);
    [OperationContract]
    void ConnectNewClient ();
}

[ServiceContract]
interface IWebshopCallback
{
    [OperationContract]
    void NewClientConnected (int totalNrOfConnectedClients);
}

我的服務:

[ServiceBehavior (InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
class WebshopService : IWebshop
{
  ...
}

在服務內部,我有一個方法可以在客戶端調用另一個方法:

public void ConnectNewClient ()
    {
        totalNumber++;
        OperationContext.Current.GetCallbackChannel<IWebshopCallback> ().NewClientConnected (totalNumber);
    }

在客戶端,我有一個從IWebshopCallback派生並具有方法NewClientConnected(int a)的表單。

問題是,當我嘗試運行代碼時,遇到了以下異常:

發送到http:// localhost:4000 / IWebshopContract的此請求操作在配置的超時時間(00:00:59.9989895)內未收到答復。 分配給該操作的時間可能是較長超時的一部分。

但是,更奇怪的是,如果我繼續應用程序的工作,我會看到此功能有效。

是什么原因造成的呢?

在服務器端,當您調用一個函數時,您需要使用Task.Run(),因此您應該像這樣:

var callback = OperationContext.Current.GetCallbackChannel<IWebshopCallback> ();
Task.Run (() => callback.NewClientConnected(totalNumber));

而不是這樣:

OperationContext.Current.GetCallbackChannel<IWebshopCallback> ().NewClientConnected ();

通常,除雙工模式外,我們的行為是客戶端在調用服務后立即返回,並且服務器發送回的信息通過回調合同進行傳輸。 在這種工作模式下,我們將服務合同和回調合同都轉換為單向通信。

[OperationContract(Action = "post_num", IsOneWay = true)]
void PostNumber(int n);

我做了一個演示,希望對您有用。

服務器端。

class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost sh=new ServiceHost(typeof(MyService)))
            {
                ServiceMetadataBehavior smb;
                smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb==null)
                {
                    smb = new ServiceMetadataBehavior()
                    {
                        HttpGetEnabled = true
                    };
                    sh.Description.Behaviors.Add(smb);
                }
                sh.Open();
                Console.WriteLine("Service is ready");

                Console.ReadKey();
                sh.Close();
            }
        }
    }
    [ServiceContract(Namespace ="mydomain",Name = "demo", ConfigurationName = "isv", CallbackContract = typeof(ICallback))]
    public interface IDemo
    {
        [OperationContract(Action = "post_num", IsOneWay = true)]
        void PostNumber(int n);
    }
    [ServiceContract]
    public interface ICallback
    {
        [OperationContract(Action = "report", IsOneWay = true)]
        void Report(double progress);
    }

    [ServiceBehavior(ConfigurationName ="sv")]
    public class MyService : IDemo
    {
        public void PostNumber(int n)
        {
            ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
            for (int i = 0; i <=n; i++)
            {
                Task.Delay(500).Wait();
                double p = Convert.ToDouble(i) / Convert.ToDouble(n);
                callback.Report(p);
            }
        }
    }

服務器配置

  <system.serviceModel>
    <services>
      <service name="sv">
        <endpoint address="http://localhost:3333" binding="wsDualHttpBinding" contract="isv"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:3333"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

客戶端。

class Program
{
    static void Main(string[] args)
    {
        DuplexChannelFactory<IDemo> factory = new DuplexChannelFactory<IDemo>(new CallbackHandler(), "test_ep");
        IDemo channel = factory.CreateChannel();
        Console.WriteLine("Start to Call");
        channel.PostNumber(15);
        Console.WriteLine("Calling is done");
        Console.ReadLine();
    }
}
[ServiceContract(Namespace ="mydomain",Name = "demo", ConfigurationName = "isv", CallbackContract = typeof(ICallback))]
public interface IDemo
{
    [OperationContract(Action = "post_num",IsOneWay =true)]
    void PostNumber(int n);
}
[ServiceContract]
public interface ICallback
{
    [OperationContract(Action = "report",IsOneWay =true)]
    void Report(double progress);
}
public class CallbackHandler : ICallback
{
    public void Report(double progress)
    {
        Console.WriteLine("{0:p0}", progress);
    }
}

客戶端配置

  <system.serviceModel>
    <client>
      <endpoint name="test_ep" address="http://localhost:3333" binding="wsDualHttpBinding" contract="isv"/>
    </client>
  </system.serviceModel>

結果。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM