2011-05-19 84 views
4

服务器客户端体系结构。没有涉及IIS。这两个应用程序都是WinForm!WCF:客户端调用方法,但不返回

我做了一个共享库中的以下接口:

[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(); 
     } 

回答

1

把[KnownTypes]属性是为了让WCF知道具体类实现的接口。

+0

非常感谢!那解决了它:-) – yas4891 2011-05-20 05:34:08

+0

嗯。在这里有另一个问题:只要我只有几个(100)对象,它现在可以正常工作,但在增加数字(1,000,000)时会再次失败 – yas4891 2011-05-20 05:45:31

+0

@ yas4891 - 当您有1,000,000个对象时,有什么异常? – Maxim 2011-05-20 12:56:51

1

您可以启用ServiceDebugBehaviorIncludeExceptionDetailInFaults和沟通我们更多的相关信息?

检查你的TransmissionClasses可序列化与正确的属性(DataContract /数据成员/ KnownType)

+0

啊OK!谢谢。只要我再次在我的PC上就会完成。 – yas4891 2011-05-19 10:35:01

+0

我按照设想完成了。但是,我没有得到任何额外的信息。行为仍然是一样的。我已更新了我的帖子,其中包含有关'TransmissionClass'和服务器端WCF初始化的其他信息。 – yas4891 2011-05-19 15:36:21