|
| 1 | +using Autofac; |
| 2 | +using Surging.Core.CPlatform.EventBus.Events; |
| 3 | +using Surging.Core.CPlatform.EventBus.Implementation; |
| 4 | +using Surging.Core.CPlatform.Ioc; |
| 5 | +using Surging.Core.CPlatform.Messages; |
| 6 | +using Surging.Core.CPlatform.Utilities; |
| 7 | +using Surging.Core.ProxyGenerator; |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | +using System.Text; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +namespace Surging.Core.Protocol.WebService.Runtime |
| 15 | +{ |
| 16 | + public abstract class WebServiceBehavior : IServiceBehavior |
| 17 | + { |
| 18 | + private ServerReceivedDelegate _received; |
| 19 | + public event ServerReceivedDelegate Received |
| 20 | + { |
| 21 | + add |
| 22 | + { |
| 23 | + |
| 24 | + _received += value; |
| 25 | + } |
| 26 | + remove |
| 27 | + { |
| 28 | + _received -= value; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + public string MessageId { get; } = Guid.NewGuid().ToString("N"); |
| 35 | + |
| 36 | + |
| 37 | + public async Task Write(object result, int statusCode = 200, string exceptionMessage = "") |
| 38 | + { |
| 39 | + if (_received == null) |
| 40 | + return; |
| 41 | + var message = new TransportMessage(MessageId, new ReactiveResultMessage |
| 42 | + { |
| 43 | + ExceptionMessage = exceptionMessage, |
| 44 | + StatusCode = statusCode, |
| 45 | + Result = result |
| 46 | + |
| 47 | + }); |
| 48 | + await _received(message); |
| 49 | + } |
| 50 | + public T CreateProxy<T>(string key) where T : class |
| 51 | + { |
| 52 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>(key); |
| 53 | + } |
| 54 | + |
| 55 | + public object CreateProxy(Type type) |
| 56 | + { |
| 57 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(type); |
| 58 | + } |
| 59 | + |
| 60 | + public object CreateProxy(string key, Type type) |
| 61 | + { |
| 62 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(key, type); |
| 63 | + } |
| 64 | + |
| 65 | + public T CreateProxy<T>() where T : class |
| 66 | + { |
| 67 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>(); |
| 68 | + } |
| 69 | + |
| 70 | + public T GetService<T>(string key) where T : class |
| 71 | + { |
| 72 | + if (ServiceLocator.Current.IsRegisteredWithKey<T>(key)) |
| 73 | + return ServiceLocator.GetService<T>(key); |
| 74 | + else |
| 75 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>(key); |
| 76 | + } |
| 77 | + |
| 78 | + public T GetService<T>() where T : class |
| 79 | + { |
| 80 | + if (ServiceLocator.Current.IsRegistered<T>()) |
| 81 | + return ServiceLocator.GetService<T>(); |
| 82 | + else |
| 83 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>(); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + public object GetService(Type type) |
| 88 | + { |
| 89 | + if (ServiceLocator.Current.IsRegistered(type)) |
| 90 | + return ServiceLocator.GetService(type); |
| 91 | + else |
| 92 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(type); |
| 93 | + } |
| 94 | + |
| 95 | + public object GetService(string key, Type type) |
| 96 | + { |
| 97 | + if (ServiceLocator.Current.IsRegisteredWithKey(key, type)) |
| 98 | + return ServiceLocator.GetService(key, type); |
| 99 | + else |
| 100 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(key, type); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + public void Publish(IntegrationEvent @event) |
| 106 | + { |
| 107 | + GetService<IEventBus>().Publish(@event); |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | +} |
| 112 | + |
0 commit comments