WCF 删除默认响应 [英] WCF remove the default response
问题描述
我有一个 WCF 服务,它的默认响应如下所示:
I have a WCF service whose default response looks like this:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body/>
</s:Envelope>
我希望它完全不同,例如
I want it to be totally different, e.g.
<?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)
.
I tried creating the response manually as a string and then added Response.Write(xml)
.
问题是默认响应(第一个)也被发送到客户端,所以我得到了两个响应.
The problem is that the default response (the first one) is also sent to the client so I get both responses.
如何阻止 WCF 发送不需要的响应?
How do i stop the WCF sending that unwanted response?
推荐答案
更好的方法是替换 实施IDispatchMessageInspector 并修改 BeforeSendReply 中的消息 - 使用 您提到的示例中的类:
A better approach is to replace the response by implementing IDispatchMessageInspector and modifying the message in BeforeSendReply - using the class from 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
}
}
然后你需要创建支持类:
Then you need to create the supporting classes:
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)
{
}
}
这允许您在服务配置中声明检查器:将此文本添加到
部分(将 Your.Namespace
替换为实际的类命名空间):
This then allows you to declare the inspector in your service configuration: add this text to the <system.serviceModel>
section (replace Your.Namespace
with the actual class 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
替换为实际名称,您可以在
Finally, add this new behaviour extension to the service behaviour. You will need to replace: defaultBehaviour
with the actual name, which you will find in <services><service name="YourService" behaviorConfiguration="defaultBehaviour"> <!-- < that's the name you want -->
<behaviors>
<serviceBehaviors>
<behavior name="defaultBehaviour">
<CustomExtension/>
这篇关于WCF 删除默认响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!