您可以使用OperationContext.IncomingMessageProperties 属性。这是您的场景中的用法:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using Demo.MessageInspector;
using Demo.Utilities;
namespace Demo.WebServices
{
public class MyWebService : IMyWebService, IServiceBehavior
{
public void MyWebServiceMethod()
{
// get the activityId from the incoming message properties
var activityIdProperty = OperationContext.Current.IncomingMessageProperties
.FirstOrDefault(property => property.Key == Properties.ActivityId.ToString());
// create an empty Guid
Guid activityId = new Guid();
if (activityIdProperty.Value != null)
{
// replace the empty Guid with the activityId
activityId = (Guid)activityIdProperty.Value;
}
bool success = DoSomething();
if (!success)
MyLog.Message("Error happened in MyWebServiceMethod", activityId);
}
private bool DoSomething()
{
// TODO: implement
return false;
}
public void AddBindingParameters(
ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase,
System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
BindingParameterCollection bindingParameters
)
{
return;
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
{
var messageInspector = new DemoMessageInspector();
epDisp.DispatchRuntime.MessageInspectors.Add(messageInspector);
}
}
}
public void Validate(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase)
{
// TODO: implement validation
//throw new NotImplementedException();
}
}
}
namespace Demo.MessageInspector
{
public class DemoMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request,
IClientChannel channel, InstanceContext instanceContext)
{
Guid activityId = Guid.NewGuid();
// add the activityId to the incoming message properties
OperationContext.Current.IncomingMessageProperties
.Add(Properties.ActivityId.ToString(), activityId);
MyLog.Message("AfterReceiveRequest", activityId);
return activityId;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
Guid activityId = (Guid)correlationState;
MyLog.Message("BeforeSendReply", activityId);
}
}
}
namespace Demo.Utilities
{
public enum Properties
{
ActivityId
}
public class MyLog
{
internal static void Message(string p, Guid guid)
{
File.AppendAllText(@"C:\Temp\log.txt",
String.Format("{0} {1} {2}\r\n", DateTime.Now, p, guid));
}
}
}
[更新]
或者,您可以使用CorrelationManager.ActivityId 属性。为此,首先将以下内容添加到您的配置文件(客户端和服务器):
<system.diagnostics>
<sources>
<source name="System.ServiceModel" propagateActivity="true">
<listeners>
<add name="ignored" type="System.Diagnostics.DefaultTraceListener" />
</listeners>
</source>
</sources>
</system.diagnostics>
然后,在类中添加以下方法Demo.Utilities.Helper
:
namespace Demo.Utilities
{
internal class Helper
{
internal static Guid GetCorrelationId()
{
var headerPosition = OperationContext.Current.IncomingMessageHeaders.FindHeader("ActivityId",
"http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics");
if (headerPosition > -1)
{
var activityIdHeader = OperationContext.Current.IncomingMessageHeaders
.GetReaderAtHeader(headerPosition);
var activityIdAttribute = activityIdHeader.GetAttribute("CorrelationId");
return Guid.Parse(activityIdAttribute);
}
else
{
return Guid.Empty;
}
}
}
}
现在你可以在下面使用这样的方法DemoMessageInspector
:
public object AfterReceiveRequest(ref Message request,
IClientChannel channel, InstanceContext instanceContext)
{
var correlationId = Helper.GetCorrelationId();
MyLog.Message("AfterReceiveRequest\tCorrelationId", correlationId);
// ...
}
在您的服务方法中像这样:
public void MyWebServiceMethod()
{
var correlationId = Helper.GetCorrelationId();
MyLog.Message("MyWebServiceMethod\tCorrelationId", correlationId);
// ...
}