今天遇到一个问题,系统基于WCF框架,不同的服务完全相似的代码,一个就报错,一个成功。而且报错的代码其实际顺利运行完毕所有代码,事务也成功地提交了,只是在返回信息时WCF给了一个错误信息:
<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none"><Code><Value>Receiver</Value><Subcode><Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</Value></Subcode></Code><Reason><Text xml:lang="zh-CN">message 对象已被释放。</Text></Reason><Detail><ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HelpLink i:nil="true"/><InnerException i:nil="true"/><Message>message 对象已被释放。</Message><StackTrace> 在 System.ServiceModel.Channels.ByteStreamMessage.InternalByteStreamMessage.get_Properties()
↵ 在 System.ServiceModel.OperationContext.get_IncomingMessageProperties()
↵ 在 System.ServiceModel.Web.IncomingWebRequestContext.get_UriTemplateMatch()
↵ 在 System.ServiceModel.Description.WebHttpBehavior.TrySetupJavascriptCallback(String callbackParameterName)
↵ 在 System.ServiceModel.Dispatcher.JavascriptCallbackMessageInspector.BeforeSendReply(Message& reply, Object correlationState)
↵ 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.BeforeSendReplyCore(MessageRpc& rpc, Exception& exception, Boolean& thereIsAnUnhandledException)</StackTrace><Type>System.ObjectDisposedException</Type></ExceptionDetail></Detail></Fault>
经过排查,发现是由于Web.Config文件中binging节点的 crossDomainScriptAccessEnabled="true" 属性导致的,去掉后一切正常。微软官方帮助文档说该属性是用来设定是否允许跨域脚本访问,不清楚为何该属性会导致上述问题。只不过我们项目暂时不存在跨域访问问题,故去掉没有大碍。
具体web.config配置文件和代码如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="Service.ServiceBehavior" name="VME.Service.PictureService">
<endpoint address="" behaviorConfiguration="VME.Service.ServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="VME.Contract.IPictureService" bindingConfiguration="webBinding" />
</service>
<service behaviorConfiguration="Service.ServiceBehavior" name="VME.Service.ServiceService">
<endpoint address="" behaviorConfiguration="VME.Service.ServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="VME.Contract.IServiceService" bindingConfiguration="serviceBinding" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="VME.Service.ServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Service.ServiceBehavior">
<serviceThrottling maxConcurrentCalls="50" maxConcurrentSessions="50" maxConcurrentInstances="50"/>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>
<bindings>
<webHttpBinding>
<binding name="webBinding" useDefaultWebProxy="false" crossDomainScriptAccessEnabled="true"
maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
sendTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:01:00" closeTimeout="00:01:00"
transferMode="Streamed" hostNameComparisonMode="StrongWildcard">
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" maxDepth="32" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="None"></security>
</binding>
<binding name="serviceBinding" useDefaultWebProxy="false" crossDomainScriptAccessEnabled="true"
maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
sendTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:01:00" closeTimeout="00:01:00"
transferMode="Streamed" hostNameComparisonMode="StrongWildcard">
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" maxDepth="32" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
[ServiceContract]
public interface IPictureService
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
bool SendIdCardPicture(Stream idCardImg);
}
[ServiceContract]
public interface IServiceService
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
bool AddService(Stream service);
}
上面IPictureService接口的程序是能正常返回信息,而IServiceService接口的程序就报错。
有高手请给予指点迷津,到底哪里出了问题。