gpt4 book ai didi

c# - 如何将自定义 EndPointBehavior 添加到服务的 web.config 中?

转载 作者:太空狗 更新时间:2023-10-29 17:35:00 26 4
gpt4 key购买 nike

我关注了this article并创建了 MyMessageInspectorMyEndPointBehavior 类,如下所示:

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 中?

我添加了以下扩展:

<extensions>
<behaviorExtensions>
<add name="myMessageInspector" type="MessageInspectorProject.MyEndPointBehavior, MessageInspectorProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>

但是当我尝试在下面使用它时,它会提示:

<serviceBehaviors>
<behavior>
<myMessageInspector/>

它的提示如下:

Invalid element in configuration. The extension 'myMessageInspector' does not derive from correct extension base type 'System.ServiceModel.Configuration.BehaviorExtensionElement'.

如何将 MyEndPointBehavior 添加到 web.config?

最佳答案

您还必须创建自定义 BehaviorExtensionElement 并在 web.config 文件中使用它。有很多文章可以帮助你喜欢这些

http://weblogs.asp.net/paolopia/writing-a-wcf-message-inspector

https://github.com/geersch/WcfMessageLogging

http://burcakcakiroglu.com/?p=2083

http://trycatch.me/adding-custom-message-headers-to-a-wcf-service-using-inspectors-behaviors/

以这种方式修改你的代码

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

public class CustomBehaviorExtensionElement : BehaviorExtensionElement
{
protected override object CreateBehavior()
{
return new MyEndPointBehavior();
}

public override Type BehaviorType
{
get
{
return typeof(MyEndPointBehavior);
}
}
}

并更新你的 web.config

<extensions>
<behaviorExtensions>
<add name="myMessageInspector" type="MessageInspectorProject.CustomBehaviorExtensionElement, MessageInspectorProject"/>
</behaviorExtensions>
</extensions>

<behaviors>
<endpointBehaviors>
<behavior>
<myMessageInspector />
</behavior>
</endpointBehaviors>
</behaviors>

关于c# - 如何将自定义 EndPointBehavior 添加到服务的 web.config 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15636874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com