WCF和netTcpBinding-远程客户端连接到服务的问题.本地客户很好. [英] WCF and netTcpBinding - Problems with remote clients connecting to Service. Local client are fine.
问题描述
我有一个WCF客户端/服务.当我从一台机器运行服务并从同一台机器运行客户端时,所有功能都可以完美运行.客户端连接到服务.我可以请求信息并从服务中获取回调.一旦我尝试连接 来自其他计算机的客户端,我在身份验证/安全性方面遇到错误.任何帮助将不胜感激.
I have a WCF client/service. When I run the service from a machine and run the client from the same machine all functions perfectly. The client connects to the service. I can request information and get callbacks from the service. As soon as I try and connect a client from a different machine I get errors with authentication/security. Any help would be appreciated.
以下是服务代码:
Here is the Service Code:
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Timers;
namespace TheService
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, IncludeExceptionDetailInFaults = true)]
public class MyService : IMyService
{
List<MDConnection> m_MDConnections = new List<MDConnection>();
List<IMyServiceCallback> clients = new List<IMyServiceCallback>();
public bool WSLogin(string MachineName)
{
MDConnection c = new MDConnection()
{
MachineName = MachineName,
Callback = OperationContext.Current.GetCallbackChannel<IMyServiceCallback>()
};
if (m_MDConnections.Exists(x=>x.MachineName == MachineName))
{
Console.WriteLine("> Workstation {0} is already logged in so a new session has not been created.", MachineName);
return false;
}
m_MDConnections.Add(c);
c = null;
Console.WriteLine("> Workstation {0} has logged in and a session has been created at {1}", MachineName, DateTime.Now);
return true;
}
public bool WSLogout(String MachineName)
{
if (!m_MDConnections.Exists(x=>x.MachineName == MachineName))
{
Console.WriteLine("> Workstation {0} does not have a valid session.", MachineName);
return false;
}
m_MDConnections.RemoveAll(x => x.MachineName == MachineName);
Console.WriteLine("> Workstation {0} has logged out and a session has been destroyed at {1}", MachineName, DateTime.Now);
return true;
}
public int NumberOfWorkstations()
{
return m_MDConnections.Count();
}
}
}
using System;
using System.ServiceModel;
namespace TheService
{
[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
public interface IMyService
{
[OperationContract]
bool WSLogin(string MachineName);
[OperationContract]
bool WSLogout(string MachineName);
[OperationContract]
int NumberOfWorkstations();
}
public interface IMyServiceCallback
{
[OperationContract]
void OnCallback();
}
}
using System;
namespace TheService
{
public class MDConnection
{
public string MachineName
{
get;
set;
}
public IMyServiceCallback Callback
{
get;
set;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="TheService.MyService">
<endpoint address="" binding="netTcpBinding" contract="TheService.IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:19919" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace TheService
{
public class Program
{
static void Main(string[] args)
{
var host = new ServiceHost(typeof(MyService));
host.Open();
Console.WriteLine("Service started at {0}", DateTime.Now);
Console.WriteLine("Press key to stop the service.");
Console.ReadLine();
host.Close();
}
}
}
现在提供客户代码
Now the Client code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using TheService;
namespace Client
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press enter to continue once service is hosted.");
Console.ReadLine();
MyServiceCallback callback = new MyServiceCallback();
InstanceContext instanceContext = new InstanceContext(callback);
NetTcpBinding myBinding = new NetTcpBinding();
EndpointAddress myEndpoint = new EndpointAddress("net.tcp://192.168.1.241:19919/");
DuplexChannelFactory<IMyService> myChannelFactory = new DuplexChannelFactory<IMyService>(instanceContext, myBinding, myEndpoint);
var Client = myChannelFactory.CreateChannel();
while (true)
{
Console.WriteLine("[1] Workstation Login");
Console.WriteLine("[2] Workstation Logout");
Console.WriteLine("[3] Number of Workstations");
Console.Write("> ");
string input = Console.ReadLine();
switch (input)
{
case "1":
Client.WSLogin(Environment.MachineName);
break;
case "2":
Client.WSLogout(Environment.MachineName);
break;
case "3":
Console.WriteLine("> Response: " + Client.NumberOfWorkstations());
break;
case "0":
Environment.Exit(0);
break;
default:
break;
}
}
}
}
}
using System;
using TheService;
namespace Client
{
public class MyServiceCallback : IMyServiceCallback
{
public void OnCallback()
{
Console.WriteLine("> Received callback at {0}", DateTime.Now);
}
}
}
感谢您的光临.
推荐答案
https://msdn.microsoft.com/zh-CN/library/aa347704(v = vs.110).aspx
https://msdn.microsoft.com/en-us/library/aa347704(v=vs.110).aspx
也许正在使用Windows身份验证,无法对客户端计算机进行身份验证,因为Windows域上没有任何内容,因此客户端提供的凭据不是用户凭据,而是托管WCF服务的计算机.
Maybe, Windows Authentication is being used, the client machine can't be authenticated, because nothing is on a Windows Domain, or user credentials presented by the client are not credentials known be the machine hosting the WCF service.
这篇关于WCF和netTcpBinding-远程客户端连接到服务的问题.本地客户很好.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!