/// <summary> /// The GetTIDFromMessage() static function is used to retrieve an ITID interface /// to a TID object based on the MessageHeaders object within a Message. /// If the TID version is not known, the latest TID version will be used to /// deserialize the TID. /// GetTIDFromMessage() returns null if the TID header cannot be found. /// </summary> /// <param name="messageHeadersIn"></param> /// <returns>ITID</returns> public static ITID GetTIDFromMessage(MessageHeaders messageHeadersIn) { // Get the name of the TID namespace from the message header string tidNamespace = ""; foreach (MessageHeaderInfo mhi in messageHeadersIn) { if (mhi.Name != null && mhi.Name.ToUpper().StartsWith("TID")) { tidNamespace = mhi.Name; break; } } // Deserialize the version from message header into a TIDVersion object TIDVersion tidVersion = (tidNamespace.Length > 0) ? messageHeadersIn.GetHeader <TIDVersion>("TID", tidNamespace) : null; // Deserialize the TID header into the version-specific object based on the version returned TIDBase tid = (tidVersion == null) // Add supported versions below ? null : (tidVersion.TidVersion == "01.00.000") ? new TID(messageHeadersIn.GetHeader <TID>("TID", tidNamespace)) // : (tidVersion.TidVersion == "99.99.999") // ? messageHeadersIn.GetHeader<TID_99_99_999>("TID", tidNamespace) // keep updated to latest TID version below : new TID(messageHeadersIn.GetHeader <TID>("TID", tidNamespace)); // Return interface to new object return(tid); }
public void TestGetHeaderNonexistent() { Message m = Message.CreateMessage(MessageVersion.Default, "test", 1); MessageHeaders headers = m.Headers; headers.GetHeader <string>("name", "namespace"); }
public void TestGetHeaderOutOfRange() { Message m = Message.CreateMessage(MessageVersion.Default, "test", 1); MessageHeaders headers = m.Headers; Assert.AreEqual("test", headers.GetHeader <int> (1)); }
public void TestGetHeader() { Message m = Message.CreateMessage(MessageVersion.Default, "test", 1); MessageHeaders headers = m.Headers; Assert.AreEqual("test", headers.GetHeader <string> (0)); }
public static int GetTargetTenantId() { if (OperationContext.Current != null) { MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; foreach (MessageHeaderInfo uheader in headers) { if (uheader.Name == "TargetTenantId") { int internationalHeader = headers.GetHeader <int>( uheader.Name, uheader.Namespace); return(internationalHeader); } } return(0); } //Web Request Context else if (HttpContext.Current.Request.Headers != null) { int targetTenantId = 0; if (!string.IsNullOrEmpty(HttpContext.Current.Request.Headers["TargetTenantId"])) { System.Int32.TryParse(HttpContext.Current.Request.Headers["TargetTenantId"], out targetTenantId); } return(targetTenantId); } return(0); }
/// <summary> /// 请求发送前事件 /// </summary> /// <param name="request">请求消息</param> /// <param name="channel">信道</param> public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) { if (OperationContext.Current != null) { //WCF客户端获取公钥处理 MessageHeaders incomingHeaders = OperationContext.Current.IncomingMessageHeaders; #region # 验证消息头 if (!incomingHeaders.Any(x => x.Name == CommonConstants.WCFAuthenticationHeader && x.Namespace == GlobalSetting.ApplicationId)) { string message = "身份认证消息头不存在,请检查程序!"; NoPermissionException innerException = new NoPermissionException(message); FaultReason faultReason = new FaultReason(message); FaultCode faultCode = new FaultCode(HttpStatusCode.Unauthorized.ToString()); throw new FaultException <NoPermissionException>(innerException, faultReason, faultCode); } #endregion //读取消息头中的公钥 Guid publicKey = incomingHeaders.GetHeader <Guid>(CommonConstants.WCFAuthenticationHeader, GlobalSetting.ApplicationId); //添加消息头 System.ServiceModel.Channels.MessageHeader outgoingheader = System.ServiceModel.Channels.MessageHeader.CreateHeader(CommonConstants.WCFAuthenticationHeader, GlobalSetting.ApplicationId, publicKey); request.Headers.Add(outgoingheader); } return(null); }
/// <summary> /// Get header string as nullable Guid. The header must exist. /// </summary> /// <param name="headerName"></param> /// <param name="headers"></param> /// <returns> /// Return the nullable Guid of the header. /// Return <c>null</c> if the Guid is <c>null</c>, contains only white space or an error occurred /// </returns> public Guid?GetHeaderAsOptionalGuid(string headerName, MessageHeaders headers) { string idString = null; try { idString = headers.GetHeader <string>(headerName, WCFHeaderConstants.MLSNamespace); } catch (MessageHeaderException) { return(null); } if (string.IsNullOrWhiteSpace(idString)) { return(null); } Guid id; if (Guid.TryParse(idString, out id)) { return(id); } return(null); }
public DashboardService() { // https://stackoverflow.com/questions/3277256/identifying-wcf-client-id OperationContext opContext = OperationContext.Current; opContext.Channel.Faulted += new EventHandler(Channel_Faulted); opContext.Channel.Closed += new EventHandler(Channel_Closed); RequestContext requestContext = opContext.RequestContext; MessageHeaders headers = requestContext.RequestMessage.Headers; int headerIndex = headers.FindHeader("ClientIdentification", ""); if (headerIndex >= 0) { ClientId = headers.GetHeader <string>(headerIndex); } else { ClientId = "No ClientIdentification found"; } Client = opContext.GetCallbackChannel <IDashboardServiceCallback>(); IsConnected = true; rnd = new Random(); timer = new Timer(); timer.Interval = 5000; timer.Elapsed += Timer_Elapsed; }
public static void AddHeader <T>(this MessageHeaders headers, string name, T value) { if (headers == null) { throw new InvalidOperationException("No headers could be found in the OperationContext, or the OperationContext does not exist."); } bool headerExists = false; try { var existingHeader = headers.GetHeader <T>(Header <T> .GetFullName(name), Header <T> .TypeNamespace); if (existingHeader != null) { headerExists = true; } } catch (MessageHeaderException) { // Debug.Assert(IsHeaderNotExistsException(exception)); } if (headerExists) { throw new InvalidOperationException("A header with name " + Header <T> .GetFullName(name) + " and namespace " + Header <T> .TypeNamespace + " already exists in the message."); } MessageHeader <Header <T> > genericHeader = new MessageHeader <Header <T> >(new Header <T>(value)); headers.Add(genericHeader.GetUntypedHeader(Header <T> .GetFullName(name), Header <T> .TypeNamespace)); }
public void GetHeaderNullSerializer2() { MessageHeaders headers = new MessageHeaders(MessageVersion.Default); string ns = "http://schemas.xmlsoap.org/ws/2004/08/addressing"; headers.Add(MessageHeader.CreateHeader("Action", ns, "urn:foo")); headers.GetHeader <string> ("Action", ns, (XmlObjectSerializer)null); }
public static ImpersonationHeader GetHeaderFromIncomingMessage() { MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; return((from uheader in headers where uheader.Name == Constants.HeaderName && uheader.Namespace == Constants.Namespace select headers.GetHeader <ImpersonationHeader>(uheader.Name, uheader.Namespace)).FirstOrDefault()); }
public static int GetTenantId() { //WCF Request Context if (OperationContext.Current != null) { CurrentHireProCoreContext currentHireProCoreContext = OperationContext.Current.Extensions.Find <CurrentHireProCoreContext>(); if (currentHireProCoreContext != null && currentHireProCoreContext.IsHireProSession) { return(Constants.HireProTenantId);//HirePro TenantId } var webOperationContext = OperationContext.Current.Extensions.Find <WebOperationContext>(); if (webOperationContext != null) { var value = webOperationContext.IncomingRequest.Headers.Get("TenantId"); if (!string.IsNullOrEmpty(value)) { var tenantId = 0; int.TryParse(value, out tenantId); return(tenantId); } } MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; foreach (MessageHeaderInfo uheader in headers) { if (uheader.Name == "TenantId") { int internationalHeader = headers.GetHeader <int>( uheader.Name, uheader.Namespace); if (internationalHeader == 0) { HirePro.Framework.Core.Exception.ExceptionFactory.DataAccessException.Wrap <InvalidOperationException>().Add.Message ("Please specify valid tenant id").SetSeverity(Exception.Severity.High).Raise(); } return(internationalHeader); } } return(0); } //Web Request Context else if (HttpContext.Current.Request.Headers != null) { int tenantId = 0; if (!string.IsNullOrEmpty(HttpContext.Current.Request.Headers["TenantId"])) { System.Int32.TryParse(HttpContext.Current.Request.Headers["TenantId"], out tenantId); } return(tenantId); } return(0); }
public static WorkUnitInfo GetFrom(MessageHeaders messageHeaders, string workUnitName) { int num = messageHeaders.FindHeader(workUnitName, "http://schemas.microsoft.com/netservices/2011/06/servicebus"); if (num < 0) { return(null); } return(messageHeaders.GetHeader <WorkUnitInfo>(num)); }
/// <summary> /// Gets the data signature in the given message header collection /// </summary> /// <param name="headers">Message headers</param> /// <returns>Data signature or Guid.Empty</returns> public static Guid GetDataSignature(MessageHeaders headers) { Guid dataSignature = Guid.Empty; int headerIndex = headers.FindHeader(Header.Name, Header.Namespace); if (headerIndex != -1) { dataSignature = headers.GetHeader<Guid>(headerIndex); } return dataSignature; }
public static T GetHeader <T>(this MessageHeaders headers, string name) { try { return(headers.GetHeader <Header <T> >(Header <T> .GetFullName(name), Header <T> .TypeNamespace).Value); } catch (MessageHeaderException) { return(default(T)); } }
/// <summary> /// Gets the data signature in the given message header collection /// </summary> /// <param name="headers">Message headers</param> /// <returns>Data signature or Guid.Empty</returns> public static Guid GetDataSignature(MessageHeaders headers) { Guid dataSignature = Guid.Empty; int headerIndex = headers.FindHeader(Header.Name, Header.Namespace); if (headerIndex != -1) { dataSignature = headers.GetHeader <Guid>(headerIndex); } return(dataSignature); }
internal object GetHeader(string name, MessageHeaders headers) { int index = headers.FindHeader(name, headerNamespace); if (index >= 0) { XmlObjectSerializer serializer = new DataContractSerializer(type, name, headerNamespace, null, Int32.MaxValue, false, false, null); return(headers.GetHeader <object>(index, serializer)); } return(null); }
/// <summary> /// 接收请求后事件 /// </summary> /// <param name="request">请求消息</param> /// <param name="channel">信道</param> /// <param name="instanceContext">WCF实例上下文</param> public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { //获取消息头 MessageHeaders headers = request.Headers; string action = headers.Action; EndpointDispatcher endpointDispatcher = OperationContext.Current.EndpointDispatcher; DispatchOperation operationDispatcher = endpointDispatcher.DispatchRuntime.Operations.Single(x => x.Action == action); /* * 通过OperationBehavior设置Impersonation属性, * 默认值为ImpersonationOption.NotAllowed, * 当ImpersonationOption.NotAllowed时验证身份, * 如无需验证身份,则将Impersonation属性赋值为ImpersonationOption.Allowed。 */ if (operationDispatcher.Impersonation == ImpersonationOption.NotAllowed) { #region # 验证消息头 if (!headers.Any(x => x.Name == CommonConstants.WCFAuthenticationHeader && x.Namespace == GlobalSetting.ApplicationId)) { string message = "身份认证消息头不存在,请检查程序!"; NoPermissionException innerException = new NoPermissionException(message); FaultReason faultReason = new FaultReason(message); FaultCode faultCode = new FaultCode(HttpStatusCode.Unauthorized.ToString()); throw new FaultException <NoPermissionException>(innerException, faultReason, faultCode); } #endregion //读取消息头中的公钥 Guid publicKey = headers.GetHeader <Guid>(CommonConstants.WCFAuthenticationHeader, GlobalSetting.ApplicationId); //认证 lock (_Sync) { //以公钥为键,查询分布式缓存,如果有值则通过,无值则不通过 LoginInfo loginInfo = CacheMediator.Get <LoginInfo>(publicKey.ToString()); if (loginInfo == null) { string message = "身份过期,请重新登录!"; NoPermissionException innerException = new NoPermissionException(message); FaultReason faultReason = new FaultReason(message); FaultCode faultCode = new FaultCode(HttpStatusCode.Unauthorized.ToString()); throw new FaultException <NoPermissionException>(innerException, faultReason, faultCode); } //通过后,重新设置缓存过期时间 CacheMediator.Set(publicKey.ToString(), loginInfo, DateTime.Now.AddMinutes(GlobalSetting.AuthenticationTimeout)); } } return(null); }
public static bool TryGetHeader(MessageHeaders headers, out FaultInjectionInfo value) { int num = headers.FindHeader("FaultInjectionInfo", "http://schemas.microsoft.com/netservices/2011/06/servicebus"); if (num == -1) { value = null; return(false); } value = headers.GetHeader <FaultInjectionInfo>(num, FaultInjectionInfo.serializer); return(true); }
public void InspectHeaders(MessageHeaders messageHeaders) { try { var timestamp = messageHeaders.GetHeader <DateTime?>("time", SoapExtensionsNamespace); _notifyCallback(timestamp); } catch (Exception) { _notifyCallback(null); } }
public string GetIncomingMessageHeadersMessage(string customHeaderName, string customHeaderNS) { MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; MesssageHeaderCreateHeaderWithXmlSerializerTestType header = headers.GetHeader <MesssageHeaderCreateHeaderWithXmlSerializerTestType>(customHeaderName, customHeaderNS); if (header != null) { return(header.Message); } return(string.Empty); }
internal object GetHeader(MessageHeaders headers, string name) { var index = headers.FindHeader(name, _headerNamespace); if (index >= 0) { var serializer = new DataContractSerializer(_type, name, _headerNamespace, null, int.MaxValue, false, false, null); return(headers.GetHeader <object>(index, serializer)); } return(null); }
public static IDisposable IsolationFromOperationContext() { MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; int val = headers.FindHeader("CurrentIsolation", "http://www.signumsoftware.com/Isolation"); if (val == -1) { return(null); } return(IsolationEntity.Override(Lite.Parse <IsolationEntity>(headers.GetHeader <string>(val)))); }
string GetHeaderValue(MessageHeaders headers, string name, string ns) { //var headers = OperationContext.Current.IncomingMessageHeaders; var index = headers.FindHeader(name, ns); if (index >= 0) { return(headers.GetHeader <string>(index)); } else { return(null); } }
public static Connection GetHeaderFromMessage() { MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; foreach (var header in headers.UnderstoodHeaders) { if (header.Name == HeaderConstants.Name && header.Namespace == HeaderConstants.NamespaceURI) { return(headers.GetHeader <Connection>(header.Name, header.Namespace)); } } return(null); }
/// <summary> /// Get header as string value. The header must exist. /// </summary> /// <param name="headerName"></param> /// <param name="headers"></param> /// <returns>Return the string read from the header. Return <c>null</c> if the string is <c>null</c> or an error occurred</returns> public string GetHeaderAsString(string headerName, MessageHeaders headers) { string value = null; try { value = headers.GetHeader <string>(headerName, WCFHeaderConstants.MLSNamespace); return(value); } catch (MessageHeaderException) { return(null); } }
public void DoWork() { //check for CustomHeaderValue custom header MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; if (headers.FindHeader("CustomHeaderValue", "ns") != -1) { var customheader = headers.GetHeader <string>("CustomHeaderValue", "ns"); if (customheader != null) { Helper.Log("ServiceDemo.DoWork() was called with custom header CustomHeaderValue: {0}", customheader); } } //check for InspectorHeader custom header if (headers.FindHeader("InspectorHeader", "ns") != -1) { var customheader = headers.GetHeader <string>("InspectorHeader", "ns"); if (customheader != null) { Helper.Log("ServiceDemo.DoWork() was called with header from Inspector InspectorHeader: {0}", customheader); } } }
private void CopyMessageHeader(MessageHeaders messageHeaders, int index) { try { MessageHeaderInfo headerInfo = messageHeaders[index]; XmlQualifiedName qName = new XmlQualifiedName(headerInfo.Name, headerInfo.Namespace); string headerValue = messageHeaders.GetHeader <string>(index); MessageHeader header = MessageHeader.CreateHeader(headerInfo.Name, headerInfo.Namespace, headerValue); MessageHeaders.Add(qName, header); } catch (Exception) { //eats away headers not of the right type } }
private bool NotValid() { if (_guid == null) { return(false); } try { MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; var guid = headers.GetHeader <string>(ServiceClient.GuidHeaderName, ServiceClient.GuidHeaderNamespace); return(_guid != guid); } catch (Exception) { return(true); } }
/// <summary> /// 获取商品集 /// </summary> /// <returns>商品集</returns> public string GetProducts() { Console.WriteLine("Hello World"); string headerName = "headerName"; string headerNs = "headerNs"; MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; if (headers.Any(x => x.Name == headerName && x.Namespace == headerNs)) { string header = headers.GetHeader <string>(headerName, headerNs); Console.WriteLine($"消息头:\"{header}\""); } return("Hello World"); }
/// <summary> /// 获取登录信息 /// </summary> /// <returns>登录信息</returns> public LoginInfo GetLoginInfo() { if (OperationContext.Current != null) { //获取消息头 MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; if (!headers.Any(x => x.Name == CommonConstants.WCFAuthenticationHeader && x.Namespace == GlobalSetting.ApplicationId)) { return(null); } Guid publicKey = headers.GetHeader <Guid>(CommonConstants.WCFAuthenticationHeader, GlobalSetting.ApplicationId); LoginInfo loginInfo = CacheMediator.Get <LoginInfo>(publicKey.ToString()); return(loginInfo); } return(null); }
public void InspectHeaders(MessageHeaders messageHeaders) { try { var timestamp = messageHeaders.GetHeader<DateTime?>("time", SoapExtensionsNamespace); _notifyCallback(timestamp); } catch (Exception) { _notifyCallback(null); } }
/// <summary> /// Set wellknown headers into the CallContext Thread slot /// </summary> /// <param name="headers">List of all message headers</param> /// <param name="actor">Role (actor) of the headers. Note this is also the name of the assembly, where must be declared all CallContext Headers.</param> public static void SetDataContract(MessageHeaders headers, string actor) { if (string.IsNullOrEmpty(actor)) { try { // wellknown header LogicalWorkflowContext LogicalWorkflowContext lwc = headers.GetHeader<LogicalWorkflowContext>("LogicalWorkflowContext", "RKiss.WorkflowRemoting", "WorkflowRemoting"); if(lwc == null) throw new NullReferenceException("Deserializer failed for header 'LogicalWorkflowContext'"); // set data into the Thread slot CallContext.SetData("LogicalWorkflowContext", lwc); } catch (Exception ex) { Trace.WriteLine(ex); } } else { bool bDoneForLWC = false; for (int ii = 0; ii < headers.Count; ii++) { string typeName = string.Concat(headers[ii].Namespace, ".", headers[ii].Name, ",", headers[ii].Actor); if (!bDoneForLWC && typeName == "RKiss.WorkflowRemoting.LogicalWorkflowContext,WorkflowRemoting") { object lwc = headers.GetHeader<LogicalWorkflowContext>(ii); if (lwc == null) throw new NullReferenceException("Deserializer failed for header 'LogicalWorkflowContext'"); // set data into the Thread slot CallContext.SetData("LogicalWorkflowContext", lwc); // next header bDoneForLWC = true; continue; } else if (!string.IsNullOrEmpty(headers[ii].Actor) && string.Concat(actor, ",").Contains(string.Concat(headers[ii].Actor, ","))) { try { Type type = Type.GetType(typeName); if (type == null) throw new TypeLoadException(typeName); // deserializer DataContractSerializer dcs = new DataContractSerializer(type, headers[ii].Name, headers[ii].Namespace); object data = dcs.ReadObject(headers.GetReaderAtHeader(ii), true); if (data == null) throw new NullReferenceException(string.Format("Deserializer failed for header '{0}'", headers[ii].Name)); // set data into the Thread slot CallContext.SetData(headers[ii].Name, data); } catch (Exception ex) { Trace.WriteLine(ex); } } } } }
public void GetHeaderNullSerializer2 () { MessageHeaders headers = new MessageHeaders (MessageVersion.Default); string ns = "http://schemas.xmlsoap.org/ws/2004/08/addressing"; headers.Add (MessageHeader.CreateHeader ("Action", ns, "urn:foo")); headers.GetHeader<string> ("Action", ns, (XmlObjectSerializer) null); }