我需要在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中的單向操作#