询问者
基础连接已经关闭: 连接被意外关闭,初涉WCF,怎么感觉这么困难

问题
-
namespace BS2.WCF.Update { // 注意: 如果更改此处的类名 "FileUploadService",也必须更新 Web.config 中对 "FileUploadService" 的引用。 [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, IncludeExceptionDetailInFaults = true)] public class FileUploadService :IFileUploadContract { public bool UploadFileToHttpServer(CFileUploadDataEntity fileEntity) { try { string uriString = fileEntity.HtppUrl; WebClient myWebClient = new WebClient(); string str = HostingEnvironment.MapPath("~/FileUpLoad"); str += "/Test.zip"; using (FileStream fs = new FileStream(str, FileMode.Create)) { MemoryStream ms = new MemoryStream(fileEntity.FileSourceBytes); byte[] buffer = new byte[256]; int len; do { len = ms.Read(buffer, 0, buffer.Length); if (len > 0) { fs.Write(buffer, 0, len); } } while (len > 0); fs.Flush(); fs.Close(); } return true; } catch (Exception ex) { throw ex; } } } } // 客户端 #region 调用WCF测试 public static bool InvokeWcf(ref string filePath) { try { CFileUploadDataEntity fileEntity = new CFileUploadDataEntity();// FileUploadContractClient fuService = new FileUploadContractClient(); using (FileStream fs = File.OpenRead(filePath)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Flush(); fs.Close(); fileEntity.FileSourceBytes = buffer; } fileEntity.HtppUrl = "http://192.168.175.192:8080/FileUpLoad/Test.zip"; fileEntity.VersionXml = new System.Text.StringBuilder("<fileName>Test.zip</fileName>"); return fuService.UploadFileToHttpServer(fileEntity);//调用WCF服务 } catch (Exception ex) { throw ex; } } #endregion
//////server端Config <?xml version="1.0" encoding="utf-8"?> <!-- 注意: 除了手动编辑此文件以外, 还可以使用 Web 管理工具来配置应用程序的设置。 可以使用 Visual Studio 中的“网站”->“Asp.Net 配置”选项。 设置和注释的完整列表在 machine.config.comments 中,该文件通常位于 \Windows\Microsoft.Net\Framework\v2.x\Config --> <configuration> <configSections> <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/> <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" /> <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> </sectionGroup> </sectionGroup> </sectionGroup> </configSections> <appSettings/> <connectionStrings/> <system.web> <!-- 设置 compilation debug="true" ,将调试符号 插入已编译的页面中。但由于这会影响性能, 因此请只在开发过程中 将此值设置为 true。 --> <compilation debug="true"> <assemblies> <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </assemblies> </compilation> <!-- 通过 <authentication> 节可以配置 ASP.NET 使用的安全身份验证 模式,以标识传入的用户。 --> <authentication mode="Windows" /> <!-- 通过 <customErrors> 节可以配置在执行请求过程中出现未处理错误时, 应执行的操作。 具体说来,开发人员通过该节 可以配置要显示的 html 错误页 以代替错误堆栈跟踪。 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> --> <pages> <controls> <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </controls> </pages> <httpHandlers> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/> </httpHandlers> <httpModules> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </httpModules> </system.web> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <providerOption name="CompilerVersion" value="v3.5"/> <providerOption name="WarnAsError" value="false"/> </compiler> </compilers> </system.codedom> <!-- 在 Internet 信息服务 7.0 下,运行 ASP.NET AJAX 要求 system.webServer 节。这在以前版本的 IIS 中并非必需。 --> <system.webServer> <validation validateIntegratedModeConfiguration="false"/> <modules> <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </modules> <handlers> <remove name="WebServiceHandlerFactory-Integrated"/> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </handlers> </system.webServer> <system.serviceModel> <services> <service behaviorConfiguration="BS2.WCF.Update.FileUploadServiceBehavior" name="BS2.WCF.Update.FileUploadService"> <endpoint address="" binding="wsHttpBinding" contract="BS2.WCF.Contract.IFileUploadContract"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8015/FileUploadService.svc"/> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="BS2.WCF.Update.FileUploadServiceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> <behavior name="BS2.WCF.Update.Service1Behavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> <behavior name="BS2.WCF.Update.Service2Behavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> //客户端Config <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="BS2.Update.ServerUpload.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <applicationSettings> <BS2.Update.ServerUpload.Properties.Settings> <setting name="BS2_Update_Library_FileUploadService" serializeAs="String"> <value>http://localhost:4422/FileUploadService.svc</value> </setting> </BS2.Update.ServerUpload.Properties.Settings> </applicationSettings> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IFileUploadContract" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:30:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""> <extendedProtectionPolicy policyEnforcement="Never" /> </transport> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="http://localhost:4422/FileUploadService.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IFileUploadContract" contract="BS2.Update.Library.IFileUploadContract" name="WSHttpBinding_IFileUploadContract"> <identity> <dns value="localhost" /> </identity> </endpoint> </client> </system.serviceModel> </configuration>
出错的问题如标题所示,当我传小文件时候不会报错,但大一点的就报这错了,这是怎么回事呢?这个错是客户端的错还是服务器端的错?传5M以上都不行!传5M以上的报错如下:基础连接已经关闭: 连接被意外关闭 传了个2.06M的报错如下:远程服务器返回了意外响应:(400)Bad Requst。
老师们帮我看看!谢谢!
全部回复
-
我在客户端config里把
<binding name="WSHttpBinding_IFileUploadContract" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:30:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="">
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
这里面的部分设置都加大过,还是报那个错?这里面加大是否就是你说的用那文件工具配的效果一样呢? WCF最大能接收多大的?如果拆分着来传的话,怎么控制超时问题 -
我在客户端config里把
<binding name="WSHttpBinding_IFileUploadContract" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:30:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="">
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
这里面的部分设置都加大过,还是报那个错?这里面加大是否就是你说的用那文件工具配的效果一样呢? WCF最大能接收多大的?如果拆分着来传的话,怎么控制超时问题
快乐在于能够长时间的为自己认为值得的事情努力工作,不管它是什么。 -
<binding name="WSHttpBinding_IFileUploadContract" closeTimeout="00:10:00" openTimeout="00:50:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="50000000" maxReceivedMessageSize="100000000" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="50000000" maxBytesPerRead="50000000" maxNameTableCharCount="50000000" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""> <extendedProtectionPolicy policyEnforcement="Never" /> </transport> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> </binding>
雄哥,我这样设置了,错误依然是那个,传5M以上的报错如下:基础连接已经关闭: 连接被意外关闭 传了个2.06M的报错如下:远程服务器返回了意外响应:(400)Bad Requst。
-
你把服务端和客户端的
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="">
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
给修改了<security mode="None">
</security>
这是客户端错误,你服务和客户端都修改一下。
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
-
徐雷老师,看见你的回复真高兴,我正在听你讲的那个WCF与AJAX的讲座,谢谢你们让我受益了,还有海雄哥这么多热心的朋友,我以后也经常来这地方,帮助别人或者来寻求帮助,来学习!
为什么我的服务器端的Config里会没有<security mode="Message">这个节点呢?也没有<binding 节点,那配置文件是创建WCF项目时候自动生成的,我没改过,我只把契约的命名空间改过,另外我现在按照海雄哥说的做了,我把客户端的
<binding name="WSHttpBinding_IFileUploadContract" closeTimeout="00:10:00" openTimeout="00:50:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="50000000" maxReceivedMessageSize="100000000" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="50000000" maxBytesPerRead="50000000" maxNameTableCharCount="50000000" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""> <extendedProtectionPolicy policyEnforcement="Never" /> </transport> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> </binding>
这一块复制了一份放在了服务端的config里的 <system.serviceModel>这个节点包含了,把
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="">
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>这个也复制了一份到服务端的config里,并且把mode 改成了"None" 现在新问题又来了:
接收对 http://localhost:4422/FileUploadService.svc 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。有关详细信息,请参阅服务器日志。
现在是报这错了,这是什么错呢?我的环境是这样的,我的客户端是一个WinForm,但我不想在界面就调用WCF,所以我在界面引用了WCF服务,.NET会自动创建了一个代理类,我把自动生成的代理类,完全复制了一份,放在了另一个类库里了(改过了类名和命名空间,另外界面层自动生成的代理类,我删掉了的),我是在另一个类库里去调用 的WCF,而不是界面层,这没关系的吧?
-
谢谢。欢迎常来。
这个WSHttpBinding默认使用Message安全模式。这是在绑定里设置的。
服务端和客户端都可以设置。
另外保证你的地址是http绑定
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
-
在哪设置保证是HTTP绑定?
服务地址啊 是http协议吗
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
-
我的服务的Config没有Binding节点,只有客户端有,人创建WCF项目后只改过config里的契约命名空间,其它我都没动?为什么默认服务端config没有Binding节点呢
WCF默认会使用一些默认的配置,所以可以不使用配置文件也可以。你要通过配置文件控制WCF服务,那你自己手动加上吧。
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
-
你好,
该情况通常是由以下几种情况引起的
a: Binding配置不正确
b: 检查防火墙设置,服务端口是否已经设置为安全端口
C: WCF 服务地址是否正确
d: WCF Server Host有没有启动
e: 配置终结点基于HTTP协议1,看了一下你的UploadFileToHttpServer服务方法,可能是由于服务方法内部抛出的异常,byte[] buffer = new byte[256]你可以把数组的长度设置大一些。
2,你可以使用WCF Tracing记录错误信息到日记文件,使用Service Trace Viewer工具查看、分析跟踪的数据。或者将includeExceptionDetailInFaults设置为"true",查看服务抛出的具体异常信息。谢谢!
Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework