我正在创建一个调用另一个Web服务的WCF Rest服务。期望的结果是我的Web服务不会返回来自其他Web服务的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 :(得分:0)
在wcf.codeplex.com上查看新的WCF Http堆栈。目前你可以做类似的事情,在未来的版本中,他们正在计划一些事情来准确地解决你想要做的事情。
您目前可以使用WCF Http堆栈执行以下操作。
public void DoSomething(HttpRequestMessage request, HttpResponseMessage response)
{
}
答案 1 :(得分:0)
您可以创建自己的路由服务。 使用合同all(" *")
创建路由服务的接口[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>