如何将自定义EndPointBehavior添加到服务的web.config? [英] How to add a Custom EndPointBehavior to the web.config of the service?
问题描述
我已关注这篇文章,并创建了如下的 MyMessageInspector
和 MyEndPointBehavior
类:
I have followed this article and have created MyMessageInspector
and MyEndPointBehavior
clases as below:
public class MyMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
Console.WriteLine("Incoming request: {0}", request);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
public class MyEndPointBehavior : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
ChannelDispatcher channelDispatcher = endpointDispatcher.ChannelDispatcher;
if (channelDispatcher != null)
{
foreach (EndpointDispatcher ed in channelDispatcher.Endpoints)
{
ed.DispatchRuntime.MessageInspectors.Add(new MyMessageInspector());
}
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
如何将MyEndPointBehavior添加到web.config?
How to add MyEndPointBehavior to the web.config?
我添加了以下扩展名:
<extensions>
<behaviorExtensions>
<add name="myMessageInspector" type="MessageInspectorProject.MyEndPointBehavior, MessageInspectorProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
但是当我尝试在下面使用它时,它会抱怨:
But when I try to use it in below, it complains:
<serviceBehaviors>
<behavior>
<myMessageInspector/>
它的抱怨如下:
配置中的无效元素.扩展名"myMessageInspector"不是从正确的扩展名基本类型"System.ServiceModel.Configuration.BehaviorExtensionElement"派生的.
Invalid element in configuration. The extension 'myMessageInspector' does not derive from correct extension base type 'System.ServiceModel.Configuration.BehaviorExtensionElement'.
如何将 MyEndPointBehavior
添加到web.config?
How to add MyEndPointBehavior
to the web.config?
推荐答案
您还必须创建一个自定义 BehaviorExtensionElement 并将其用于web.config文件.有很多文章可以帮助您喜欢这些
You have to also create a custom BehaviorExtensionElement and use it in web.config file. There are many articles which can help you like these
http://weblogs.asp.net/paolopia/writing-a-wcf-message-inspector
http://cgeers.com/2011/05/10/wcf-message-logging/
http://burcakcakiroglu.com/?p=2083
http://trycatch.me/将自定义消息标题添加到wcf服务使用检查员行为/
无论如何以这种方式修复代码
Anyway fix your code to this way
public class MyMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
Console.WriteLine("Incoming request: {0}", request);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
public class MyEndPointBehavior : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
ChannelDispatcher channelDispatcher = endpointDispatcher.ChannelDispatcher;
if (channelDispatcher != null)
{
foreach (EndpointDispatcher ed in channelDispatcher.Endpoints)
{
ed.DispatchRuntime.MessageInspectors.Add(new MyMessageInspector());
}
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
在此处添加新的BehaviorExtensionElement
Here add new BehaviorExtensionElement
public class CustomBehaviorExtensionElement : BehaviorExtensionElement
{
protected override object CreateBehavior()
{
return new MyEndPointBehavior();
}
public override Type BehaviorType
{
get
{
return typeof(MyEndPointBehavior);
}
}
}
并更新您的web.config
And update your web.config
<extensions>
<behaviorExtensions>
<add name="myMessageInspector" type="MessageInspectorProject.CustomBehaviorExtensionElement, MessageInspectorProject"/>
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior>
<myMessageInspector />
</behavior>
</endpointBehaviors>
</behaviors>
这篇关于如何将自定义EndPointBehavior添加到服务的web.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!