WCF Rest Service Return HttpWebResponse
I am creating a WCF Rest service that calls another web service. The desired outcome is for my web service to return untouched the HttpWebResponse that comes from the other web service. Something like:
[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 (Ht开发者_开发技巧tpWebResponse)request.GetResponse();
}
Is this possible?
Checkout the new WCF Http stack at wcf.codeplex.com. At the moment you can do something similar, in a future release they are planning something to address exactly what you want to do.
You can currently do the following with the WCF Http stack.
public void DoSomething(HttpRequestMessage request, HttpResponseMessage response)
{
}
You can create your own routing service. Create an interface for routing service with contract all ("*")
[ServiceContract]
public interface IRoutingService
{
[WebInvoke(UriTemplate = "")]
[OperationContract(AsyncPattern = true, Action = "*", ReplyAction = "*")]
IAsyncResult BeginProcessRequest(Message requestMessage, AsyncCallback asyncCallback, object asyncState);
Message EndProcessRequest(IAsyncResult asyncResult);
}
Then implement this in service
[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();
}
}
}
}
}
and do configuration in 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>
精彩评论