wcf双工回调至web,网页直接关闭的问题
契约:namespace Contract
{
[ServiceContract(Namespace = "http://www.artech.com/", SessionMode = SessionMode.Required, CallbackContract = typeof(ICallBack))]
public interface IMyContract
{
[OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)]
void Send(HD_UNI_FRM frm);
}
public interface ICallBack
{
[OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)]
void ServerReturn(HD_UNI_FRM frm);
}
}
实现契约:
public class ContractService : IMyContract
{
public void Send(SocketLibrary.HD_UNI_FRM frm)
{
try
{
MyData.callback = OperationContext.Current.GetCallbackChannel<ICallBack>();
MyData.callback.ServerReturn(frm);
}
catch (Exception e)
{
}
}
}
服务端配置文件:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="TestHost.ContractService">
<endpoint address="net.tcp://192.168.1.103:7001/ContractService"
binding="netTcpBinding" contract="Contract.IMyContract" />
</service>
</services>
</system.serviceModel>
</configuration>
web客户端调用:
public static void Request(SocketLibrary.HD_UNI_FRM frm)
{
InstanceContext instanceContext = new InstanceContext(new WCFCallBack());
DuplexChannelFactory<IMyContract> channelFactory = new DuplexChannelFactory<IMyContract>(instanceContext, "ContractService");
IMyContract proxy = channelFactory.CreateChannel();
proxy.Send(frm);
}
回调实例:
public class WCFCallBack:ICallBack
{
public void ServerReturn(SocketLibrary.HD_UNI_FRM frm)
{
WCFDelegateInvoke.InvokeCallBack(frm);
}
}
回调委托:
public delegate void CallBackDelegate(SocketLibrary.HD_UNI_FRM frm);
public class WCFDelegateInvoke
{
public static event CallBackDelegate CallBackEvent;
public static void InvokeCallBack(SocketLibrary.HD_UNI_FRM frm)
{
if (CallBackEvent != null)
{
CallBackEvent(frm);
}
}
}
web客户端配置文件:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<client>
<endpoint name="ContractService" address="net.tcp://192.168.1.103:7001/ContractService" binding="netTcpBinding" contract="Contract.IMyContract"/>
</client>
</system.serviceModel>
</configuration>
在winform中回调正常,但是在web中回调网页直接关闭,求高人指点. --------------------编程问答-------------------- 没看懂 --------------------编程问答-------------------- 没有时间 细细研究,粗略看一下。
你的webservice 我没有看出有任何问题。
检查下 你的哦回调吧。 --------------------编程问答-------------------- 写函数 测试下就可以了。 --------------------编程问答-------------------- 测试winform 通过,没问题,回调至web,服务器回调没异常抛出,但是web 直接挂了,无法检查问题在哪里,很郁闷! --------------------编程问答-------------------- 没人知道么?顶一下 --------------------编程问答-------------------- 估计是根本不支持这种方式,设想WEB客户端网页,网页是无连接的呀,不可能始终保持和服务器的连接,那么你的回调怎么能实时反映到客户端? --------------------编程问答--------------------
我也是这样想的,要想主动推送数据到web,估计没办法 --------------------编程问答-------------------- 那就要在网页端实现一个长连接。
补充:.NET技术 , C#