WCF删除默认响应

本文关键字:响应 默认 删除 WCF | 更新日期: 2023-09-27 18:18:51

我有一个WCF服务,它的默认响应是这样的:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body/>
</s:Envelope>

我希望它完全不同,例如

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' 
                   xmlns:xsd='http://www.w3.org/1999/XMLSchema' 
                   xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance'>
   <SOAP-ENV:Body>
       <ns1:methodResponse xmlns:ns1='urn:MyService' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
           <message href='cid:success'/>
       </ns1:methodResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我尝试手动创建响应作为字符串,然后添加Response.Write(xml)

问题是默认响应(第一个)也被发送到客户端,所以我得到两个响应。

如何阻止WCF发送不需要的响应?

WCF删除默认响应

更好的方法是通过实现替换响应IDispatchMessageInspector和修改beforeendreply中的消息—使用您提到的示例中的类:

public class CustomInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel,
    InstanceContext instanceContext)
    {
        return null;
    }
    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        reply = ChangeResponse(reply);
    }
    private Message ChangeResponse(Message oldMessage)
    {
       // change message
    }
}

然后你需要创建支持类:

public class CustomExtension : BehaviorExtensionElement
{
    protected override object CreateBehavior()
    {
        return new CustomBehavior();
    }
    public override Type BehaviorType
    {
        get
        {
            return typeof(CustomBehavior);
        }
    }
}
public class CustomBehavior : IServiceBehavior
{
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
    ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>())
        {
            foreach (var endpoint in dispatcher.Endpoints)
            {
                endpoint.DispatchRuntime.MessageInspectors.Add(new CustomInterceptor());
            }
        }
    }
    public void AddBindingParameters(ServiceDescription serviceDescription,
        ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints,
        BindingParameterCollection bindingParameters)
    {
    }
    public void Validate(ServiceDescription serviceDescription,
        ServiceHostBase serviceHostBase)
    {
    }
}

这样你就可以在你的服务配置中声明检查器了:把这段文字添加到<system.serviceModel>部分(用实际的类命名空间替换Your.Namespace):

<extensions>
  <behaviorExtensions>
    <!-- Replace default response -->
    <add name="CustomExtension"
          type="Your.Namespace.CustomExtension, Your.Namespace, 
                Version=1.0.0.0, Culture=neutral" />
  </behaviorExtensions>
</extensions>

最后,将这个新的行为扩展添加到服务行为中。您需要将:defaultBehaviour替换为实际名称,您将在<services><service name="YourService" behaviorConfiguration="defaultBehaviour"> <!-- < that's the name you want -->

中找到该名称。
<behaviors>
  <serviceBehaviors>
    <behavior name="defaultBehaviour">
      <CustomExtension/>