Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF服务不会返回500内部服务器错误。相反,只有400个错误请求_C#_Asp.net_Wcf - Fatal编程技术网

C# WCF服务不会返回500内部服务器错误。相反,只有400个错误请求

C# WCF服务不会返回500内部服务器错误。相反,只有400个错误请求,c#,asp.net,wcf,C#,Asp.net,Wcf,我创建了一个简单的RESTful WCF文件流服务。当发生错误时,我希望生成500个内部服务器错误响应代码。相反,只生成400个错误请求。 当请求有效时,我会得到正确的响应(200 OK),但即使我抛出异常,我也会得到400 IFileService: [ServiceContract] public interface IFileService { [OperationContract] [WebInvoke(Method = "GET", BodyStyle

我创建了一个简单的RESTful WCF文件流服务。当发生错误时,我希望生成500个内部服务器错误响应代码。相反,只生成400个错误请求。 当请求有效时,我会得到正确的响应(200 OK),但即使我抛出异常,我也会得到400

IFileService:

[ServiceContract]
public interface IFileService
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        BodyStyle = WebMessageBodyStyle.Bare,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/DownloadConfig")]
    Stream Download();
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class GCConfigFileService : IGCConfigFileService
{
    public Stream Download()
    {
        throw new Exception();
    }
}
文件服务:

[ServiceContract]
public interface IFileService
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        BodyStyle = WebMessageBodyStyle.Bare,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/DownloadConfig")]
    Stream Download();
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class GCConfigFileService : IGCConfigFileService
{
    public Stream Download()
    {
        throw new Exception();
    }
}
Web.Config

<location path="FileService.svc">
<system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
</system.web>
</location>
<system.serviceModel>
<client />
<behaviors>
  <serviceBehaviors>
    <behavior name="FileServiceBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<services>
  <service name="FileService" 
           behaviorConfiguration="FileServiceBehavior">
    <endpoint address=""
              binding="webHttpBinding"
              bindingConfiguration="FileServiceBinding"
              behaviorConfiguration="web"
              contract="IFileService"></endpoint>
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding
      name="FileServiceBinding"
      maxBufferSize="2147483647"
      maxReceivedMessageSize="2147483647"
      transferMode="Streamed"
      openTimeout="04:01:00"
      receiveTimeout="04:10:00" 
      sendTimeout="04:01:00">
      <readerQuotas maxDepth="2147483647" 
                    maxStringContentLength="2147483647"
                    maxArrayLength="2147483647" 
                    maxBytesPerRead="2147483647" 
                    maxNameTableCharCount="2147483647" />
    </binding>
  </webHttpBinding>
</bindings>

简单: 尝试抛出新的WebFaultException(HttpStatusCode.InternalServerError)

要指定错误详细信息,请执行以下操作:

throw new WebFaultException<string>("Custom Error Message!", HttpStatusCode.InternalServerError);
然后,您需要创建服务行为以附加到您的服务:

class ErrorBehaviorAttribute : Attribute, IServiceBehavior
{
   Type errorHandlerType;

   public ErrorBehaviorAttribute(Type errorHandlerType)
   {
      this.errorHandlerType = errorHandlerType;
   }

   public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
   {
   }

   public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
   {
   }

   public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
   {
      IErrorHandler errorHandler;

      errorHandler = (IErrorHandler)Activator.CreateInstance(errorHandlerType);
      foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
      {
         ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
         channelDispatcher.ErrorHandlers.Add(errorHandler);
      }
   }
}
简单: 尝试抛出新的WebFaultException(HttpStatusCode.InternalServerError)

要指定错误详细信息,请执行以下操作:

throw new WebFaultException<string>("Custom Error Message!", HttpStatusCode.InternalServerError);
然后,您需要创建服务行为以附加到您的服务:

class ErrorBehaviorAttribute : Attribute, IServiceBehavior
{
   Type errorHandlerType;

   public ErrorBehaviorAttribute(Type errorHandlerType)
   {
      this.errorHandlerType = errorHandlerType;
   }

   public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
   {
   }

   public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
   {
   }

   public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
   {
      IErrorHandler errorHandler;

      errorHandler = (IErrorHandler)Activator.CreateInstance(errorHandlerType);
      foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
      {
         ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
         channelDispatcher.ErrorHandlers.Add(errorHandler);
      }
   }
}

谢谢你的回复。我尝试了
抛出新的FaultException(!”
,但这对我不起作用(仍然得到400分)。FaultException似乎应该用于基于SOAP的服务,而我的不是。我使用的是基于REST的方法。更新答案后,请告诉我它是否适用于WebFaultException。WebFaultException确实会创建500个内部服务器错误,但是否无法设置错误消息?我想返回一个原因,但是WebFaultException类不允许我设置这个。非常感谢您的帮助<代码>WebFaultException(“自定义错误消息!”,HttpStatusCode.InternalServerError)正是我想要的。感谢您的回复。我尝试了
抛出新的FaultException(!”
,但这对我不起作用(仍然得到400分)。FaultException似乎应该用于基于SOAP的服务,而我的不是。我使用的是基于REST的方法。更新答案后,请告诉我它是否适用于WebFaultException。WebFaultException确实会创建500个内部服务器错误,但是否无法设置错误消息?我想返回一个原因,但是WebFaultException类不允许我设置这个。非常感谢您的帮助<代码>WebFaultException(“自定义错误消息!”,HttpStatusCode.InternalServerError)正是我要找的。