WCF REST服务:Jersey和TransferMode = Streamed访问时出现异常

时间:2012-01-16 16:03:36

标签: wcf rest jersey

我有一个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-Client访问服务时,一切都很好。 当我使用带泽西的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

2 个答案:

答案 0 :(得分:0)

如果仅为响应启用了流式传输,则不会出现此问题。 在绑定配置中设置TransferMode = TransferMode.StreamedResponse;可以解决我们的问题。

答案 1 :(得分:0)

差不多2年后..在这里我的回答:

我遇到了同样的错误同样的问题,但你无法使用StreamedResponse来解决。如MSDN HERE中所述,使用StreamedResponse将为{strong>响应启用Streaming,为请求启用Buffered

所以.. 工作,但是在缓冲模式下。如果要传输大文件(如10gb),程序会在发送前将其预先加载到RAM中。

现在,如果您的问题与我的问题相同,我可以告诉它唯一的客户端。我很确定你的java代码适用于小于65.536字节的文件。那是因为您需要在客户端上更改maxReceivedMessageSize属性以进行下载!

它是long类型,因此您可以设置为 9223372036854775807 字节的最大值,但我认为您不会需要那么多!

希望这有助于像我一样在这种情况下浪费一天的人!

相关问题