WCF REST 服务:通过 Jersey 和 TransferMode = Streamed 访问时出现异常
我有一个 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 设置为 Streamed
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 客户端与 Jersey 一起使用时,出现以下异常:
尝试在不存在的网络连接上进行操作
仅当 TransferMode 设置为 Streamed 时才会发生。
使用以下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服务?
I have a WCF-REST service with one method which returns a string:
[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;
}
}
The service is hosted with a WebHttpBinding and the TransferMode is set to Streamed
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();
When the service is accessed with a .NET-Client everything is fine.
When I use a Java-Client with Jersey, the following exception occurs:
An operation was attempted on a nonexistent network connection
The only happens when the TransferMode is set to Streamed.
The following Java code is used:
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);
Is there a way to consume the streamed WCF service without this exception?
Sample project: http://dl.dropbox.com/u/21096596/WCF-Jersey.zip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果仅针对响应启用流式传输,则不会出现该问题。
在绑定配置中设置 TransferMode = TransferMode.StreamedResponse; 为我们解决了这个问题。
The problem does not appear if streaming is only enabled for the response.
Setting
TransferMode = TransferMode.StreamedResponse;
in the binding configuration fixes the problem for us.差不多两年后..这是我的答案:
我遇到了同样的问题和同样的错误,但你无法使用
StreamedResponse
来解决。正如 MSDN HERE 中所述,使用 < code>StreamedResponse 将为响应启用Streaming
,并为Request启用Buffered
。所以......它会工作,但是在缓冲模式下。如果您要传输大文件(例如 10GB),您的程序会在发送之前将其预加载到 RAM 中。
现在,如果您的问题与我遇到的问题相同,我可以告诉它唯一的客户端。我非常确定您的 java 代码适用于小于 65.536 字节的文件。那是因为您需要更改客户端上的
maxReceivedMessageSize
属性才能下载!它是
long
类型,因此您可以将最大值设置为 9223372036854775807 字节,但我认为您不需要那么多!希望这可以帮助那些像我一样在这种情况下浪费了一天生命的人!
almost 2 years later.. here my answer:
i had the very same problem with the very same error, but you can't solve using
StreamedResponse
. As stated from MSDN HERE usingStreamedResponse
will enableStreaming
for Response andBuffered
for Request.So.. it will work, but in buffered mode. If you're transfering large files (like 10gb) your program will preload it in RAM before sending.
Now, if your problem is the same as the one i had, i can tell its only client-side. I'm pretty sure that your java code works for file smaller than 65.536 byte. Thats because you need to change your
maxReceivedMessageSize
property on the client for downloads!Its a
long
type, so you can set to a max of 9223372036854775807 bytes, but i don't think you will ever need that much!Hope this helps someone who, like me, wasted 1 day of life in this situation!