And*_*usa 5 c# wcf callback nettcpbinding
我在使用netTcpBinding WCF客户端/服务器进行回调时遇到了一些问题.这是代码...任何想法?
服务方
合同:
using System.Runtime.Serialization;
using System.ServiceModel;
namespace API.Interface
{
[ServiceContract(CallbackContract = typeof(IServiceCallback))]
public interface IMyService
{
[OperationContract]
void DoSomething();
}
public interface IServiceCallback
{
[OperationContract(IsOneWay = true)]
void OnCallback();
}
}
Run Code Online (Sandbox Code Playgroud)
服务:
using System;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.Timers;
using API.Interface;
namespace API.Service
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class MyService : IMyService
{
public static IServiceCallback callback;
public static Timer Timer;
public void DoSomething()
{
Console.WriteLine("> Session opened at {0}", DateTime.Now);
callback = OperationContext.Current.GetCallbackChannel<IServiceCallback>();
Timer = new Timer(1000);
Timer.Elapsed += OnTimerElapsed;
Timer.Enabled = true;
}
void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
callback.OnCallback();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我用来启动服务的代码
var service = new MyService();
// Start up the WCF API
var service = new ServiceHost(turboService);
service.Open();
Run Code Online (Sandbox Code Playgroud)
这是App.Config
<system.serviceModel>
<services>
<service name="API.Service.MyService">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="API.Interface.IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8732/MyService/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
客户端
CallbackService
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using USBAutomationTester.ServiceReference;
namespace USBAutomationTester
{
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false)]
public class CallbackService : IMyServiceCallback
{
public void OnCallback()
{
Console.WriteLine("> Received callback at {0}", DateTime.Now);
}
}
}
Run Code Online (Sandbox Code Playgroud)
连接和呼叫
var instanceContext = new InstanceContext(new CallbackService());
var service = new TurboValidateServiceClient(instanceContext);
service.DoSomething();
Run Code Online (Sandbox Code Playgroud)
App.Config中
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IMyService" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8732/MyService/"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ITurboValidateService"
contract="ServiceReference.IMyService"
name="NetTcpBinding_IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
我相信我需要所有的东西.谷歌搜索让我走上了几条不同的道路而没有真正的结果.我可以看到服务调用回调,但我的客户端永远不会得到它.
在此先感谢,我知道这是一个WCF 101类型的问题,但我很难过.
UPDATE
在客户端,我得到这个例外
"无法处理带有操作的传入消息,因为它的目标是请求 - 回复操作,但由于未设置MessageId属性,因此无法回复."
其次是
" 关闭时,频道收到了一条带有Action'http://tempuri.org/IMyService/OnCallback ' 的意外输入消息.你应该只在不期待任何输入消息时关闭你的频道."
问题可能是在计时器触发时上下文已经关闭。
public void DoSomething()
{
Console.WriteLine("> Session opened at {0}", DateTime.Now);
callback = OperationContext.Current.GetCallbackChannel<IServiceCallback>();
Timer = new Timer(1000);
Timer.Elapsed += OnTimerElapsed;
Timer.Enabled = true;
}
void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
callback.OnCallback();
}
Run Code Online (Sandbox Code Playgroud)
尝试将回调转换为ICommunicationObject并检查State属性。Open如果在您尝试时未将其设置为OnCallback,那就是您的问题。
| 归档时间: |
|
| 查看次数: |
9273 次 |
| 最近记录: |