Created
May 19, 2016 08:51
-
-
Save arman-hpp/d1591d31af2932a7c8aca8da246599d2 to your computer and use it in GitHub Desktop.
Add Header to WCF packets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var ret1 = ServiceFactory.BaseInfoService.Instance.GetData(15); | |
ServiceFactory.BaseInfoService.SetClientContext(new ClientContext(332, 13)); | |
var ret2 = ServiceFactory.BaseInfoService.Instance.GetData(1055); | |
Console.WriteLine(ret1 + " " + ret2); | |
Console.ReadKey(); | |
} | |
} | |
public void SetClientContext(ClientContext context) | |
{ | |
var contextChannel = (IContextChannel)Instance; | |
var scope = new OperationContextScope(contextChannel); | |
var header = MessageHeader.CreateHeader("ClientContext", "", context); | |
OperationContext.Current.OutgoingMessageHeaders.Clear(); | |
OperationContext.Current.OutgoingMessageHeaders.Add(header); | |
} | |
[Serializable] | |
public class ClientContext | |
{ | |
public ClientContext(int userId, int branchId) | |
{ | |
this.UserId = userId; | |
this.BranchId = branchId; | |
} | |
public int UserId { get; set; } | |
public int BranchId { get; set; } | |
} | |
public static class WcfSecurityHelper | |
{ | |
public static ClientContext GetClientContext() | |
{ | |
var opContext = OperationContext.Current; | |
var requestContext = opContext.RequestContext; | |
var headers = requestContext.RequestMessage.Headers; | |
var headerIndex = headers.FindHeader("ClientContext", ""); | |
if (headerIndex >= 0) | |
return headers.GetHeader<ClientContext>(headerIndex); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment