0
我已經創建了一個簡單的回調服務使用wsDualHttpBinding並嘗試使用ASP.net Web應用程序調用它,但我的問題是回調事件沒有被擊中。我已經通過Console Client嘗試了它,並且它可以正常工作。請幫我解決我在做什麼錯誤。我是新來的ASP.netWCF wsDualHttpBinding 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();
}
}
<?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>
同樣在這裏。它在Console中工作,但不在ASP.Net中。你設法使它有效嗎?謝謝。 – TTCG