Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 869927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:23:12+00:00 2026-05-15T10:23:12+00:00

This is my first step into the world of stackoverflow, so apologies if I

  • 0

This is my first step into the world of stackoverflow, so apologies if I cock anything up.

I’m trying to create a WCF Operation which has a parameter that is not exposed to the outside world, but is instead automatically passed into the function.

So the world sees this: int Add(int a, int b)

But it is implemented as: int Add(object context, int a, int b)

Then, the context gets supplied by the system at run-time. The example I’m working with is completely artificial, but mimics something that I’m looking into in a real-world scenario.

I’m able to get close, but not quite the whole way there.

First off, I created a simple method and wrote an application to confirm it works. It does. It returns a + b and writes the context as a string to my debug. Yay.

    [OperationContract]
    int Add(object context, int a, int b);

I then wrote the following code:

public class SupplyContextAttribute : Attribute, IOperationBehavior
{
    public void Validate(OperationDescription operationDescription)
    {
        if (!operationDescription.Messages.Any(m => m.Body.Parts.First().Name == "context"))
            throw new FaultException("Parameter 'context' is missing.");
    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        dispatchOperation.Invoker = new SupplyContextInvoker(dispatchOperation.Invoker);
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
    }

    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {
        // Remove the 'context' parameter from the inbound message
        operationDescription.Messages[0].Body.Parts.RemoveAt(0);
    }
}

public class SupplyContextInvoker : IOperationInvoker
{
    readonly IOperationInvoker _invoker;

    public SupplyContextInvoker(IOperationInvoker invoker)
    {
        _invoker = invoker;
    }

    public object[] AllocateInputs()
    {
        return _invoker.AllocateInputs().Skip(1).ToArray();
    }

    private object[] IntroduceContext(object[] inputs)
    {
        return new[] { "MyContext" }.Concat(inputs).ToArray();
    }

    public object Invoke(object instance, object[] inputs, out object[] outputs)
    {
        return _invoker.Invoke(instance, IntroduceContext(inputs), out outputs);
    }

    public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
    {
        return _invoker.InvokeBegin(instance, IntroduceContext(inputs), callback, state);
    }

    public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
    {
        return _invoker.InvokeEnd(instance, out outputs, result);
    }

    public bool IsSynchronous
    {
        get { return _invoker.IsSynchronous; }
    }
}

And my WCF operation now looks like this:

    [OperationContract, SupplyContext]
    int Amend(object context, int a, int b);

My updated references no longer show the ‘context’ parameter, which is exactly what I want.

The trouble is that whenver I run the code, it gets past the AllocateInputs and then throws an Index was outside the bounds of the Array. error somewhere in the WCF guts.

I’ve tried other things, and I find that I can successfully change the type of the parameter and rename it and have my code work. But the moment I remove the parameter it falls over.

Can anyone give me some idea of how to get this to work (or if it can be done at all).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:23:13+00:00Added an answer on May 15, 2026 at 10:23 am

    Well, I figured it out on my own. The MessagePartDescription has an Index property. I just need to resynch these values.

        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
            var parts = operationDescription.Messages[0].Body.Parts;
            parts.RemoveAt(0);
            for (int i = 0; i < parts.Count; i++)
                parts[i].Index = i;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This project is the probable first step in migrating a large CMS from Classic
this is my first question to stackoverflow so here it goes... I use cruise
This is my first post to stackoverflow, so bear with me. :) I am
problem euler #5 i found the solution but i don't know why this first
This is my first post here and I wanted to get some input from
This is my first experience using the Zend Framework. I am attempting to follow
This is my first time attempting to call an ASP.NET page method from jQuery.
this is my first question here so I hope I can articulate it well
This is my first time using joomla. I don't know if I'm using the
This is my first crack at a method that is run periodically during the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.