簡體   English   中英

WCF使用NetTCP跨越同一網絡上的計算機

[英]WCF With NetTCP across machines on the same network

我正在嘗試在同一網絡上的多台計算機和一台服務器之間實現一些跨進程通信。 我現在正在嘗試的是使用WCF和NetTcpBinding,我的應用程序托管在同一台機器上,但是當我嘗試從另一台機器連接時,它會拋出SSPI安全錯誤。

我發現了很多這樣的跨機器的例子,但都涉及到app.config文件,我真的很想避免。 我希望能夠將此功能嵌入到沒有其他依賴項(即配置文件)的DLL中,我可以將其傳遞給所有必需的服務器地址等,它將起作用。 無論如何,僅僅在代碼中設置此安全性(通過端點等)?

我正在用以下代碼測試這一切:

服務器:

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];

      string result = new string(retVal);
      Console.WriteLine(value + " -> " + result);
      return result;
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
        var uri = "net.tcp://" + System.Net.Dns.GetHostName() + ":9985";
        Console.WriteLine("Opening connection on: " + uri);

      using (ServiceHost host = new ServiceHost(
        typeof(StringReverser),
        new Uri[]{
          new Uri("net.tcp://" + System.Net.Dns.GetHostName() + ":9985")
        }))
      {
        host.AddServiceEndpoint(typeof(IStringReverser),
          new NetTcpBinding(),
          "TcpReverse");

        host.Open();

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

        host.Close();
      }
    }
  }
}

客戶:

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)
    {
        var ep = "net.tcp://SERVER:9985/TcpReverse";
      ChannelFactory<IStringReverser> pipeFactory =
        new ChannelFactory<IStringReverser>(
          new NetTcpBinding(),
          new EndpointAddress(
            ep));

      IStringReverser pipeProxy =
        pipeFactory.CreateChannel();

      Console.WriteLine("Connected to: " + ep);
      while (true)
      {
        string str = Console.ReadLine();
        Console.WriteLine("pipe: " + 
          pipeProxy.ReverseString(str));
      }
    }
  }
}

通常在綁定上配置安全性。 您正在使用NetTcpBinding及其默認值,這意味着啟用了Transport安全性。

在服務器和客戶端上,您應該將NetTcpBinding實例分配給本地變量,以便您可以更改安全性(可能還有其他)設置,然后在調用AddServiceEndpoint或創建ChannelFactory時使用該變量。

樣品:

var binding = new NetTcpBinding();
// disable security:
binding.Security.Mode = SecurityMode.None;

這可能是您的服務正在運行的SPN的問題。 它很可能是一個機器帳戶而不是域帳戶。 這個帖子中有更多信息。

更新:有關於以編程方式設置SPN的信息,但它只是點擊了幾下...這里是一個直接鏈接 (參見頁面的最后一部分)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM