我有一个WCF-REST服务,它有一个返回字符串的方法:
[ServiceContract]
public interface IService
{
[WebInvoke(UriTemplate = "/getastring/")]
[OperationContract]
string GetAString(string input);
}
public class Service : IService
{
public string GetAString(string input)
{
Trace.WriteLine("GetAString");
return input;
}
}该服务由WebHttpBinding托管,TransferMode设置为流。
ServiceHost streamedHost = new ServiceHost(typeof(Service), new Uri("http://localhost/streamed"));
WebHttpBinding streamedBinding = new WebHttpBinding();
streamedBinding.TransferMode = TransferMode.Streamed;
ServiceEndpoint streamedEndpoint = streamedHost.AddServiceEndpoint(typeof(IService), streamedBinding, string.Empty);
streamedEndpoint.Behaviors.Add(new ErrorWebHttpBehavior());
streamedHost.Open();当使用.NET客户端访问服务时,一切都很好。当我在泽西使用Java客户机时,会出现以下异常:
尝试对不存在的网络连接进行操作。
只有当TransferMode设置为流时才会发生这种情况。
使用了以下Java代码:
Client client = Client.create(new DefaultClientConfig());
WebResource service = client.resource(UriBuilder.fromUri("http://localhost").build());
String result = service.path("regular/getastring").type(MediaType.APPLICATION_JSON).post(String.class, "123");
System.out.println(result);是否有一种方法可以毫无例外地使用流WCF服务?
样本项目:http://dl.dropbox.com/u/21096596/WCF-Jersey.zip
发布于 2012-01-18 14:34:00
如果仅为响应启用流,则不会出现此问题。在绑定配置中设置TransferMode = TransferMode.StreamedResponse;为我们解决了问题。
发布于 2014-04-30 15:58:24
差不多2年后..。以下是我的答案:
同样的错误,我也有同样的问题,但是你不能用StreamedResponse来解决。正如MSDN中所述,使用这里将启用Streaming for Response和Buffered for Request。
所以..。它将工作,但在缓冲模式。如果您正在传输大型文件(如10 in ),您的程序将在发送之前将其预加载到RAM中。
现在,如果你的问题和我的问题一样,我可以告诉它唯一的客户端。我非常肯定,您的java代码适用于小于65.536字节的文件。这是因为您需要更改客户端上的maxReceivedMessageSize属性以供下载!
这是一个9223372036854775807类型,所以您可以设置为最大的long字节,但我认为您将永远不需要那么多!
希望这能帮助像我这样在这种情况下浪费一天生命的人!
https://stackoverflow.com/questions/8882685
复制相似问题