我有一个自托管的C#WCF .Net 4.6.1 Windows服务,该服务与另一个自托管的WCF服务进行通信。当两个服务都在同一台服务器上时,这工作正常。但是,当我将服务器移至另一台计算机时,出现此错误:
System.ServiceModel.CommunicationException:套接字连接已中止。这可能是由于处理您的消息时出错,远程主机超出了接收超时或潜在的网络资源问题引起的。两台计算机上都没有运行防火墙,并且在使用http://192.168.1.129:6253/eTutorWcfService(在应用程序中使用net.tcp)时得到响应。
客户app.config
:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IeTutorMessage" />
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding_IeTutorMessage" />
</netTcpBinding>
</bindings>
<client>
<endpoint name="BasicHttpBinding_IeTutorMessage"
address="http://localhost:6253/eTutorWcfService"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IeTutorMessage"
contract="eTutorServiceReference.IeTutorMessage" />
<endpoint name="NetTcpBinding_IeTutorMessage"
address="net.tcp://localhost:6254/eTutorWcfService"
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IeTutorMessage"
contract="eTutorServiceReference.IeTutorMessage" >
<identity>
<servicePrincipalName value = ""/>
</identity>
</endpoint>
</client>
服务器app.config
:
<services>
<service name="eTutorServer.eTutorWcfService"
behaviorConfiguration="myeTutorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:6253/eTutorWcfService"/>
<add baseAddress="net.tcp://localhost:6254/eTutorWcfService"/>
</baseAddresses>
</host>
<endpoint
address="http://localhost:6253/eTutorWcfService"
binding="basicHttpBinding"
contract="eTutorServer.IeTutorMessage" />
<endpoint
address="net.tcp://localhost:6254/eTutorWcfService"
binding="netTcpBinding"
contract="eTutorServer.IeTutorMessage" />
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
<endpoint
address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="myeTutorServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
客户端代码:
EndpointAddress address = new EndpointAddress("net.tcp://" + eTutorServiceIp + ":6254/eTutorWcfService");
eTutorServiceReference.IeTutorMessageClient client = new eTutorServiceReference.IeTutorMessageClient("NetTcpBinding_IeTutorMessage", address);
try
{
rtn = client.eTutorMessage(itm);
client.Close();
}
当客户端尝试连接时,服务器的输出窗口显示SecurityTokenValidationException
,但是我不确定该怎么办或不确定是否有意义。我确定这与安全性有关,但我不知道在哪里添加内容。
答案 0 :(得分:1)
首先,Nettcpbinding使用传输安全模式,默认情况下使用Windows凭据对客户端进行身份验证。
WCF throws exception that the server has rejected the client credentials, what is the default security mode for NetTCP in WCF
然后,当我们更改服务器配置并重新托管服务时,应在调用它时重新生成客户端代理类。此外,由于Localhost是默认生成的,因此我们可能需要在客户端配置中更改端点地址。
我可以忍受,但真的很想知道如何去做 没有安全感。
最后,当我们将安全性更改为“无”时,客户端不需要提供凭据即可调用服务。我建议您重新托管服务并重新生成客户端代理类。我做了一个演示,希望它对您有用。
服务器端(控制台应用程序)
class Program
{
static void Main(string[] args)
{
using (ServiceHost sh=new ServiceHost(typeof(MyService)))
{
sh.Opened += delegate
{
Console.WriteLine("Service is ready......");
};
sh.Closed += delegate
{
Console.WriteLine("Service is closed");
};
sh.Open();
Console.ReadLine();
sh.Close();
}
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string SayHello();
}
public class MyService : IService
{
public string SayHello()
{
return "Hello Stranger";
}
}
App.config
<system.serviceModel>
<services>
<service behaviorConfiguration="Service1Behavior" name="VM1.MyService">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="mybinding" contract="VM1.IService" >
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:13007/"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="mybinding">
<security mode="None">
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="Service1Behavior">
<serviceMetadata />
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
客户端。
ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
try
{
Console.WriteLine(client.SayHello());
}
catch (Exception)
{
throw;
}
App.config
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IService">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<!--we may need to change the generated endpoint address to autual server IP address.-->
<endpoint address="net.tcp://10.157.13.69:13007/" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IService" contract="ServiceReference1.IService"
name="NetTcpBinding_IService" />
</client>
</system.serviceModel>
请随时告诉我是否有什么可以帮忙的。
答案 1 :(得分:0)
我添加了以下代码,它可以正常工作:
access denied ("java.io.FilePermission" "/globalSearchElkData/nodes/0" "write")
但是,我想在没有安全性的情况下执行此操作,因为它将被放置在多个服务器上,而这些服务器都没有公共IP。我尝试添加到绑定中,但是在客户端上这不是有效的节点,而在服务器上,它将停止启动服务。我试图将以下代码添加到服务器,但无法打开ServiceHost:
client.ClientCredentials.Windows.ClientCredential.UserName = runAs;
client.ClientCredentials.Windows.ClientCredential.Password = runAsPassword;
client.ClientCredentials.Windows.ClientCredential.Domain = runAsDomain;
我可以忍受这个,但是我真的很想知道如何在没有安全的情况下做到这一点。