wcf wsdualhttpbinding超时问题
请帮我!!!
我正在尝试构建一个使用回调合约的应用程序。我的应用程序在同一台计算机上的客户端和服务器上都可以正常工作,但是当我尝试从其他计算机访问服务时,我遇到了异常超时(操作 din 未在分配的超时)。客户端从服务器上的数据库中读取一些值抛出服务。我不知道我的错误在哪里。我尝试将客户端放在虚拟机中,而服务(和数据库) )在真机上。
这是我的服务器配置文件:
<binding name="TrainService" closeTimeout="00:02:00" openTimeout="00:02:00"
receiveTimeout="00:10:00" sendTimeout="00:02:00" bypassProxyOnLocal="false"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="None">
<message clientCredentialType="None" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsDualHttpBinding>
<wsHttpBinding>
<binding name="WSHttpBinding_ITrainService" closeTimeout="00:02:00"
openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="None" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8732/Design_Time_Addresses/WCFandEFService/TrainService/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITrainService"
contract="TrainServiceRef.ITrainService" name="WSHttpBinding_ITrainService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:8080/TcpService" binding="netTcpBinding"
contract="TrainServiceRef.ITrainService" name="NetTcpBinding_ITrainService" />
<endpoint address="http://localhost:8082/InterlockingService/Host/line"
binding="wsDualHttpBinding" bindingConfiguration="LineService"
contract="InterlockingServiceReference.ILineService" name="LineService">
<identity>
<userPrincipalName value="Romina-PC\Romina" />
</identity>
</endpoint>
<endpoint address="http://localhost:8082/InterlockingService/Host/trains"
binding="wsDualHttpBinding" bindingConfiguration="TrainService"
contract="InterlockingServiceReference.ITrainService" name="TrainService">
<identity>
<userPrincipalName value="Romina-PC\Romina" />
</identity>
</endpoint>
</client>
</system.serviceModel>
我的客户端配置文件:
-->
<binding name="TrainService" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="52428800" maxReceivedMessageSize="6553600"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="1638400"
maxBytesPerRead="4096" maxNameTableCharCount="1638400" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="None">
<!--<message clientCredentialType="None" negotiateServiceCredential="true"
algorithmSuite="Default" />-->
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8082/InterlockingService/Host/line"
binding="wsDualHttpBinding" bindingConfiguration="LineService"
contract="InterlockingServiceRef.ILineService" name="LineService">
<identity>
<userPrincipalName value="Romina-PC\Romina" />
</identity>
</endpoint>
<endpoint address="http://localhost:8082/InterlockingService/Host/trains"
binding="wsDualHttpBinding" bindingConfiguration="TrainService"
contract="InterlockingServiceRef.ITrainService" name="TrainService">
<identity>
<userPrincipalName value="Romina-PC\Romina" />
</identity>
</endpoint>
</client>
</system.serviceModel>
谢谢。
这些是合同:公共接口ITrainServiceCallBack { [操作合约(IsOneWay=true)] void OnTrainChangeState(TrainData 列车); }
//[ServiceContract(Name = "LineService", CallbackContract = typeof(ILineServiceCallBack))]
[ServiceContract(CallbackContract = typeof(ITrainServiceCallBack))]
public interface ITrainService
{
[OperationContract]
TrainData GetTrainData(string trainName);
[OperationContract]
bool ChangeTrainState_bool(ref TrainData train);
[OperationContract]
void ChangeTrainState(ref Trains_Detail train);
[OperationContract]
bool SubscribeToTrainChangeEvent();
[OperationContract]
bool UnSubscribeFromTrainChangeEvent();
[OperationContract]
TrainData TranslateTrainEntityToTrainData(Trains_Detail trainEntity);
[OperationContract]
IEnumerable<Trains_Detail> GetTrains();
[OperationContract]
IEnumerable<Trains_Detail> GetTrains_ByStation(int Statie_plecare, int Statie_sosire);
}
方法实现:
using System; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Runtime.Serialization; 使用 System.ServiceModel; 使用系统文本; 使用系统数据;
命名空间 WCFandEFService { // 注意:您可以使用“重构”菜单上的“重命名”命令来同时更改代码和配置文件中的类名“ProductService”。 //[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public partial class InterlockingService : ITrainService
{
static List<ITrainServiceCallBack> subscribers_train = new List<ITrainServiceCallBack>();
public bool TrainExists(string nrTren, InterlockingEntities database)
{
// Check to see whether the specified product exists in the database
int numTrain = (from t in database.Trains_Details
where string.Equals(t.Nr_tren, nrTren)
select t).Count();
return numTrain > 0;
}
public TrainData TranslateTrainEntityToTrainData(Trains_Detail trainEntity)
{
TrainData train = new TrainData();
train.nrTren=trainEntity.Nr_tren;
train.ora_Plecare= trainEntity.Ora_plecare;
train.ora_Sosire=trainEntity.Ora_sosire;
train.statie_Plecare=trainEntity.Statie_plecare;
train.statie_Sosire=trainEntity.Statie_sosire;
train.rang=trainEntity.Rang;
train.observatii=trainEntity.Observatii;
train.RowVersion=trainEntity.RowVersion;
return train;
}
#region ILineService Members
public IEnumerable<Trains_Detail> GetTrains()
{
InterlockingEntities context = new InterlockingEntities();
IEnumerable<Trains_Detail> result =
(from t in context.Trains_Details
//where l.Station == station
select t);
return result;
}
public IEnumerable<Trains_Detail> GetTrains_ByStation(int Statie_plecare,int Statie_sosire)
{
InterlockingEntities context = new InterlockingEntities();
IEnumerable<Trains_Detail> result =
(from t in context.Trains_Details
where t.Statie_plecare==Statie_plecare && t.Statie_sosire==Statie_sosire
select t);
return result;
}
public TrainData GetTrainData(string trainNr)
{
InterlockingEntities context = new InterlockingEntities();
Trains_Detail trainInDB =
(from t
in context.Trains_Details
where String.Compare(t.Nr_tren, trainNr) == 0
select t).FirstOrDefault();
if (trainInDB == null)
{
throw new Exception("No line cu numele " + trainInDB.Nr_tren);
}
context.Detach(trainInDB);
return TranslateTrainEntityToTrainData(trainInDB);
}
public bool ChangeTrainState_bool(ref TrainData train)
{
InterlockingEntities context = new InterlockingEntities();
String trainName = train.nrTren;
//int lineStation = line.station;
Trains_Detail trainInDB =
(from t
in context.Trains_Details
where String.Compare(t.Nr_tren, trainName) == 0
select t).FirstOrDefault();
if (trainInDB == null)
{
throw new Exception("No train cu numele " + trainInDB.Nr_tren);
}
context.Detach(trainInDB);
trainInDB.Nr_tren = train.nrTren;
trainInDB.Ora_plecare=train.ora_Plecare;
trainInDB.Ora_sosire=train.ora_Sosire;
trainInDB.Statie_plecare=train.statie_Plecare;
trainInDB.Statie_sosire=train.statie_Sosire;
trainInDB.Rang=train.rang;
trainInDB.RowVersion = train.RowVersion;
context.Attach(trainInDB);
context.ObjectStateManager.ChangeObjectState(trainInDB, System.Data.EntityState.Modified);
context.SaveChanges();
train.RowVersion = trainInDB.RowVersion;
context.Dispose();
raiseTrainChangeState(TranslateTrainEntityToTrainData(trainInDB));
return true;
}
public void ChangeTrainState(ref Trains_Detail train)
{
using (var context = new InterlockingEntities())
{
context.Attach(train);
context.ObjectStateManager.ChangeObjectState(train, EntityState.Modified);
context.SaveChanges();
}
}
}
public bool SubscribeToTrainChangeEvent()
{
try
{
ITrainServiceCallBack callback = OperationContext.Current.GetCallbackChannel<ITrainServiceCallBack>();
if (!subscribers_train.Contains(callback))
{
subscribers_train.Add(callback);
}
return true;
}
catch (Exception)
{
return false;
}
}
public bool UnSubscribeFromTrainChangeEvent()
{
try
{
ITrainServiceCallBack callback = OperationContext.Current.GetCallbackChannel<ITrainServiceCallBack>();
subscribers_train.Remove(callback);
return true;
}
catch (Exception)
{
return false;
}
}
#endregion
private void raiseTrainChangeState(TrainData train)
{
subscribers_train.AsParallel().ForAll(callback =>
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
callback.OnTrainChangeState(train);
}
else
{
subscribers_train.Remove(callback);
}
});
}
}
}
Please help me!!!
I am trying to build an application which uses callbacks contracts.My app work fine both the client and the server on the same machine, but when i try to access the service from other machine i got an exception timeout(the operation din not complete within the allotted timeout).The client reads some values throw the service from the database which is on the server.I don't know where is my mistake.I have tried to put the client in a virtual machine, and the service(and database) on the real machine.
Here is my Server config file:
<binding name="TrainService" closeTimeout="00:02:00" openTimeout="00:02:00"
receiveTimeout="00:10:00" sendTimeout="00:02:00" bypassProxyOnLocal="false"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="None">
<message clientCredentialType="None" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsDualHttpBinding>
<wsHttpBinding>
<binding name="WSHttpBinding_ITrainService" closeTimeout="00:02:00"
openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="None" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8732/Design_Time_Addresses/WCFandEFService/TrainService/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITrainService"
contract="TrainServiceRef.ITrainService" name="WSHttpBinding_ITrainService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:8080/TcpService" binding="netTcpBinding"
contract="TrainServiceRef.ITrainService" name="NetTcpBinding_ITrainService" />
<endpoint address="http://localhost:8082/InterlockingService/Host/line"
binding="wsDualHttpBinding" bindingConfiguration="LineService"
contract="InterlockingServiceReference.ILineService" name="LineService">
<identity>
<userPrincipalName value="Romina-PC\Romina" />
</identity>
</endpoint>
<endpoint address="http://localhost:8082/InterlockingService/Host/trains"
binding="wsDualHttpBinding" bindingConfiguration="TrainService"
contract="InterlockingServiceReference.ITrainService" name="TrainService">
<identity>
<userPrincipalName value="Romina-PC\Romina" />
</identity>
</endpoint>
</client>
</system.serviceModel>
My client config file:
-->
<binding name="TrainService" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="52428800" maxReceivedMessageSize="6553600"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="1638400"
maxBytesPerRead="4096" maxNameTableCharCount="1638400" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="None">
<!--<message clientCredentialType="None" negotiateServiceCredential="true"
algorithmSuite="Default" />-->
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8082/InterlockingService/Host/line"
binding="wsDualHttpBinding" bindingConfiguration="LineService"
contract="InterlockingServiceRef.ILineService" name="LineService">
<identity>
<userPrincipalName value="Romina-PC\Romina" />
</identity>
</endpoint>
<endpoint address="http://localhost:8082/InterlockingService/Host/trains"
binding="wsDualHttpBinding" bindingConfiguration="TrainService"
contract="InterlockingServiceRef.ITrainService" name="TrainService">
<identity>
<userPrincipalName value="Romina-PC\Romina" />
</identity>
</endpoint>
</client>
</system.serviceModel>
Thank you.
Those are the contracts: public interface ITrainServiceCallBack
{
[OperationContract (IsOneWay=true)]
void OnTrainChangeState(TrainData train);
}
//[ServiceContract(Name = "LineService", CallbackContract = typeof(ILineServiceCallBack))]
[ServiceContract(CallbackContract = typeof(ITrainServiceCallBack))]
public interface ITrainService
{
[OperationContract]
TrainData GetTrainData(string trainName);
[OperationContract]
bool ChangeTrainState_bool(ref TrainData train);
[OperationContract]
void ChangeTrainState(ref Trains_Detail train);
[OperationContract]
bool SubscribeToTrainChangeEvent();
[OperationContract]
bool UnSubscribeFromTrainChangeEvent();
[OperationContract]
TrainData TranslateTrainEntityToTrainData(Trains_Detail trainEntity);
[OperationContract]
IEnumerable<Trains_Detail> GetTrains();
[OperationContract]
IEnumerable<Trains_Detail> GetTrains_ByStation(int Statie_plecare, int Statie_sosire);
}
Method implementation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
namespace WCFandEFService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ProductService" in both code and config file together.
//[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public partial class InterlockingService : ITrainService
{
static List<ITrainServiceCallBack> subscribers_train = new List<ITrainServiceCallBack>();
public bool TrainExists(string nrTren, InterlockingEntities database)
{
// Check to see whether the specified product exists in the database
int numTrain = (from t in database.Trains_Details
where string.Equals(t.Nr_tren, nrTren)
select t).Count();
return numTrain > 0;
}
public TrainData TranslateTrainEntityToTrainData(Trains_Detail trainEntity)
{
TrainData train = new TrainData();
train.nrTren=trainEntity.Nr_tren;
train.ora_Plecare= trainEntity.Ora_plecare;
train.ora_Sosire=trainEntity.Ora_sosire;
train.statie_Plecare=trainEntity.Statie_plecare;
train.statie_Sosire=trainEntity.Statie_sosire;
train.rang=trainEntity.Rang;
train.observatii=trainEntity.Observatii;
train.RowVersion=trainEntity.RowVersion;
return train;
}
#region ILineService Members
public IEnumerable<Trains_Detail> GetTrains()
{
InterlockingEntities context = new InterlockingEntities();
IEnumerable<Trains_Detail> result =
(from t in context.Trains_Details
//where l.Station == station
select t);
return result;
}
public IEnumerable<Trains_Detail> GetTrains_ByStation(int Statie_plecare,int Statie_sosire)
{
InterlockingEntities context = new InterlockingEntities();
IEnumerable<Trains_Detail> result =
(from t in context.Trains_Details
where t.Statie_plecare==Statie_plecare && t.Statie_sosire==Statie_sosire
select t);
return result;
}
public TrainData GetTrainData(string trainNr)
{
InterlockingEntities context = new InterlockingEntities();
Trains_Detail trainInDB =
(from t
in context.Trains_Details
where String.Compare(t.Nr_tren, trainNr) == 0
select t).FirstOrDefault();
if (trainInDB == null)
{
throw new Exception("No line cu numele " + trainInDB.Nr_tren);
}
context.Detach(trainInDB);
return TranslateTrainEntityToTrainData(trainInDB);
}
public bool ChangeTrainState_bool(ref TrainData train)
{
InterlockingEntities context = new InterlockingEntities();
String trainName = train.nrTren;
//int lineStation = line.station;
Trains_Detail trainInDB =
(from t
in context.Trains_Details
where String.Compare(t.Nr_tren, trainName) == 0
select t).FirstOrDefault();
if (trainInDB == null)
{
throw new Exception("No train cu numele " + trainInDB.Nr_tren);
}
context.Detach(trainInDB);
trainInDB.Nr_tren = train.nrTren;
trainInDB.Ora_plecare=train.ora_Plecare;
trainInDB.Ora_sosire=train.ora_Sosire;
trainInDB.Statie_plecare=train.statie_Plecare;
trainInDB.Statie_sosire=train.statie_Sosire;
trainInDB.Rang=train.rang;
trainInDB.RowVersion = train.RowVersion;
context.Attach(trainInDB);
context.ObjectStateManager.ChangeObjectState(trainInDB, System.Data.EntityState.Modified);
context.SaveChanges();
train.RowVersion = trainInDB.RowVersion;
context.Dispose();
raiseTrainChangeState(TranslateTrainEntityToTrainData(trainInDB));
return true;
}
public void ChangeTrainState(ref Trains_Detail train)
{
using (var context = new InterlockingEntities())
{
context.Attach(train);
context.ObjectStateManager.ChangeObjectState(train, EntityState.Modified);
context.SaveChanges();
}
}
}
public bool SubscribeToTrainChangeEvent()
{
try
{
ITrainServiceCallBack callback = OperationContext.Current.GetCallbackChannel<ITrainServiceCallBack>();
if (!subscribers_train.Contains(callback))
{
subscribers_train.Add(callback);
}
return true;
}
catch (Exception)
{
return false;
}
}
public bool UnSubscribeFromTrainChangeEvent()
{
try
{
ITrainServiceCallBack callback = OperationContext.Current.GetCallbackChannel<ITrainServiceCallBack>();
subscribers_train.Remove(callback);
return true;
}
catch (Exception)
{
return false;
}
}
#endregion
private void raiseTrainChangeState(TrainData train)
{
subscribers_train.AsParallel().ForAll(callback =>
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
callback.OnTrainChangeState(train);
}
else
{
subscribers_train.Remove(callback);
}
});
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回调合约中的操作合约应标记为 IsOneWay = true,并且应返回 void - 并非服务中的所有操作合约。
另外,我在 NetTcpBinding 上使用双工合约时遇到了类似的问题。我通过在代理上设置 OperationTimeout 值(在我的例子中为 5 分钟)解决了这个问题。我通过在创建通道时将通道强制转换到客户端上的 IContextChannel 接口来以编程方式设置它。您应该能够在客户端配置文件中执行相同的操作。
我的代码:
The operation contract(s) in the Callback contract should be marked IsOneWay = true, and should return void - not all the operation contracts in the service.
Also, I had a similar problem using duplex contracts over NetTcpBinding. I resolved this by setting the OperationTimeout value on the proxy (in my case, to 5 minutes). I set it programatically by casting my channel to the IContextChannel interface on the client when creating the channel. You should probably be able to do the same in the client config file.
My code:
您在客户端和服务器中的端点地址指定“localhost”作为计算机名称。如果它们位于不同的计算机上,客户端将无法与服务通信。尝试将其更改为服务器所在的实际机器名称。
更新:这是一个 WSDualHttpBinding 在合约和回调合约上使用非单向操作的示例。 WSDualHttpBinding 不要求回调协定上的操作是一种方式。 PollingDuplexHttpBinding(对于 Silverlight)确实如此,但那是另一个故事了。
Your endpoint address in the client and the server specifies "localhost" as the machine name. If they're in different machines, the client won't be able to talk to the service. Try changing it to the actual machine name where the server is located.
Update: here's an example of a WSDualHttpBinding working with non-one-way operations on both contract and callback contract. WSDualHttpBinding does not require that operations on the callback contract to be one way. PollingDuplexHttpBinding (for Silverlight) does, but that's another story.
检查您的运营合同是否已标记为一种方式。当人们未能将合约标记为一种方式时,双 http 绑定通常会出现超时。
编辑
请将以下行放入服务合同的行为中。
为了供我们参考,请考虑发布您的服务合同结构和运营合同的实施。
Check if you have marked your operation contract as one way. Its usual to get a timeout in dual http binding where people fail to mark the contract as one way.
EDIT
Please place the following line in the service contract's behavior.
For our reference, consider posting your service contract structure and the operation contract's implementation.