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