加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 17:16:55 所属栏目:百科 来源:网络整理
导读:服务器客户架构.没有涉及IIS.这两个应用程序都是WinForm! 我在共享库中创建了以下接口: [ServiceContract(SessionMode=SessionMode.Required,CallbackContract=typeof(ClientInterface))] public interface RequestInterface { [OperationContract] void S
服务器客户架构.没有涉及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),"Request");

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

                        host.Close();
        }

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读