使用WCF http通过LAN进行通信

时间:2013-05-05 13:01:36

标签: c# wcf http c#-4.0 distributed-computing

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

// 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));
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

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