简体   繁体   English

回调中的WCF服务超时

[英]WCF Service timeout in Callback

I'm trying to get to grips with WCF, in particular writing a WCF Service application with callback. 我正在尝试与WCF保持联系,尤其是编写带有回调的WCF服务应用程序。

I've setup the service, together with the callback contract but when the callback is called, the app is timing out. 我已经设置了服务以及回调协定,但是调用回调时,应用程序正在超时。

Essentially, from a client I'm setting a property within the service class. 本质上,我是从客户端在服务类中设置属性。 The Setter of this property, if it fails validation fires a callback and, well, this is timing out. 此属性的Setter(如果验证失败)会触发回调,并且正超时。

I realise that this is probably to it not being an Asynchronous calback, but can someone please show me how to resolve this? 我意识到这可能不是异步缩减,但是有人可以告诉我如何解决这个问题吗?

Thanks 谢谢

  // The call back (client-side) interface
public interface ISAPUploadServiceReply
{
    [OperationContract(IsOneWay = true)]
    void Reply(int stateCode);
}

// The Upload Service interface
[ServiceContract(CallbackContract = typeof(ISAPUploadServiceReply))]
public interface ISAPUploadService
{

    int ServerState
    {
        [OperationContract]
        get;
        [OperationContract(IsOneWay=true)]
        set;

And the implementation... 以及实施...

public int ServerState
    {
        get 
        { 
            return serverState; 
        }
        set 
        {
            if (InvalidState(Value))
            {
               var to = OperationContext.Current.GetCallbackChannel<ISAPUploadServiceReply>();
               to.Reply(eInvalidState);
            }
            else serverState = value; 
        } 
    }

My interfaces have been modified to (hopefully) reflect an asynchronous ability 我的接口已修改为(希望)反映异步功能

  // The call back (client-side) interface
public interface ISAPUploadServiceReply
{
   [OperationContractAttribute(AsyncPattern = true)]
    IAsyncResult BeginReply(string message, AsyncCallback callback, object state);
    void EndReply(IAsyncResult result);
}

..and the implementation (please, I'm very, very, guessing on this - and besides it doesn't work - 'server replied with runtime error of unknown response ....' ..和实现(请,我非常非常非常地对此猜测-除此之外它不起作用-“服务器回复了未知响应的运行时错误...。”

 public class SAPUploadServiceClient : ISAPUploadServiceReply
{
    private string msg;

    public IAsyncResult BeginReply(string message, AsyncCallback callback, object state)
    {
       // Create a task to do the work
       msg = message;
      var task = Task<int>.Factory.StartNew(this.DisplayMessage, state);
      return task.ContinueWith(res => callback(task));

    }

    public void EndReply(IAsyncResult result)
    {
      MessageBox.Show(String.Format("EndReply"));
    }

    private int DisplayMessage(object state)
    {
       MessageBox.Show(String.Format("Display Message At last here's  the message : {0}", msg));
       return 1;
    }
}

The callback is invoked by a public method within my service, callable from a client 回调由我的服务中的公共方法调用,可从客户端调用

  public void FireCallback(string msg)
    {
        ISAPUploadServiceReply callback = OperationContext.Current.GetCallbackChannel<ISAPUploadServiceReply>();

        callback.BeginReply("Hello from service " + msg, callback.EndReply, null);
    }

The interface modification is ... 界面修改为...

[ServiceContract(CallbackContract = typeof(ISAPUploadServiceReply))]
public interface ISAPUploadService
{
    [OperationContract(IsOneWay = true)]
    void FireCallback(string msg);

Please- I know the above looks quite desperate and that's why it is. 拜托-我知道上面看起来很绝望,这就是为什么。 I just want to get ANY call back working from my WCF Service so I can progress with this. 我只想从WCF服务获得任何回电,以便我可以继续进行下去。 It shouldn't be this hard to simply invoke an asynchronous callback. 简单地调用一个异步回调并不难。

I simply want to fire an event to the client from the service. 我只是想从服务向客户端触发事件。 That is ALL I'm trying to do. 那就是我想要做的。 The following perhaps explains the process of events that I'm trying to achieve more clearly... 以下内容也许可以更清楚地说明我正在尝试实现的事件的过程...

Client calls service method and waits.... Service method 1 undertakes some work (in this case, querying a database and constructing an xml response) When the work is finished, the service fires a callback event telling the client the work has completed and an XML response is available. 客户端调用服务方法并等待...。服务方法1承担一些工作(在这种情况下,查询数据库并构造xml响应)工作完成后,服务会触发回调事件,告知客户端工作已完成,并且XML响应可用。 Client reads XML response and continues. 客户端读取XML响应并继续。

Sheesh... 嘘...

Server side: 服务器端:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1 { ... }

Executing callback (service): 执行回调(服务):

var to = OperationContext.Current.GetCallbackChannel<IChatServiceCallback>();
Task.Factory.StartNew(() =>
{
    to.myCallbackFunction( ... );
});

Client side callback: 客户端回调:

[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
public class MyServiceCallback : IService1Callback { ... }

I am not 100% sure, but I remember I had such issues when I called the callback method from the same thread.. This should solve it: 我不确定100%,但是我记得当我从同一线程调用回调方法时遇到了此类问题。这应该可以解决:

public int ServerState
    {
        get
        {
            return serverState;
        }
        set
        {
            if (InvalidState(Value))
            {
                var to = OperationContext.Current.GetCallbackChannel<ISAPUploadServiceReply>();
                Task.Factory.StartNew(() =>
                    {
                        to.Reply(eInvalidState);
                    });

            }
            else
                serverState = value;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM