0

我使用 wsDualHttpBinding 创建了一个简单的回调服务,并尝试使用 ASP.net Web 应用程序调用它,但我的问题是回调事件没有被命中。我已经使用控制台客户端进行了尝试,并且可以正常工作。请帮助我我在做什么错误。我是 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);
    }

}

应用程序配置

<?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 代码

<?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>
4

1 回答 1

0

你用过

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

请改成

Response.Write("Temp dropped to {0} : " + temp);
于 2013-01-22T16:59:12.703 回答