question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding Context Items for services

See original GitHub issue

I implemented a custom client filter:

	public class AppendIdentifierFilter: IClientFilter
	{
		public async ValueTask<ResponseContext> SendAsync(RequestContext context, Func<RequestContext, ValueTask<ResponseContext>> next)
		{
			context.Items.Add("test", "foo);
			return await next(context);
		}
	}

and tried to access it in my service on the server-side:

public async UnaryResult<Nil> Test()
		{
			Logger.Info($"we have the items {string.Join(", ", Context.Items.Keys)}");
....

But the items dictionary appears to be empty.

To initialize the service I used the following way:

MagicOnionClient.Create<ITestService>(_channel, new IClientFilter[]
				{
					new AppendIdentifierFilter(_identifier), 
				});

From the documentation, I could not figure out what part was still missing.

Thank you for any help

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mayukicommented, Feb 20, 2020

Sorry. Certainly the example code does not work properly. To add a header to the request, use WithHeaders method.

var client = MagicOnionClient.Create<IMyService>(channel)
    .WithHeaders(new Metadata
    {
        { "x-foo", "Bar" }
    });

await client.MethodBazAsync();
1reaction
mayukicommented, Nov 22, 2019

There are no sharings between RequestContext (Client) and CallContext (Server). For example, you can use CallContext.Items for sharing values between MagicOnionFilters and a service method in the server-side per call.

If you want to pass the key-values with request from client to server, use RequestContext.CallOptions.Headers (Client) and CallContext.RequestHeaders (Server).

// Server: Client filter
public class MyClientFilter : IClientFilter
{
    public async ValueTask<ResponseContext> SendAsync(RequestContext context, Func<RequestContext, ValueTask<ResponseContext>> next)
    {
        context.CallOptions.Headers.Add("x-foo", "Bar");
        return await next(context);
    }
}

// Server: Service filter
public class MyFilterAttribute : MagicOnionFilterAttribute
{
    public override async ValueTask Invoke(ServiceContext context, Func<ServiceContext, ValueTask> next)
    {
        var foo = context.CallContext.RequestHeaders.Get("x-foo");
        await next(context);
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Add context menu items to services (windows)
I am trying to a delete option to the right click menu of windows services, I know that you can do this with...
Read more >
How to add a mac OS service item directly in the contextual ...
I want my context menu item directly in the context menu regardless of how many services does the particular user has enabled. –...
Read more >
chrome.contextMenus - Chrome Developers
Use the chrome.contextMenus API to add items to Google Chrome's context menu. You can choose what types of objects your context menu additions...
Read more >
Access HttpContext in ASP.NET Core
HttpContext encapsulates all information about an individual HTTP request and response. An HttpContext instance is initialized when an HTTP ...
Read more >
Adding a list of objects to Context Data returned by Layout ...
I was working on a project that needed some data passed on through the context. Logic was implemented based on https://jss.sitecore.com/docs/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found