C# 具有安全模式的WCF over net.tcp无异常
我们正在使用回调与WCF进行数据流传输。 WCF托管在IIS中(默认设置,只是将net.tcp添加到协议中) 我们正在尝试禁用WCF上的安全性,因此我已将客户端和服务器上的安全模式设置为“无”,但我得到以下CommunicationException: 套接字连接已中止。这可能是由错误引起的 正在处理您的消息,或接收超时被 远程主机或底层网络资源问题。本地插座 超时为“00:00:59.8439844” 我对服务进行了跟踪,TraceViewer给了我以下异常(System.ServiceModel.ProtocolException): 在以下位置需要流安全性: ,但没有安全保障 谈判背景。这可能是由远程端点引起的 其绑定中缺少StreamSecurityBindingElement 客户端程序。cs:C# 具有安全模式的WCF over net.tcp无异常,c#,.net,wcf,security,net.tcp,C#,.net,Wcf,Security,Net.tcp,我们正在使用回调与WCF进行数据流传输。 WCF托管在IIS中(默认设置,只是将net.tcp添加到协议中) 我们正在尝试禁用WCF上的安全性,因此我已将客户端和服务器上的安全模式设置为“无”,但我得到以下CommunicationException: 套接字连接已中止。这可能是由错误引起的 正在处理您的消息,或接收超时被 远程主机或底层网络资源问题。本地插座 超时为“00:00:59.8439844” 我对服务进行了跟踪,TraceViewer给了我以下异常(System.ServiceMod
using System;
using System.ServiceModel;
using WcfClient.ServiceReference1;
class Program {
static void Main() {
Callback callback = new Callback();
InstanceContext context = new InstanceContext(callback);
Service1Client service1Client = new Service1Client(context,
"NetTcpBinding_IService1");
service1Client.GetData(0);
Console.Read();
service1Client.Stop();
}
}
internal class Callback : IService1Callback {
public void StreamSignalData(int[] result) {
foreach (int i in result) {
Console.WriteLine(DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff") +
": " + i);
}
}
}
客户端App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IService1">
<security mode="None" />
</binding>
</netTcpBinding>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IService1">
<reliableSession ordered="true" />
<security mode="None" />
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/wcf-error/Service1.svc"
binding="wsDualHttpBinding"
bindingConfiguration="WSDualHttpBinding_IService1"
contract="ServiceReference1.IService1"
name="WSDualHttpBinding_IService1" />
<endpoint address="net.tcp://localhost/wcf-error/Service1.svc"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService1"
contract="ServiceReference1.IService1" name="NetTcpBinding_IService1">
</endpoint>
</client>
</system.serviceModel>
</configuration>
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation targetFramework="4.0" debug="true"/>
</system.web>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding" >
<security mode="None" />
</binding>
</netTcpBinding>
<wsDualHttpBinding>
<binding name="wsDualHttpBinding">
<security mode="None" />
</binding>
</wsDualHttpBinding>
</bindings>
<protocolMapping>
<add scheme="http" binding="wsDualHttpBinding"/>
</protocolMapping>
<services>
<service name="Service">
<endpoint address="" binding="wsDualHttpBinding"
bindingConfiguration="wsDualHttpBinding"
name="EndPointHTTP" contract="WcfService1.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" name="mex"
contract="IMetadataExchange" />
<endpoint binding="netTcpBinding" bindingConfiguration="netTcpBinding"
name="EndPointTCP" contract="WcfService1.IService1" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<useRequestHeadersForMetadataAddress />
<dataContractSerializer />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
WCF Service1.svc.cs
using System;
using System.Net.Security;
using System.ServiceModel;
using System.Threading;
[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof(IStreamCallback),
ProtectionLevel = ProtectionLevel.None)]
public interface IService1 {
[OperationContract]
void GetData(int value);
[OperationContract]
void Stop();
}
public interface IStreamCallback {
[OperationContract(IsOneWay = true)]
void StreamSignalData(int[] result);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1 {
private Timer _timer;
private readonly IStreamCallback _callback =
OperationContext.Current.GetCallbackChannel<IStreamCallback>();
public void GetData(int value) {
_timer = new Timer(StreamData, null, 0, 500);
}
public void Stop() {
_timer.Dispose();
}
private void StreamData(object state) {
int[] randomNumbers = new int[50];
Random random = new Random();
for (int i = 0; i < 50; i++) {
randomNumbers[i] = random.Next(100);
}
_callback.StreamSignalData(randomNumbers);
}
}
using System;
using System.Net.Security;
using System.ServiceModel;
using System.Threading;
namespace WcfService1
{
[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof (IStreamCallback),
ProtectionLevel = ProtectionLevel.None)]
public interface IService1
{
[OperationContract]
void GetData(int value);
[OperationContract]
void Stop();
}
public interface IStreamCallback
{
[OperationContract(IsOneWay = true)]
void StreamSignalData(int[] result);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1
{
private Timer _timer;
private readonly IStreamCallback _callback =
OperationContext.Current.GetCallbackChannel<IStreamCallback>();
public void GetData(int value)
{
_timer = new Timer(StreamData, null, 0, 500);
}
public void Stop()
{
_timer.Dispose();
}
private void StreamData(object state)
{
int[] randomNumbers = new int[50];
Random random = new Random();
for (int i = 0; i < 50; i++)
{
randomNumbers[i] = random.Next(100);
}
_callback.StreamSignalData(randomNumbers);
}
}
}
使用系统;
使用System.Net.Security;
使用System.ServiceModel;
使用系统线程;
[ServiceContract(SessionMode=SessionMode.Required,
CallbackContract=typeof(IStreamCallback),
ProtectionLevel=ProtectionLevel.None)]
公共接口IService1{
[经营合同]
void GetData(int值);
[经营合同]
无效停止();
}
公共接口IStreamCallback{
[运营合同(IsOneWay=true)]
无效流信号数据(int[]结果);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
[CallbackBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)]
公共类服务1:IService1{
私人定时器;
私有只读IStreamCallback\u回调=
OperationContext.Current.GetCallbackChannel();
public void GetData(int值){
_计时器=新计时器(StreamData,null,0,500);
}
公共停车场(){
_timer.Dispose();
}
私有void StreamData(对象状态){
int[]随机数=新的int[50];
随机=新随机();
对于(int i=0;i<50;i++){
随机数[i]=随机数。下一个(100);
}
_callback.StreamSignalData(随机数);
}
}
WCF Web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IService1">
<security mode="None" />
</binding>
</netTcpBinding>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IService1">
<reliableSession ordered="true" />
<security mode="None" />
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/wcf-error/Service1.svc"
binding="wsDualHttpBinding"
bindingConfiguration="WSDualHttpBinding_IService1"
contract="ServiceReference1.IService1"
name="WSDualHttpBinding_IService1" />
<endpoint address="net.tcp://localhost/wcf-error/Service1.svc"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService1"
contract="ServiceReference1.IService1" name="NetTcpBinding_IService1">
</endpoint>
</client>
</system.serviceModel>
</configuration>
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation targetFramework="4.0" debug="true"/>
</system.web>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding" >
<security mode="None" />
</binding>
</netTcpBinding>
<wsDualHttpBinding>
<binding name="wsDualHttpBinding">
<security mode="None" />
</binding>
</wsDualHttpBinding>
</bindings>
<protocolMapping>
<add scheme="http" binding="wsDualHttpBinding"/>
</protocolMapping>
<services>
<service name="Service">
<endpoint address="" binding="wsDualHttpBinding"
bindingConfiguration="wsDualHttpBinding"
name="EndPointHTTP" contract="WcfService1.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" name="mex"
contract="IMetadataExchange" />
<endpoint binding="netTcpBinding" bindingConfiguration="netTcpBinding"
name="EndPointTCP" contract="WcfService1.IService1" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<useRequestHeadersForMetadataAddress />
<dataContractSerializer />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
你知道这是什么吗?
谷歌上的大多数点击都说客户端和服务器上的配置应该是相同的(即安全性为无),但我似乎找不到错误。
用谷歌搜索StreamSecurityBindingElement没有找到好的解释…我无法复制您的问题,但我有相同的设置,几乎没有注意到什么: 在WCF Service1.svc.cs中创建名称空间
namespace WcfService1
{
[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof (IStreamCallback),
ProtectionLevel = ProtectionLevel.None)]
public interface IService1
{
.....
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public
class Service1 : IService1
{
应该是
。服务名称是服务的完整类型
配置中的服务部分:
<services>
<service name="WcfService1.Service1">
<endpoint address="" binding="wsDualHttpBinding"
bindingConfiguration="wsDualHttpBinding"
name="EndPointHTTP" contract="WcfService1.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" name="mex"
contract="IMetadataExchange" />
<endpoint binding="netTcpBinding" bindingConfiguration="netTcpBinding"
name="EndPointTCP" contract="WcfService1.IService1" />
</service>
</services>
WCF Service1.svc.cs
using System;
using System.Net.Security;
using System.ServiceModel;
using System.Threading;
[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof(IStreamCallback),
ProtectionLevel = ProtectionLevel.None)]
public interface IService1 {
[OperationContract]
void GetData(int value);
[OperationContract]
void Stop();
}
public interface IStreamCallback {
[OperationContract(IsOneWay = true)]
void StreamSignalData(int[] result);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1 {
private Timer _timer;
private readonly IStreamCallback _callback =
OperationContext.Current.GetCallbackChannel<IStreamCallback>();
public void GetData(int value) {
_timer = new Timer(StreamData, null, 0, 500);
}
public void Stop() {
_timer.Dispose();
}
private void StreamData(object state) {
int[] randomNumbers = new int[50];
Random random = new Random();
for (int i = 0; i < 50; i++) {
randomNumbers[i] = random.Next(100);
}
_callback.StreamSignalData(randomNumbers);
}
}
using System;
using System.Net.Security;
using System.ServiceModel;
using System.Threading;
namespace WcfService1
{
[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof (IStreamCallback),
ProtectionLevel = ProtectionLevel.None)]
public interface IService1
{
[OperationContract]
void GetData(int value);
[OperationContract]
void Stop();
}
public interface IStreamCallback
{
[OperationContract(IsOneWay = true)]
void StreamSignalData(int[] result);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1
{
private Timer _timer;
private readonly IStreamCallback _callback =
OperationContext.Current.GetCallbackChannel<IStreamCallback>();
public void GetData(int value)
{
_timer = new Timer(StreamData, null, 0, 500);
}
public void Stop()
{
_timer.Dispose();
}
private void StreamData(object state)
{
int[] randomNumbers = new int[50];
Random random = new Random();
for (int i = 0; i < 50; i++)
{
randomNumbers[i] = random.Next(100);
}
_callback.StreamSignalData(randomNumbers);
}
}
}
使用系统;
使用System.Net.Security;
使用System.ServiceModel;
使用系统线程;
命名空间WcfService1
{
[ServiceContract(SessionMode=SessionMode.Required,
CallbackContract=typeof(IStreamCallback),
ProtectionLevel=ProtectionLevel.None)]
公共接口IService1
{
[经营合同]
void GetData(int值);
[经营合同]
无效停止();
}
公共接口IStreamCallback
{
[运营合同(IsOneWay=true)]
无效流信号数据(int[]结果);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
[CallbackBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)]
公共类服务1:IService1
{
私人定时器;
私有只读IStreamCallback\u回调=
OperationContext.Current.GetCallbackChannel();
public void GetData(int值)
{
_计时器=新计时器(StreamData,null,0,500);
}
公共停车场()
{
_timer.Dispose();
}
私有void StreamData(对象状态)
{
int[]随机数=新的int[50];
随机=新随机();
对于(int i=0;i<50;i++)
{
随机数[i]=随机数。下一个(100);
}
_callback.StreamSignalData(随机数);
}
}
}
回调
确保处理_callback.StreamSignalData(随机数);当客户端断开连接时。您将收到一个通信异常。哪一方(客户端/服务)在您的日志中给了您错误?@PetarVučetin该服务。@PetarVučetin我尝试在客户端上进行跟踪,但除了它已经引发的异常之外,它没有给我任何其他信息。谢谢您的输入,但不幸的是,这并没有解决我的问题,这很奇怪。嗯-我获得了“它在我的电脑上工作”徽章: