WCF删除默认响应

时间:2015-10-21 16:29:39

标签: c# wcf

我有一个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发送不需要的响应?

1 个答案:

答案 0 :(得分:1)

更好的方法是替换响应implementing IDispatchMessageInspector and modifying the message in BeforeSendReply - 使用the example you mentioned中的类:

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/>