/
SoapFormat.cs
104 lines (91 loc) · 3.46 KB
/
SoapFormat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#if !NETCORE
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using ServiceStack.DataAnnotations;
using ServiceStack.Host;
using ServiceStack.Host.Handlers;
using ServiceStack.Web;
namespace ServiceStack;
public class SoapFormat : IPlugin, Model.IHasStringId
{
public string Id { get; set; } = Plugins.Soap;
public bool DisableSoap11 { get; set; }
public bool DisableSoap12 { get; set; }
public void Register(IAppHost appHost)
{
var contentTypes = (ContentTypes)appHost.ContentTypes;
var predefinedRoutes = appHost.GetPlugin<PredefinedRoutesFeature>();
if (predefinedRoutes == null)
throw new NotSupportedException("SoapFormat requires the PredefinedRoutesFeature Plugin");
if (!DisableSoap11)
{
contentTypes.Register(MimeTypes.Soap11, SoapHandler.SerializeSoap11ToStream, null);
contentTypes.ContentTypeStringSerializers[MimeTypes.Soap11] = (r, o) =>
SoapHandler.SerializeSoap11ToBytes(r, o).FromUtf8Bytes();
var soap11 = ContentFormat.GetContentFormat(Format.Soap11);
predefinedRoutes.HandlerMappings[soap11] = () => new Soap11MessageReplyHttpHandler();
}
if (!DisableSoap12)
{
contentTypes.Register(MimeTypes.Soap12, SoapHandler.SerializeSoap12ToStream, null);
contentTypes.ContentTypeStringSerializers[MimeTypes.Soap12] = (r, o) =>
SoapHandler.SerializeSoap12ToBytes(r, o).FromUtf8Bytes();
var soap12 = ContentFormat.GetContentFormat(Format.Soap12);
predefinedRoutes.HandlerMappings[soap12] = () => new Soap12MessageReplyHttpHandler();
}
}
}
// Overridable APIs to customize SOAP behavior
public abstract partial class ServiceStackHost
{
public virtual List<Type> ExportSoapOperationTypes(List<Type> operationTypes)
{
var types = operationTypes
.Where(x => !x.AllAttributes<ExcludeAttribute>()
.Any(attr => attr.Feature.Has(Feature.Soap)))
.Where(x => !x.IsGenericTypeDefinition)
.ToList();
return types;
}
public virtual bool ExportSoapType(Type type)
{
return !type.IsGenericTypeDefinition &&
!type.AllAttributes<ExcludeAttribute>()
.Any(attr => attr.Feature.Has(Feature.Soap));
}
public virtual void WriteSoapMessage(IRequest req, System.ServiceModel.Channels.Message message, Stream outputStream)
{
try
{
using (var writer = XmlWriter.Create(outputStream, Config.XmlWriterSettings))
{
message.WriteMessage(writer);
}
}
catch (Exception ex)
{
var response = OnServiceException(req, req.Dto, ex).Result;
if (response == null || !outputStream.CanSeek)
return;
outputStream.Position = 0;
try
{
message = SoapHandler.CreateResponseMessage(response, message.Version, req.Dto.GetType(),
req.GetSoapMessage().Headers.Action == null);
using (var writer = XmlWriter.Create(outputStream, Config.XmlWriterSettings))
{
message.WriteMessage(writer);
}
}
catch { }
}
finally
{
HostContext.CompleteRequest(req);
}
}
}
#endif