Guest User

Untitled

a guest
Jan 15th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. [ServiceContract]
  2. public interface IFileService
  3. {
  4. [OperationContract]
  5. [WebInvoke(Method = "GET",
  6. BodyStyle = WebMessageBodyStyle.Bare,
  7. ResponseFormat = WebMessageFormat.Json,
  8. UriTemplate = "/DownloadConfig")]
  9. Stream Download();
  10. }
  11.  
  12. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  13. public class GCConfigFileService : IGCConfigFileService
  14. {
  15. public Stream Download()
  16. {
  17. throw new Exception();
  18. }
  19. }
  20.  
  21. <location path="FileService.svc">
  22. <system.web>
  23. <authorization>
  24. <allow users="*"/>
  25. </authorization>
  26. </system.web>
  27. </location>
  28. <system.serviceModel>
  29. <client />
  30. <behaviors>
  31. <serviceBehaviors>
  32. <behavior name="FileServiceBehavior">
  33. <serviceMetadata httpGetEnabled="true"/>
  34. <serviceDebug includeExceptionDetailInFaults="false" />
  35. </behavior>
  36. </serviceBehaviors>
  37. <endpointBehaviors>
  38. <behavior name="web">
  39. <webHttp/>
  40. </behavior>
  41. </endpointBehaviors>
  42. </behaviors>
  43. <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  44. multipleSiteBindingsEnabled="true" />
  45. <services>
  46. <service name="FileService"
  47. behaviorConfiguration="FileServiceBehavior">
  48. <endpoint address=""
  49. binding="webHttpBinding"
  50. bindingConfiguration="FileServiceBinding"
  51. behaviorConfiguration="web"
  52. contract="IFileService"></endpoint>
  53. </service>
  54. </services>
  55. <bindings>
  56. <webHttpBinding>
  57. <binding
  58. name="FileServiceBinding"
  59. maxBufferSize="2147483647"
  60. maxReceivedMessageSize="2147483647"
  61. transferMode="Streamed"
  62. openTimeout="04:01:00"
  63. receiveTimeout="04:10:00"
  64. sendTimeout="04:01:00">
  65. <readerQuotas maxDepth="2147483647"
  66. maxStringContentLength="2147483647"
  67. maxArrayLength="2147483647"
  68. maxBytesPerRead="2147483647"
  69. maxNameTableCharCount="2147483647" />
  70. </binding>
  71. </webHttpBinding>
  72. </bindings>
  73.  
  74. throw new WebFaultException<string>("Custom Error Message!", HttpStatusCode.InternalServerError);
  75.  
  76. class HttpErrorHandler : IErrorHandler
  77. {
  78. public bool HandleError(Exception error)
  79. {
  80. return false;
  81. }
  82.  
  83. public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
  84. {
  85. if (fault != null)
  86. {
  87. HttpResponseMessageProperty properties = new HttpResponseMessageProperty();
  88. properties.StatusCode = HttpStatusCode.InternalServerError;
  89. fault.Properties.Add(HttpResponseMessageProperty.Name, properties);
  90. }
  91. }
  92. }
  93.  
  94. class ErrorBehaviorAttribute : Attribute, IServiceBehavior
  95. {
  96. Type errorHandlerType;
  97.  
  98. public ErrorBehaviorAttribute(Type errorHandlerType)
  99. {
  100. this.errorHandlerType = errorHandlerType;
  101. }
  102.  
  103. public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
  104. {
  105. }
  106.  
  107. public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
  108. {
  109. }
  110.  
  111. public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
  112. {
  113. IErrorHandler errorHandler;
  114.  
  115. errorHandler = (IErrorHandler)Activator.CreateInstance(errorHandlerType);
  116. foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
  117. {
  118. ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
  119. channelDispatcher.ErrorHandlers.Add(errorHandler);
  120. }
  121. }
  122. }
  123.  
  124. [ServiceContract]
  125. public interface IService
  126. {
  127. [OperationContract(Action = "*", ReplyAction = "*")]
  128. Message Action(Message m);
  129. }
  130.  
  131. [ErrorBehavior(typeof(HttpErrorHandler))]
  132. public class Service : IService
  133. {
  134. public Message Action(Message m)
  135. {
  136. throw new FaultException("!");
  137. }
  138. }
Add Comment
Please, Sign In to add comment