我想使用ELMAH配置使用SQL Server进行日志记录。我遇到了以下问题:
内容类型text / html;响应消息的charset = utf-8与绑定的内容类型不匹配(application / soap + xml; charset = utf-8)。如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法。响应的前1024个字节是:
<!DOCTYPE html> <html> <head> <title>Configuration Error</title> <meta name="viewport" content="width=device-width" /> <style> body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
.....
这是我的服务web.config
:
<configuration>
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="ADO.NET.SqlServer" connectionString="Data Source=MYDBSourceName;Initial Catalog=MYDatabaseName;Persist Security Info=True;User ID=sa;pwd=sa providerName="System.Data.SqlClient" />
</connectionStrings>
<elmah>
<security allowRemoteAccess="1"/>
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ADO.NET.SqlServer" applicationName="GWM Service"/>
</elmah>
<appSettings file="localSettings.config">
</appSettings>
<system.net>
<mailSettings>
<smtp>
</smtp>
</mailSettings>
</system.net>
<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
</httpModules>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<handlers>
<add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/>
</handlers>
<modules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="behaviorAuthentication" name="MyServiceName">
<endpoint binding="wsHttpBinding" bindingConfiguration="bindingAuthentication" contract="MyServiceContractInterfaceName">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorAuthentication">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="bindingAuthentication" transactionFlow="false" sendTimeout="00:30:00" receiveTimeout="00:30:00">
<reliableSession enabled="true"/>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
这位于另一个托管层项目中。我的服务类和合同在另一个项目中的位置。它们被映射在一起进行自我托管。
我添加了以下类和属性:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Collections.ObjectModel;
using System.Net;
using System.Web;
using System.IO;
using Elmah;
namespace Logging
{
public class ElmahErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return false;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (error == null)
{
return;
}
if (HttpContext.Current == null)
{
return;
}
Elmah.ErrorSignal.FromCurrentContext().Raise(error);
}
}
public class ServiceErrorBehaviorAttribute : Attribute, IServiceBehavior
{
Type errorHandlerType;
public ServiceErrorBehaviorAttribute(Type errorHandlerType)
{
this.errorHandlerType = errorHandlerType;
}
public void AddBindingParameters(ServiceDescription serviceDescription,
System.ServiceModel.ServiceHostBase serviceHostBase,
System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
IErrorHandler errorHandler;
errorHandler = (IErrorHandler)Activator.CreateInstance(errorHandlerType);
foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher cd = cdb as ChannelDispatcher;
cd.ErrorHandlers.Add(errorHandler);
}
}
public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
}
public class HttpErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return false;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
//if (error != null) // Notify ELMAH of the exception.
//{
// Elmah.ErrorSignal.FromCurrentContext().Raise(error);
//}
if (error == null)
return;
if (HttpContext.Current == null) //In case we run outside of IIS
return;
Elmah.ErrorSignal.FromCurrentContext().Raise(error);
}
}
}
在我的服务层而非托管层。并用以下内容装饰我的服务类:
[ServiceErrorBehavior(typeof(HttpErrorHandler))]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
现在故意在我的服务方法中添加了ab异常。但是在执行时我得到了例外,如上所述。有人可以帮帮我吗?