2015-10-21 145 views
1

我有一個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發送不需要的響應?

+0

發現語義上,在第二響應唯一的差別是標籤methodResponse和其內部的XML。你想添加這個標籤,或者你需要命名命名空間前綴是完全一樣的嗎? –

+0

我只需要用包含首選XML的逐字字符串來替換默認的XML數據。我不需要更改名稱空間和標題。可能嗎? –

+0

我跟着一些教程,並帶着這個http://ideone.com/bFbnGk但是我不知道如何使它工作(可能我不知道如何將檢查器添加到web.config),我仍然得到默認響應。 THX –

回答

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

嗯,我把這種消息類型的方法?我甚至不確定那些是我的自定義XML,它是字符串類型(逐字)的變量,例如 –

+0

var xml = @「<?xml version ='1.0'encoding ='UTF-8'?> 「; –

+0

@ElizabethDimova看到編輯答案 – stuartd