[英]WCF REST Proxy using ChannelFactory
我正在嘗試使用ChannelFactory在WCF服務上創建代理WCF服務。
我為WCF服務定義了以下接口來調用。 它具有通用的GetResource方法,該方法將響應字節[]直接寫入HttpContext並根據資源的類型設置其內容類型。
[ServiceContract]
public interface ITest
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "getResource/?resourceKey={resourceKey}")]
void GetResource(string resourceKey);
}
GetResource的代理實現:
public void GetResource(string resourceKey)
{
var factory = new ChannelFactory<ITest>(GetCustomBinding(), new EndpointAddress('https://test.com/mytest.svc'));
channel.Endpoint.Behaviors.Add(new WebHttpBehavior());
var channel = factory.CreateChannel();
using (var scope = new OperationContextScope((IContextChannel)channel))
{
channel.GetResource(resourceKey);
}
}
private static Binding GetCustomBinding()
{
var binding = new WebHttpBinding(WebHttpSecurityMode.Transport)
{
MaxReceivedMessageSize = 20000000,
MaxBufferSize = 20000000,
MaxBufferPoolSize = 20000000,
CloseTimeout = TimeSpan.FromMinutes(1)
};
binding.ContentTypeMapper = new JsonMapper();
return binding;
}
private class JsonMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
return WebContentFormat.Json;
}
}
我遇到的問題是,當我在此端點上調用代理proxy.svc / GetResource?resourceKey =“ text.css”時,它返回正確的內容類型,但沒有實際的內容,ContentLength = 0。
我應該如何處理響應正文轉發? 我如何閱讀回復正文? 我嘗試使用WebOperationContext.Current.IncomingResponse,但它也沒有給我響應主體。
我認為您正在嘗試以非預期的方式使用WCF。 void方法將不返回任何內容。 從理論上講,您可以通過添加檢查行為來攔截此行為,但甚至不確定這樣做是否可行。
我有(可能)類似的代碼,該代碼從數據庫加載文件並返回它們,但是它將return
內容。
var file = ServiceUtilities.FileManager.GetFile(id);
if (file != null)
{
var fcr = new FileContentResult(file.Content, file.MimeType);
return fcr;
}
也許您應該重新考慮預期的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.