2011-01-20 170 views
1

我正在創建一個調用另一個Web服務的WCF Rest服務。期望的結果是我的Web服務返回不受來自其他Web服務的HttpWebResponse的影響。例如:WCF Rest服務返回HttpWebResponse

[OperationContract] 
[WebInvoke(Method = "GET", 
    ResponseFormat = WebMessageFormat.Xml, 
    UriTemplate = "DoSomething?variable={variable}", 
    BodyStyle = WebMessageBodyStyle.Bare)] 
    HttpWebResponse DoSomething(string variable); 

public HttpWebResponse DoSomething(string variable) 
{ 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(otherServicerequestUrl); 
    return (HttpWebResponse)request.GetResponse(); 
} 

這可能嗎?

+0

我想沒有。但是你可以嘗試使用System.ServiceModel.Channels.Message類。在SOAP服務中,它完全用於此目的,但我不知道它是否適用於REST。 – 2011-01-20 23:19:25

回答

0

在wcf.codeplex.com上籤出新的WCF Http堆棧。目前你可以做類似的事情,在未來的版本中,他們正在計劃一些解決你想要做的事情。

您目前可以使用WCF Http堆棧執行以下操作。

public void DoSomething(HttpRequestMessage request, HttpResponseMessage response) 
{ 
} 
0

您可以創建自己的路由服務。 創建合同的所有路由服務的接口( 「*」)

[ServiceContract] 
public interface IRoutingService 
{ 

    [WebInvoke(UriTemplate = "")] 
    [OperationContract(AsyncPattern = true, Action = "*", ReplyAction = "*")] 
    IAsyncResult BeginProcessRequest(Message requestMessage, AsyncCallback asyncCallback, object asyncState); 

    Message EndProcessRequest(IAsyncResult asyncResult); 

} 

然後在服務

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, 
    AddressFilterMode = AddressFilterMode.Any, ValidateMustUnderstand = false)] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class RoutingService : IRoutingService, IDisposable 
    { 
     private IRoutingService _client; 

     public IAsyncResult BeginProcessRequest(Message requestMessage, AsyncCallback asyncCallback, object asyncState) 
     { 

      ClientSection clientSection = (ClientSection)ConfigurationManager.GetSection("system.serviceModel/client"); 
      string clientEndPoint = clientSection.Endpoints[0].Address.AbsoluteUri; 
      string methodCall = requestMessage.Headers.To.AbsolutePath.Substring(requestMessage.Headers.To.AbsolutePath.IndexOf(".svc") + 4); 

      Uri uri = new Uri(clientEndPoint + "/" + methodCall); 
      EndpointAddress endpointAddress = new EndpointAddress(uri); 
      WebHttpBinding binding = new WebHttpBinding("JsonBinding"); 
      var factory = new ChannelFactory<IRoutingService>(binding, endpointAddress); 

      // Set message address 
      requestMessage.Headers.To = factory.Endpoint.Address.Uri; 

      // Create client channel 
      _client = factory.CreateChannel(); 

      // Begin request 
      return _client.BeginProcessRequest(requestMessage, asyncCallback, asyncState); 
     } 

     public Message EndProcessRequest(IAsyncResult asyncResult) 
     { 
      Message message = null; 
      try 
      { 
       message = _client.EndProcessRequest(asyncResult); 
      } 
      catch(FaultException faultEx) 
      { 
       throw faultEx; 
      } 
      catch(Exception ex) 
      { 
       ServiceData myServiceData = new ServiceData(); 
       myServiceData.Result = false; 
       myServiceData.ErrorMessage = ex.Message; 
       myServiceData.ErrorDetails = ex.Message; 
       throw new FaultException<ServiceData>(myServiceData, ex.Message); 
      } 
      return message; 
     } 

     public void Dispose() 
     { 
     if (_client != null) 
      { 
       var channel = (IClientChannel)_client; 
       if (channel.State != CommunicationState.Closed) 
       { 
        try 
        { 
         channel.Close(); 
        } 
        catch 
        { 
         channel.Abort(); 
        } 
       } 
      } 
     } 
    } 

實現這一點,並做web.config配置

<services> 
    <service name="RoutingService" behaviorConfiguration="JsonRoutingBehave"> 
    <endpoint address="" binding="webHttpBinding" contract="IRoutingService" name="serviceName" > 
     <identity> 
     <dns value="localhost" /> 
     </identity> 
    </endpoint> 
    </service> 
</services> 
<client> 
    <endpoint address="<actual service url here>" binding="webHttpBinding" bindingConfiguration ="JsonBinding" contract="*" name="endpointName"> 
    <identity> 
     <dns value="localhost" /> 
    </identity> 
    </endpoint> 
</client> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="web"> 
     <webHttp helpEnabled="true" 
       automaticFormatSelectionEnabled="true" 
       faultExceptionEnabled="true"/> 
     <enableWebScript/> 
    </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
    <behavior name="JsonRoutingBehave"> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
     <routing filterTableName="ftable"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<routing> 
    <filters> 
    <filter name="all" filterType="MatchAll"/> 
    </filters> 

    <filterTables> 
    <filterTable name="ftable"> 
     <add filterName="all" endpointName="endpointName"/> 
    </filterTable> 
    </filterTables> 
</routing> 
<bindings> 
    <webHttpBinding> 
    <binding name="JsonBinding" openTimeout="00:20:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" allowCookies="true" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"> 
     <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
    </binding> 
    </webHttpBinding> 
</bindings>