我需要在IClientMessageInspector中做一些操作日志记录,并知道操作何时开始并结束至关重要。但我不能得到AfterReceiveReply单向操作,这是明确的原因。有没有办法知道在BeforeSendRequest超载中操作是单向的,所以我可以忽略它?IClientMessageInspector检测单向操作
0
A
回答
1
检查器本身没有任何信息(或传递给BeforeSendRequest的消息),但可以将此信息传递给检查器,并使用消息操作来查看操作是否是单向的。
public class StackOverflow_10354828
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string Echo(string text);
[OperationContract(IsOneWay = true)]
void Process(string input);
}
public class Service : ITest
{
public string Echo(string text)
{
return text;
}
public void Process(string input) { }
}
class MyInspector : IClientMessageInspector
{
public HashSet<string> oneWayActions;
public MyInspector(ServiceEndpoint endpoint)
{
this.oneWayActions = new HashSet<string>();
foreach (var operation in endpoint.Contract.Operations)
{
if (operation.IsOneWay)
{
oneWayActions.Add(operation.Messages[0].Action);
}
}
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
Console.WriteLine("In AfterReceiveReply");
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
Console.WriteLine("In BeginSendRequest");
if (this.oneWayActions.Contains(request.Headers.Action))
{
Console.WriteLine("This is a one-way operation");
}
return null;
}
}
class MyBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new MyInspector(endpoint));
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
host.Open();
Console.WriteLine("Host opened");
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
factory.Endpoint.Behaviors.Add(new MyBehavior());
ITest proxy = factory.CreateChannel();
proxy.Echo("Hello");
Console.WriteLine();
proxy.Process("world");
Console.WriteLine();
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
1
回复自己,一个瞬间我这样做:
布尔isOneWay = request.Headers.ReplyTo == NULL;
相关问题
- 1. 单向操作
- 2. onpropertychange检测用户操作
- 3. 检测后台操作
- 4. 传单反向wrapLatLng操作
- 5. 单元测试操作类
- 6. 检测操作系统版本JavaScript和重定向
- 7. 自定义重定向操作筛选器单元测试
- 8. jQuery - 检测操作系统和操作系统版本
- 9. 最简单的方式操作系统检测与PHP?
- 10. 操作栏检测后退按钮单击片段
- 11. MMDrawerController:检测菜单关闭操作或刷卡手势
- 12. 如何检测谁将注册表单发送给操作?
- 13. 在Clojure中检测操作系统
- 14. 如何检测拖放操作结束?
- 15. C++检测操作系统版本
- 16. 检测复制操作与inotify的
- 17. jQuery检测用户操作页
- 18. 是否有宏检测操作系统...?
- 19. 从Firefox扩展检测操作系统
- 20. 在find_or_create_by之后检测sql操作?
- 21. UIKit碰撞检测和操作
- 22. Excel检测粘贴特殊操作
- 23. 运行时间检测 - 原子操作
- 24. 未检测到按钮操作
- 25. 检测操作者支持与decltype/SFINAE
- 26. 检测操作系统语言
- 27. Silverlight 3.0中的IClientMessageInspector?
- 28. 在单向链表中检测周期
- 29. 优化简单的向量操作(python)
- 30. web服务c中的单向操作#