WCF wsDualHttpBinding ASP.NET客户端回调不起作用

时间:2012-06-13 09:21:15

标签: asp.net

我使用wsDualHttpBinding创建了一个简单的回调服务,并尝试使用ASP.net Web应用程序调用它,但我的问题是回调事件没有被击中。我已经尝试使用Console Client,它可以正常使用它。请帮我解决我的错误。我是ASP.net的新手

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading;

[ServiceContract(CallbackContract=typeof(IClientCallback))]
public interface ITemperature
{
    [OperationContract(IsOneWay=true)]
    void RegisterForTempDrops();
}

public interface IClientCallback
{   
    [OperationContract]
    bool TempUpdate(double temp);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class Temperature : ITemperature
{
    public void RegisterForTempDrops()
    {
        OperationContext ctxt = OperationContext.Current;
        IClientCallback callBack = ctxt.GetCallbackChannel<IClientCallback>();
        Thread.Sleep(3000); //simulate update happens somewhere; for example monitoring a database field
        callBack.TempUpdate(10);
    }

}

App.Config中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Temperature" behaviorConfiguration="ServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:5050/duplexservice" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <endpoint address="http://localhost:5050/duplexservice" binding="wsDualHttpBinding" contract="ITemperature" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">       
          <serviceMetadata httpGetEnabled="true"/>       
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

运行服务的Windows窗体。

using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace DuplexService
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        internal static ServiceHost myServiceHost = null;
        private void button1_Click(object sender, EventArgs e)
        {
            myServiceHost = new ServiceHost(typeof(Temperature));
            myServiceHost.Open();
            MessageBox.Show("Service Started!");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (myServiceHost.State != CommunicationState.Closed)
            {
                myServiceHost.Close();
                MessageBox.Show("Service Stopped!");
            }
        }
    }
}

ASP.net客户端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DuplexService;
using System.ServiceModel;

public partial class _Default : System.Web.UI.Page, ITemperatureCallback
{
    static InstanceContext site = new InstanceContext(new _Default());
    static TemperatureClient proxy = new TemperatureClient(site);

    public bool TempUpdate(double temp)
    {
        Response.Redirect("Temp dropped to {0} : " + temp);
        return true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        proxy.RegisterForTempDrops();
    }
}

Web.Config Code

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <wsDualHttpBinding>
        <binding name="WSDualHttpBinding_ITemperature" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01: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="Message">
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" />
          </security>
        </binding>
      </wsDualHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:5050/duplexservice" binding="wsDualHttpBinding"
        bindingConfiguration="WSDualHttpBinding_ITemperature" contract="DuplexService.ITemperature"
        name="WSDualHttpBinding_ITemperature">
        <identity>
          <userPrincipalName value="smdunk" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

1 个答案:

答案 0 :(得分:0)

您使用了

Response.Redirect("Temp dropped to {0} : " + temp);

请将其更改为

Response.Write("Temp dropped to {0} : " + temp);