使用WCF http通过LAN进行通信 [英] Using WCF http to communicate over a LAN

查看:178
本文介绍了使用WCF http通过LAN进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过以太网电缆与两台电脑通信。我已经进入设置,并告诉它使用两个特定的IP地址。 Ive在两台电脑上关闭防火墙,并管理从一台PC到另一台。当我尝试和使用以下代码,它不工作。在指定的地址什么都没听。任何想法?

Im trying to communicate with two pc's via an ethernet cable. I've gone into the settings and told it to use two specific ip addresses. Ive turned the firewalls off on both pc's and managed to ping from one pc to the other. When i try and use the following code, its not working though. Something about nothing listening at the specified address. Any ideas?

// SERVER

//SERVER

using System;
using System.ServiceModel;

namespace WCFServer
{
  [ServiceContract]
  public interface IStringReverser
  {
    [OperationContract]
    string ReverseString(string value);
  }

  public class StringReverser : IStringReverser
  {
    public string ReverseString(string value)
    {
      char[] retVal = value.ToCharArray();
      int idx = 0;
      for (int i = value.Length - 1; i >= 0; i--)
        retVal[idx++] = value[i];

      return new string(retVal);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      using (ServiceHost host = new ServiceHost(
        typeof(StringReverser),
        new Uri[]{
          new Uri("http://192.168.10.10")
        }))
      {

        host.AddServiceEndpoint(typeof(IStringReverser),
          new BasicHttpBinding(),
          "Reverse");

        host.Open();

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

        host.Close();
      }
    }
  }
}

/ CLIENT

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace WCFClient
{
  [ServiceContract]
  public interface IStringReverser
  {
    [OperationContract]
    string ReverseString(string value);
  }

  class Program
  {
    static void Main(string[] args)
    {
      ChannelFactory<IStringReverser> httpFactory =
         new ChannelFactory<IStringReverser>(
          new BasicHttpBinding(),
          new EndpointAddress(
            "http://192.168.10.9"));


      IStringReverser httpProxy =
        httpFactory.CreateChannel();

      while (true)
      {
        string str = Console.ReadLine();
        Console.WriteLine("http: " + 
          httpProxy.ReverseString(str));
      }
    }
  }
}


推荐答案

您的服务正在侦听的地址是 http://192.168.10.10/Reverse (Uri您给了加上端点名称)您应该将您的客户端连接到此端点,而不是 http://192.168.10.9

The Address your service is listening to is http://192.168.10.10/Reverse (Uri you gave plus endpoint name you gave), you should connect your client to this endpoint instead of http://192.168.10.9.

这篇关于使用WCF http通过LAN进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆