/// <summary>
        /// Enables inspection or modification of a message before a request message is sent to a service.
        /// </summary>
        /// <returns>
        /// The object that is returned as the <paramref name="correlationState "/>argument of the <see cref="M:System.ServiceModel.Dispatcher.IClientMessageInspector.AfterReceiveReply(System.ServiceModel.Channels.Message@,System.Object)"/> method. This is null if no correlation state is used.The best practice is to make this a <see cref="T:System.Guid"/> to ensure that no two <paramref name="correlationState"/> objects are the same.
        /// </returns>
        /// <param name="request">The message to be sent to the service.</param><param name="channel">The WCF client object channel.</param>
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            HttpRequestMessageProperty httpRequest;
            if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
            {
                httpRequest = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            }
            else
            {
                httpRequest = new HttpRequestMessageProperty();
                request.Properties.Add(HttpRequestMessageProperty.Name, httpRequest);
            }
            if (httpRequest != null)
            {
                httpRequest.Headers.Add("Authorization", string.Format("{0}", AdalTokenManager.GetToken(serviceName).CreateAuthorizationHeader()));
                var supportCode = "";
                RuntimeFactory.Current.GetStateStorageContainer().TryGetItem("supportCode", out supportCode);
                var meta = new RequestHeader
                             {
                                 Environment = Utilities.GetEnvironment(),
                                 ConfigSet = Utilities.GetConfigSetName(),
                                 ServiceName = RuntimeFactory.Current.ServiceName,
                                 MessageId = Guid.NewGuid().ToString(),
                                 ServerIdentity = Environment.MachineName,
                                 RuntimeInstance = RuntimeFactory.Current.InstanceId.ToString(),
                                 SupportCode = supportCode
                             };
                httpRequest.Headers.Add("X-Stardust-Meta", Convert.ToBase64String(JsonConvert.SerializeObject(meta).GetByteArray()));
            }

            return RuntimeFactory.Current;

        }
        private static void SetMessageValuesIfNotSetInClient(RequestHeader message, IRuntime runtime)
        {
            if (runtime == null) return;
            if (message.MessageId.IsNullOrWhiteSpace())
                message.MessageId = Guid.NewGuid().ToString();
            if (message.Environment.IsNullOrWhiteSpace())
                message.Environment = runtime.Environment;
            if (message.ServiceName.IsNullOrWhiteSpace())
                message.ServiceName = runtime.ServiceName;
            if (message.ConfigSet.IsNullOrWhiteSpace())
                message.ConfigSet = Utilities.GetConfigSetName();
            if (message.TimeStamp == null)
                message.TimeStamp = DateTime.UtcNow;
            if (runtime.RequestContext.IsInstance() && runtime.RequestContext.RequestHeader.IsInstance())
            {
                message.ReferingMessageId = runtime.RequestContext.RequestHeader.MessageId;

            }
            string supportCode;
            if (runtime.TryGetSupportCode(out supportCode)) message.SupportCode = supportCode;
            else
            {
                if (runtime.RequestContext.IsInstance() && runtime.RequestContext.RequestHeader.IsInstance())
                {
                    message.SupportCode = runtime.RequestContext.RequestHeader.SupportCode;
                }
            }
        }
        public object BeforeCall(string operationName, object[] inputs)
        {
            var runtime = RuntimeFactory.Current;
            try
            {

                if (inputs.IsNull()) return null;
                bool isStardustRequest = false;
                foreach (var input in inputs)
                {
                    if (!(input is IRequestBase) && !(input is RequestHeader)) continue;
                    var message = input as IRequestBase;
                    var header = input as RequestHeader;
                    if (header.IsNull())
                        header = message.RequestHeader;
                    if (header.IsNull())
                    {
                        message.RequestHeader = header = new RequestHeader();
                    }
                    CreateRequestHeader(operationName, header);
                    isStardustRequest = true;
                }
                if (isStardustRequest)
                {
                    var header = CreateRequestHeader(operationName, new RequestHeader());
                    GetScope().Bind(typeof(RequestHeader), header);
                }
            }
            catch (Exception ex)
            {
                ex.Log();
            }
            return runtime;
        }
 private RequestHeader CreateRequestHeader(string operationName, RequestHeader requestHeader)
 {
     requestHeader.MethodName = operationName;
     requestHeader.RuntimeInstance = RuntimeFactory.GetInstanceId().ToString();
     requestHeader.ServerIdentity = Environment.MachineName;
     SetMessageValuesIfNotSetInClient(requestHeader, RuntimeFactory.Current);
     return requestHeader;
 }
Beispiel #5
0
 internal protected RequestBase(RequestHeader header)
 {
     requestHeader = header;
 }
Beispiel #6
0
 protected RequestMessageContractBase()
 {
     requestHeader = new RequestHeader();
 }
Beispiel #7
0
 internal protected RequestBase(RequestHeader header)
 {
     requestHeader = header;
 }