【发布时间】:2011-01-23 17:52:37
【问题描述】:
我在 Vista 和 Windows 7 上使用 VS 2008 SP 1 和 Silverlight 3 工具创建了一个嵌入了 Silverlight 项目的 Web 应用程序。
双工服务代码:
[ServiceContract(Namespace = "cotoco", CallbackContract = typeof(IDuplexClient))]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DuplexServer
{
private IDuplexClient _client;
private string _message;
private int _messageCounter;
private Timer _timer;
public DuplexServer()
{
}
#region IDuplexServer Members
[OperationContract(IsOneWay = true)]
public void SendEcho(string message)
{
_client = OperationContext.Current.GetCallbackChannel<IDuplexClient>();
_message = message;
_timer = new Timer();
_timer.Interval = 2000;
_timer.Enabled = true;
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
_client.ReturnEcho(string.Format("Duplex Echo {0}: {1}", _messageCounter, _message));
_messageCounter++;
if (_messageCounter > 5)
_timer.Dispose();
}
#endregion
}
[ServiceContract]
public interface IDuplexClient
{
[OperationContract(IsOneWay = true)]
void ReturnEcho(string message);
}
双工配置部分:
我在客户端应用程序中设置了一个 Duplex 服务,如下所示:
Binding svcBinding;
EndpointAddress svcAddress;
svcBinding = new PollingDuplexHttpBinding() { UseTextEncoding = true };
svcAddress = new EndpointAddress("http://localhost/CTC.Test.WCF.Server/DuplexServer.svc");
_duplex = new DuplexServerClient(svcBinding, svcAddress);
我已经同时使用了 Simplex 服务,并且对 Simplex 服务的调用命中了服务器上设置的断点,我可以单步执行。
对 Duplex 服务的调用不会到达断点,尽管客户端上没有引发错误。如果我使用 Fiddler 检查 HTTP 流量,我可以看到在初始请求之后触发了数百个 202 请求 - 我假设这些是轮询请求。
初始双工请求:
POST http://localhost/CTC.Test.WCF.Server/DuplexServer.svc HTTP/1.1 接受:/ 内容长度:665 内容类型:application/soap+xml;字符集=utf-8 UA-CPU:x86 接受编码:gzip,放气 用户代理:Mozilla/4.0(兼容;MSIE 7.0;Windows NT 6.0;(R1 1.6);SLCC1;.NET CLR 2.0.50727;InfoPath.2;.NET CLR 3.5.21022;.NET CLR 3.5.30729;. NET CLR 3.0.30618) 主机:cotocovista4.cotoco.local 连接:保持活动 Pragma: 无缓存
后续双工请求:
POST http://localhost/CTC.Test.WCF.Server/DuplexServer.svc HTTP/1.1 接受:/ 内容长度:588 内容类型:application/soap+xml;字符集=utf-8 UA-CPU:x86 接受编码:gzip,放气 用户代理:Mozilla/4.0(兼容;MSIE 7.0;Windows NT 6.0;(R1 1.6);SLCC1;.NET CLR 2.0.50727;InfoPath.2;.NET CLR 3.5.21022;.NET CLR 3.5.30729;. NET CLR 3.0.30618) 主机:cotocovista4.cotoco.local 连接:保持活动 Pragma: 无缓存
有谁知道为什么会发生这种情况,当它停止抛出连接异常时,我想我终于解决了最后一个问题......
问候
三分
更新:我尝试在以下项目类型中重新创建它:WCF 应用程序、Web 应用程序、Web 项目。我已经成功地实现了使用 WCF 消息作为双工参数的 MS 示例,但我一直在寻找一种使用 WSDL 组装双工 Web 服务的快速方法。
【问题讨论】:
标签: c# wcf silverlight duplex pollingduplexhttpbinding