繁体   English   中英

充当客户端和服务器的程序

[英]Program acting as client and server

关于WCF,我有几个问题:-程序可以同时充当客户端和服务器吗? -我的代码有什么问题:

服务:

[ServiceContract]
public interface IShout
{
    [OperationContract]
    String Broadcast(String message);
}

实现:

public class eveShout : IShout
{
    public String Broadcast(String message)
    {
        return message + " reply";
    }
}

我以构造器的形式启动服务:

ServiceHost s = new ServiceHost(typeof(IShout));
s.AddServiceEndpoint(typeof(IShout), new BasicHttpBinding(), "http://localhost:9189");
s.Open();

当我单击其他表单上的按钮时,我想发送一条消息并得到回复。 我使用以下代码:

ChannelFactory<IShout> channel = new ChannelFactory<IShout>(new BasicHttpBinding(), "http://localhost:9189");
IShout shout = channel.CreateChannel();

String reply = shout.Broadcast("Test");

注意:所有代码都在同一个命名空间中。 注意:我首先启动“服务器”(打开),然后继续运行该应用程序。

当我运行代码时,将创建服务器。 我使用netstat -a查看端口是否打开。 当我运行命令时,我得到9189处于侦听状态。 但是代码在命令Reply = shout(“ test”)处停止。 我得到一个例外,说

00:00:59之后等待回复时请求频道超时...

是的,您可以让一个应用程序同时充当客户端和服务器。

我看到可能需要更正的几件事。 首先,尝试添加OperationContract。

[ServiceContract]
public interface IShout
{
    [OperationContract]
    String Broadcast(String message);
} 

然后,选择类的类型,而不是接口的类型。

ServiceHost s = new ServiceHost(typeof(eveShout));
s.AddServiceEndpoint(typeof(IShout), new BasicHttpBinding(), "http://localhost:9189");
s.Open(); 

确保您有权访问命名空间(s.Open()应该抛出异常,如果没有)。

net http add urlacl url=http://+:9189/ user=...

查看这些建议是否有帮助。

(哦,在您的课堂上公开广播)

一个简单的WindowsFormsApplication示例如下所示:

// form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ChannelFactory<IShout> channel = new ChannelFactory<IShout>(new BasicHttpBinding(), "http://localhost:9189");
            IShout shout = channel.CreateChannel();

            String reply = shout.Broadcast("Test"); 

        }
    }
}

// and Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ServiceModel;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ServiceHost s = new ServiceHost(typeof(eveShout));
            s.AddServiceEndpoint(typeof(IShout), new BasicHttpBinding(), "http://localhost:9189");
            s.Open(); 

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    public class eveShout : IShout
    {
        public String Broadcast(String message)
        {
            return message + " reply";
        }
    } 

    [ServiceContract]
    public interface IShout
    {
        [OperationContract]
        String Broadcast(String message);
    } 
}

看看您能否获得像此工作一样简单的东西。 这至少会向您证明可以做到,而且问题出在别的地方。

启用WCF调试。

最简单的方法是使用WCF服务配置编辑器。 打开实用程序,然后浏览以打开应用程序的配置文件。 在诊断部分中,只需单击“启用跟踪”。 默认跟踪将很好。

运行应用程序后,框架会将日志文件转储到配置文件中指定的位置。 双击以将其打开并通读红色事件(这些事件具有异常或意外结果)。 这非常有帮助,应该可以帮助您确定问题出在哪里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM