我在 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);
}
双工配置部分:
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="DuplexServerBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> < serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <services> <service behaviorConfiguration="DuplexServerBehavior" name="DuplexServer"> <endpoint address="" binding="wsDualHttpBinding" contract="DuplexServer" /> <endpoint address="mex" 绑定="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> </system.serviceModel>
我在客户端应用程序中设置了一个 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 Accept: / Content-Length: 665 Content-Type: application/soap+xml; charset=utf-8 UA-CPU:x86 Accept-Encoding:gzip,deflate User-Agent: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 连接:Keep-Alive 编译指示:无缓存
<s:信封 xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">< s:Header><a:Action s:mustUnderstand="1">urn:IDuplexServer/SendEcho</a:Action><netdx:Duplex xmlns:netdx="http://schemas.microsoft.com/2008/04/ netduplex"><netdx:地址> http://docs.oasis-open.org/ws-rx/wsmc/200702/anonymous?id=ae3e3139-0df8-41eb-97db-69c2c48782b7</netdx:地址><netdx: SessionId>43f9e320-cde3-4e21-a748-e5b29c10ee25</netdx:SessionId></netdx:Duplex><a:To s:mustUnderstand="1"> http://cotocovista4.cotoco.local/CTC.Test.WCF .Server/DuplexServer.svc</a:To></s:Header><s:Body><SendEcho><message>Message!</message></SendEcho></s:Body></s:信封> ;
随后的双工请求:
POST http://localhost/CTC.Test.WCF.Server/DuplexServer.svc HTTP/1.1 Accept: / Content-Length: 588 Content-Type: application/soap+xml; charset=utf-8 UA-CPU:x86 Accept-Encoding:gzip,deflate User-Agent: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 连接:Keep-Alive 编译指示:无缓存
<s:信封 xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">< s:Header><a:Action s:mustUnderstand="1"> http://docs.oasis-open.org/ws-rx/wsmc/200702/MakeConnection</a:Action><a:To s:mustUnderstand ="1"> http://cotocovista4.cotoco.local/CTC.Test.WCF.Server/DuplexServer.svc</a:To></s:Header><s:Body><wsmc:MakeConnection xmlns:wsmc ="http://docs.oasis-open.org/ws-rx/wsmc/200702"><wsmc:地址> http://docs.oasis-open.org/ws-rx/wsmc/200702/anonymous? id=ae3e3139-0df8-41eb-97db-69c2c48782b7</wsmc:Address></wsmc:MakeConnection></s:Body></s:Envelope> ;
有谁知道为什么会发生这种情况,我以为当它停止抛出连接异常时我终于解决了最后一个问题......
问候
特里斯
更新:我尝试在以下项目类型中重新创建它:WCF 应用程序、Web 应用程序、Web 项目。我已经成功地实现了使用 WCF 消息作为双工参数的 MS 示例,但我一直在寻找一种使用 WSDL 组装双工 Web 服务的快速方法。