【问题标题】:WCF not working inside form classWCF 在表单类中不起作用
【发布时间】:2013-12-15 06:28:42
【问题描述】:

我按照本教程进行了基本的 WCF 进程间通信: http://tech.pro/tutorial/855/wcf-tutorial-basic-interprocess-communication

我有一个客户端和一个服务器,它们都用作控制台应用程序(如教程创建的那样)。

但是当我将 WCF 服务器代码移动到表单中时,它不起作用。

控制台应用程序服务器代码(作为控制台应用程序工作正常):

[ServiceContract]
public interface IModelData
{
    [OperationContract]
    ArrayList GetData();
}

public class ModelDataServer : IModelData
{
    public ArrayList GetData()
    {
        return new ArrayList(); // Just for testing that it works (before form version)
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (ServiceHost host = new ServiceHost(
        typeof(ModelDataServer),
        new Uri[]{
            new Uri("http://localhost:8000")
        }))
        {
            host.AddServiceEndpoint(typeof(IModelData),
                new BasicHttpBinding(),
                "ModelData");

            host.Open();

            Console.WriteLine("Service is available. " +
                "Press <ENTER> to exit.");
            Console.ReadLine();

            host.Close();
        }
    }
}

我了解到,如果您在 UI 线程上启动 ServiceHost,WCF 和 Windows 窗体就会出现问题。 running WCF in windows forms

我的表单代码:

public partial class ServerForm : Form
{
    ModelDataServer Server;
    public ScraperForm()
    {
        InitializeComponent();

        Server = new ModelDataServer(); // this should start the server?
    }

// other stuff
}

[ServiceContract]
public interface IModelData
{
    [OperationContract]
    ArrayList GetData();
}

[ServiceBehavior(UseSynchronizationContext=false)]
public class ModelDataServer : IModelData
{
    ServiceHost Host;
    public ModelDataServer()
    {
        using (ServiceHost Host = new ServiceHost(
        typeof(ModelDataServer),
        new Uri[]{
          new Uri("http://localhost:8000")
        }))
        {
            Host.AddServiceEndpoint(typeof(IModelData),
              new BasicHttpBinding(),
              "ModelData");

            Host.Open();
        }
    }

    public ArrayList GetData()
    {
        return GetData();
    }

    public void CloseServer()
    {
        Host.Close();
    }
}

我不相信我的主机在上面的示例中被垃圾收集,因为它是一个字段。我说的对吗?

从我的客户端(使用上述控制台应用程序)调用此服务器时遇到的错误如下:

There was no endpoint listening at http://localhost:8000/ModelData that could accept
the message. This is often caused by an incorrect address or SOAP action. See
InnerException, if present, for more details.

感谢您的帮助。

【问题讨论】:

  • 显示你的 web.config 代码

标签: c# forms wcf endpoint


【解决方案1】:
using (ServiceHost Host = new ServiceHost(
    typeof(ModelDataServer),
    new Uri[]{
      new Uri("http://localhost:8000")
    }))
    {
        Host.AddServiceEndpoint(typeof(IModelData),
          new BasicHttpBinding(),
          "ModelData");

        Host.Open();
    }

您正在使用块中打开您的服务主机。 Host.Open(); 语句之后没有任何内容,因此您的 using 块已退出并且主机已关闭。因此没有端点在监听。

您最好(在这种情况下)跳过using 块并明确地打开和关闭服务主机。您应该能够挂钩到应用程序结束事件之一以在应用程序退出时关闭主机。

【讨论】:

  • 因为我已经将Host设置为类的字段,所以我认为它在使用后不会被释放。谢谢。
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 2017-09-01
相关资源
最近更新 更多