C# WCF流在调试时起作用,但在部署时不起作用
我有一个向WCF web服务提交大量数据的应用程序。由于数据可能很大,我想使用流式传输模式,这样我就可以在数据进入时处理数据,而不是缓冲所有数据 当Im调试应用程序时,流媒体按预期工作。一旦我真的将服务发布到我的IIS服务器上,流式传输就不再工作了,数据似乎只是得到了缓冲 我通过在web服务函数的开头放置一个C# WCF流在调试时起作用,但在部署时不起作用,c#,wcf,iis,C#,Wcf,Iis,我有一个向WCF web服务提交大量数据的应用程序。由于数据可能很大,我想使用流式传输模式,这样我就可以在数据进入时处理数据,而不是缓冲所有数据 当Im调试应用程序时,流媒体按预期工作。一旦我真的将服务发布到我的IIS服务器上,流式传输就不再工作了,数据似乎只是得到了缓冲 我通过在web服务函数的开头放置一个抛出新异常来测试它,并更改源流以提供“无限数据” 以下是服务器源代码: [ServiceContract] public class Service1 { [OperationCon
抛出新异常来测试它,并更改源流以提供“无限数据”
以下是服务器源代码:
[ServiceContract]
public class Service1
{
[OperationContract]
public void SubmitData(Stream stream)
{
throw new Exception("Test");
using (var reader = new StreamReader(stream))
{
string line = reader.ReadLine();
// Do Something...
}
}
}
Service1Client client = new Service1Client();
client.Endpoint.Address = new EndpointAddress(address);
client.Open();
using (var stream = new InfiniteDataStream())
client.SubmitData(stream);
client.Close();
<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding transferMode="Streamed" maxReceivedMessageSize="2147483647">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</basicHttpsBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
</system.serviceModel>
<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding name="BasicHttpsBinding_Service1" transferMode="Streamed" >
<security mode="TransportWithMessageCredential" />
</binding>
</basicHttpsBinding>
</bindings>
<client>
<endpoint address="https://localhost:56297/WebService/Service1.svc"
binding="basicHttpsBinding" bindingConfiguration="BasicHttpsBinding_Service1"
contract="WebService.Service1" name="BasicHttpsBinding_Service1" />
</client>
以下是客户端代码:
[ServiceContract]
public class Service1
{
[OperationContract]
public void SubmitData(Stream stream)
{
throw new Exception("Test");
using (var reader = new StreamReader(stream))
{
string line = reader.ReadLine();
// Do Something...
}
}
}
Service1Client client = new Service1Client();
client.Endpoint.Address = new EndpointAddress(address);
client.Open();
using (var stream = new InfiniteDataStream())
client.SubmitData(stream);
client.Close();
<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding transferMode="Streamed" maxReceivedMessageSize="2147483647">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</basicHttpsBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
</system.serviceModel>
<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding name="BasicHttpsBinding_Service1" transferMode="Streamed" >
<security mode="TransportWithMessageCredential" />
</binding>
</basicHttpsBinding>
</bindings>
<client>
<endpoint address="https://localhost:56297/WebService/Service1.svc"
binding="basicHttpsBinding" bindingConfiguration="BasicHttpsBinding_Service1"
contract="WebService.Service1" name="BasicHttpsBinding_Service1" />
</client>
服务器Web.Config:
[ServiceContract]
public class Service1
{
[OperationContract]
public void SubmitData(Stream stream)
{
throw new Exception("Test");
using (var reader = new StreamReader(stream))
{
string line = reader.ReadLine();
// Do Something...
}
}
}
Service1Client client = new Service1Client();
client.Endpoint.Address = new EndpointAddress(address);
client.Open();
using (var stream = new InfiniteDataStream())
client.SubmitData(stream);
client.Close();
<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding transferMode="Streamed" maxReceivedMessageSize="2147483647">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</basicHttpsBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
</system.serviceModel>
<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding name="BasicHttpsBinding_Service1" transferMode="Streamed" >
<security mode="TransportWithMessageCredential" />
</binding>
</basicHttpsBinding>
</bindings>
<client>
<endpoint address="https://localhost:56297/WebService/Service1.svc"
binding="basicHttpsBinding" bindingConfiguration="BasicHttpsBinding_Service1"
contract="WebService.Service1" name="BasicHttpsBinding_Service1" />
</client>
最后一条评论指出这是一个在.NET4.5中修复的bug,但我是用4.6.1构建的
另外,我的服务器运行的是IIS 8.5,所以我决定在我的本地Win10机器上尝试相同的测试,该机器有IIS 10,仍然在做同样的事情
使用Wireshark,我能够确认数据正在流式传输到服务器,而服务器无法处理数据。
我还尝试将transferMode=“Streamed”
更改为transferMode=“StreamedRequest”
,运气不好。将抛出新异常
行代码替换为写入日志文件,表明代码本身没有问题,在本地运行的IIS中托管的服务实例下,从客户端到服务器的请求流按预期工作。重复?你读过“是”吗?有一次我偶然发现,被接受的答案引用了我在更新中引用的相同MSDN线程帖子。声明该问题在.Net 4.5中已修复,只是为了查看该问题是否与SSL/HTTPs有关。您是否尝试过改用非安全绑定?@EugeneKomisarenko是的,我尝试从basicHttpBinding更改为basicHttpBinding,并从绑定中删除了标记,同样的结果在进行进一步更改之前,我真的建议启用WCF诊断,然后看看您是否可以首先从这些日志中检测到问题。如果我错了,请纠正我,但这不会只影响响应吗?我正在尝试对请求进行流式处理。您是对的,先前版本的答案是关于响应的流式处理的,请参阅更新的答案,了解抛出异常的副作用。感谢您的发现,我的原始测试代码中一定存在另一个问题,那就是将流写入文件,直到流被完全发送,它才写入文件。所以我想我引入了一个关于异常测试的新问题。但无论最初的问题是什么,我都不确定,因为现在它工作正常了。