客户端没有发生WCF回调

时间:2016-01-14 06:53:56

标签: wcf

我尝试为我的测试自动化实现WCF双工服务。调用对服务器来说很好,但它没有回调客户端方法。我也已经浏览过其他博客和stacoverflow。使用Isoneway属性为true,将Concurrancy模式设置为reentrant,并将usesynchronizationcontext设置为false。以前它工作正常。我不知道是什么导致了这个问题。 在此服务合同中,添加了回调

[ServiceContract(CallbackContract=typeof(ICollaborationServiceCallBack),SessionMode=SessionMode.Required)]
    public interface ICollaborationInfrastructureService
    {

在测试中,初始化代理

[TestFixture]
[CallbackBehavior(UseSynchronizationContext = false, ConcurrencyMode = ConcurrencyMode.Reentrant,IncludeExceptionDetailInFaults=true)]
public class SAF_TestCollaboration : TestFixtureBase, ICollaborationInfrastructureServiceCallback
{
    private SfdTestSteps steps = null;
    private CollaborationInfrastructureServiceClient client = null;  
    private SfdTask task = null;
    protected override void TestFixtureSetupBegin()
    {
        base.TestFixtureSetupBegin();
        steps = new SfdTestSteps();
        task = new SfdTask();
        PrepareEnvironment();
        client = new CollaborationInfrastructureServiceClient(new InstanceContext(this));
    }

    public void Login(string username,string password)
    {
        TestMonitor.Do("xyz","xyz", "xyz",
            () =>
            {
                xyzzzzz.....
                OperationContext.Current.GetCallbackChannel<ICollaborationServiceCallBack>().DoLoginIn("xyz", "xyz");
            });
    }

方法在测试方实施......

    public void DoLoginIn(string username, string password)
    {
        steps.Login.UserName = username;
        steps.Login.Password = password;
        steps.Login.DoLogin();
    }

1 个答案:

答案 0 :(得分:4)

问题是OperationContext.Current绑定到线程。如果切换到另一个线程OperationContext.Current则为空。

试试这个:

   public void Login(string username,string password)
    {
        var callbackChannel = OperationContext.Current.GetCallbackChannel<ICollaborationServiceCallBack>();
        TestMonitor.Do("xyz","xyz", "xyz",
            () =>
            {
                xyzzzzz.....
                callbackChannel.DoLoginIn("xyz", "xyz");
            });
    }