C# WCF(Silverlight)双工-未命中服务器

C# WCF(Silverlight)双工-未命中服务器,c#,wcf,silverlight,duplex,pollingduplexhttpbinding,C#,Wcf,Silverlight,Duplex,Pollingduplexhttpbinding,我在Vista和Windows 7上使用VS 2008 SP 1和Silverlight 3工具创建了一个嵌入Silverlight项目的web应用程序 双工服务代码: [ServiceContract(Namespace = "cotoco", CallbackContract = typeof(IDuplexClient))] [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] [AspNetComp

我在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);
}
我同时使用了一个Simplex服务,对Simplex服务的调用命中了服务器上设置的断点,我可以单步执行

对双工服务的调用不会到达断点,尽管客户端上不会出现错误。如果我用Fiddler检查HTTP流量,我可以看到在初始请求之后,有数百个202请求被触发——我假设这些是轮询请求

初始双工请求:

POST HTTP/1.1 接受:/ 内容长度:665 内容类型:应用程序/soap+xml;字符集=utf-8 UA-CPU:x86 接受编码:gzip,deflate 用户代理: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:没有缓存

HTTP/1.1 接受:/ 内容长度:588 内容类型:应用程序/soap+xml;字符集=utf-8 UA-CPU:x86 接受编码:gzip,deflate 用户代理: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:没有缓存


http://docs.oasis-open.org/ws-rx/wsmc/200702/MakeConnectionhttp://cotocovista4.cotoco.local/CTC.Test.WCF.Server/DuplexServer.svchttp://docs.oasis-open.org/ws-rx/wsmc/200702/anonymous?id=ae3e3139-0df8-41eb-97db-69c2c48782b7你知道电话是否真的接通了,只是没有到达你的断点?(您可以通过插入一些其他副作用来测试这一点,例如登录到文本文件或类似的东西。)我问这个问题是因为我在过去遇到了完全相同的问题,在IIS中托管的WCF双工服务上的断点不会被命中,即使进行了调用。最后我不得不切换到一个自托管服务来解决这个问题。我把它作为一个bug提交给微软,他们表示它将在.NET 4.0/VS2010/Silverlight 4.0时间框架内解决。我会发布链接等,但我现在找不到


如果这是您的一个选项,并且问题只是断点没有被命中,那么我的最佳建议是要么移动到VS2010,要么移动到自托管服务。

因此-我最终使用以下web配置部分使其工作,并相应地调整客户端:

<system.serviceModel>

<extensions>
  <bindingElementExtensions>
    <add name="pollingDuplex"
         type="System.ServiceModel.Configuration.PollingDuplexElement, System.ServiceModel.PollingDuplex"/>
  </bindingElementExtensions>
</extensions>

    <bindings>
        <customBinding>
            <binding name="DuplexConfig">
                <binaryMessageEncoding/>
                <pollingDuplex maxPendingSessions="2147483647" maxPendingMessagesPerSession="2147483647" inactivityTimeout="02:00:00" serverPollTimeout="00:05:00"/>
                <httpTransport/>
            </binding>
        </customBinding>
    </bindings>

    <behaviors>
        <serviceBehaviors>
            <behavior name="CTC.Test.WCF.Server.DuplexServiceBehavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
                <serviceThrottling maxConcurrentSessions="2147483647"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>

    <services>
        <service behaviorConfiguration="CTC.Test.WCF.Server.DuplexServiceBehavior" name="CTC.Test.WCF.Server.DuplexService">
            <endpoint address="" binding="customBinding" bindingConfiguration="DuplexConfig" contract="CTC.Test.WCF.Server.IDuplexService"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>

</system.serviceModel>

如果有人能解释为什么这样做有效,但使用wsDualHttpBinding却没有,那将非常有用。:)

(还有-为什么显示在代码块中?:/)

问候


Tristan

我在DuplexServer构造函数和所有web方法中都设置了异常,并预计这会在某个地方导致严重故障,但不管怎样,它还是继续存在。不过我还没有做任何追踪。我会调查一下。好的-我添加了跟踪,它肯定不会受到影响。好的-我使用的是一个Web应用程序,并已切换到使用InstanceContextMode.SingleInstance的Web项目。现在,它会命中服务类构造函数一次(其他实例模式不会)。但它仍然没有击中成员的信息。嗯。您是否尝试过摆脱单工服务而只访问双工服务?在我当前的项目中,我曾经有过同样的安排,但是这会造成各种各样的困难,所以我把所有的事情都转移到一个单一的双工服务上,这似乎有助于解决问题。也许值得一试。是的,我已经删除了单工服务。我还尝试了各种项目——Web应用程序、Web项目和WCF项目。仍然没有成功。问题:另外,使用这种方法,它似乎保持了一个开放的连接,而不是我使用wsDualHttpBinding得到的Mass202垃圾邮件。这是正确的吗?这会占用连接池吗?有没有办法直接使用PollingDuplexBinding选项?
<system.serviceModel>

<extensions>
  <bindingElementExtensions>
    <add name="pollingDuplex"
         type="System.ServiceModel.Configuration.PollingDuplexElement, System.ServiceModel.PollingDuplex"/>
  </bindingElementExtensions>
</extensions>

    <bindings>
        <customBinding>
            <binding name="DuplexConfig">
                <binaryMessageEncoding/>
                <pollingDuplex maxPendingSessions="2147483647" maxPendingMessagesPerSession="2147483647" inactivityTimeout="02:00:00" serverPollTimeout="00:05:00"/>
                <httpTransport/>
            </binding>
        </customBinding>
    </bindings>

    <behaviors>
        <serviceBehaviors>
            <behavior name="CTC.Test.WCF.Server.DuplexServiceBehavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
                <serviceThrottling maxConcurrentSessions="2147483647"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>

    <services>
        <service behaviorConfiguration="CTC.Test.WCF.Server.DuplexServiceBehavior" name="CTC.Test.WCF.Server.DuplexService">
            <endpoint address="" binding="customBinding" bindingConfiguration="DuplexConfig" contract="CTC.Test.WCF.Server.IDuplexService"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>

</system.serviceModel>