c# – WCF:客户端调用方法,但不返回

服务器客户架构.没有涉及IIS.这两个应用程序都是WinForm!

我在共享库中创建了以下接口:

[ServiceContract(SessionMode=SessionMode.Required,
        CallbackContract=typeof(ClientInterface))]
    public interface RequestInterface
    {
        [OperationContract]
        void Subscribe();

        [OperationContract]
        MyInterface GetInterface();
    }

    public interface MyInterface
    {
        [DataMember]
        List<MySubInterface> SubClasses{get;}
    }

    public interface MySubInterface
    {
        [DataMember]
        int Value { get; }
    }

并在服务器上实现它们,如下所示:

public class RequestHandler : RequestInterface
{
    private List<ClientInterface> iClients = new List<ClientInterface>();

    public MyInterface GetInterface()
    {
        List<MySubInterface> tList = new List<MySubInterface>();
        Form1.AddText("method invoked by:" + Thread.CurrentContext.ToString());
        foreach (RealSubclass tClass in Form1.iClass.SubClasses)
        {
            tList.Add(new TransmissionSubclass(tClass.Value));
        }

        TransmissionClass tTC = new TransmissionClass(tList);
        Form1.AddText("created:" + tTC);
        return tTC;
    }
    public void Subscribe()
    {
        Form1.AddText("subscribing:" + OperationContext.Current.GetCallbackChannel<ClientInterface>());
        iClients.Add(OperationContext.Current.GetCallbackChannel<ClientInterface>());
        fireClassEvent("halloWelt");
    }
}

在客户端上,我正在执行以下代码,尝试调用GetInterface()方法:

        ClientClass tClass = new ClientClass(this);
        DuplexChannelFactory<RequestInterface> pipeFactory =
         new DuplexChannelFactory<RequestInterface>(
            tClass,
            new NetNamedPipeBinding(),
            new EndpointAddress(
               "net.pipe://localhost/Request"));

        RequestInterface pipeProxy = pipeFactory.CreateChannel();

        //pipeProxy.Subscribe(); -- works like a charm
        MyInterface tInterface = pipeProxy.GetInterface(); // doesn't work
        fillListView(tInterface);

但是,当通过客户端逐步调试时,调试在标记的行处中断并且似乎退出此函数.
当我关闭应用程序的表单时,它只返回“逐步”模式.
另一方面,我可以从我的服务器上的日志输出中看到GetInterface()方法被执行.
TransmissionClass和TransmissionSubclass都在共享库中并实现MyInterface / MySubInterface

[DataContract]
[Serializable]
public class TransmissionClass : MyInterface
{
    [DataMember]
    public List<MySubInterface> SubClasses{get;private set;}

    public TransmissionClass(List<MySubInterface> aList)
    {
        SubClasses = aList;
    }

    public override string ToString()
    {
        return "TransmissionClass,Count:" + SubClasses.Count;
    }
}

服务器端的WCF初始化:

using (ServiceHost host = new ServiceHost(
            typeof(RequestHandler),
            new Uri[] { new Uri("net.pipe://localhost") }))
                    {

                        ServiceDebugBehavior tBehavior = new ServiceDebugBehavior();
                        if (null == tBehavior)
                        {
                            host.Description.Behaviors.Add(new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
                        }
                        else if (!tBehavior.IncludeExceptionDetailInFaults)
                        {
                            tBehavior.IncludeExceptionDetailInFaults = true;
                        }
                        host.AddServiceEndpoint(typeof(RequestInterface),
                          new NetNamedPipeBinding(), "Request");

                        host.Open();
                        Application.Run(new Form1());

                        host.Close();
        }

最佳答案 放置[KnownTypes]属性让WCF知道哪些具体类实现了您的接口.

点赞