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

时间:2011-05-19 07:02:32

标签: c# .net 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()方法被执行 TransmissionClassTransmissionSubclass都在共享库中并实现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();
        }

2 个答案:

答案 0 :(得分:1)

您可以在IncludeExceptionDetailInFaults中启用ServiceDebugBehavior并向我们传达其他信息吗?

检查您的TransmissionClasses是否可以使用正确的属性(DataContract / DataMember / KnownType)进行序列化

答案 1 :(得分:1)

放入[KnownTypes]属性让WCF知道哪些具体类实现了你的接口。