Asked  7 Months ago    Answers:  5   Viewed   32 times

I have a WCF service that is hosted in a Windows Service. Clients that using this service must pass an identifier every time they're calling service methods (because that identifier is important for what the called method should do). I thought it is a good idea to somehow put this identifier to the WCF header information.

If it is a good idea, how can I add the identifier automatically to the header information. In other words, whenever the user calls the WCF method, the identifier must be automatically added to the header.

UPDATE: Clients that are using the WCF service are both Windows applications and Windows Mobile application (using Compact Framework).

 Answers

65

The advantage to this is that it is applied to every call.

Create a class that implements IClientMessageInspector. In the BeforeSendRequest method, add your custom header to the outgoing message. It might look something like this:

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
    HttpRequestMessageProperty httpRequestMessage;
    object httpRequestMessageObject;
    if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
    {
        httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
        if (string.IsNullOrEmpty(httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER]))
        {
            httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER] = this.m_userAgent;
        }
    }
    else
    {
        httpRequestMessage = new HttpRequestMessageProperty();
        httpRequestMessage.Headers.Add(USER_AGENT_HTTP_HEADER, this.m_userAgent);
        request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
    }
    return null;
}

Then create an endpoint behavior that applies the message inspector to the client runtime. You can apply the behavior via an attribute or via configuration using a behavior extension element.

Here is a great example of how to add an HTTP user-agent header to all request messages. I am using this in a few of my clients. You can also do the same on the service side by implementing the IDispatchMessageInspector.

Is this what you had in mind?

Update: I found this list of WCF features that are supported by the compact framework. I believe message inspectors classified as 'Channel Extensibility' which, according to this post, are supported by the compact framework.

Tuesday, June 1, 2021
 
TMichel
answered 7 Months ago
41

There are several solutions depending on what you need...

If you want to add a custom header (or set of headers) to an individual request then just add the headers property:

// Request with custom header
$.ajax({
    url: 'foo/bar',
    headers: { 'x-my-custom-header': 'some value' }
});

If you want to add a default header (or set of headers) to every request then use $.ajaxSetup():

$.ajaxSetup({
    headers: { 'x-my-custom-header': 'some value' }
});

// Sends your custom header
$.ajax({ url: 'foo/bar' });

// Overwrites the default header with a new header
$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } });

If you want to add a header (or set of headers) to every request then use the beforeSend hook with $.ajaxSetup():

$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('x-my-custom-header', 'some value');
    }
});

// Sends your custom header
$.ajax({ url: 'foo/bar' });

// Sends both custom headers
$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } });

Edit (more info): One thing to be aware of is that with ajaxSetup you can only define one set of default headers and you can only define one beforeSend. If you call ajaxSetup multiple times, only the last set of headers will be sent and only the last before-send callback will execute.

Tuesday, June 1, 2021
 
cbcp
answered 7 Months ago
91

Open your project properties, go to the Debug tab, under start options you will see something like /client:"WcfTestClient.exe"

delete that line.

For a WCF Service Application and WCF Workflow Service Application you need to modify the .csproj.user file. A full write up can be found here.

The key part is

    <WebProjectProperties>
      <EnableWcfTestClientForSVC>False</EnableWcfTestClientForSVC>
    </WebProjectProperties>
Sunday, August 8, 2021
 
smogg
answered 4 Months ago
16

The following httpCustom could be used:

"httpCustom": {
  "url": "http://example.com/some/path",
  "headers": {
    "X-Auth-Token": "n5u43SunZCGX0AbnD9e8R537eDslLM"
  }
}

The token will expire from time to time. Thus, it should be renewed at a regular interval, modifying the httpCustom element with the new token (using PATCH /v2/subscriptions/<id> operation, see NGSIv2 specification for details).

Regarding Rush, it is not necesary if you can achieve the HTTP-to-HTTPS with some equivalent software. Native HTTPS notifications at Orion has been identified as a topic of interest and even there was a pull request with code implementation in that line. Currently it isn't a priority, however contributions related with this are welcomed :)

EDIT: the above httpCustom configuration will make Orion sending notifications including that X-Auth-Token header. Thus, it is supposed that at the url endpoint (e.g. http://example.com/some/path in the example above) the authentication elements able to process the X-Auth-Token header will be listening (typically, a Policy Enforcement Point -PEP- Proxy).

UPDATE: since verion 1.7.0, Orion implements native HTTPS notifications (i.e. without needing Rush).

Saturday, August 21, 2021
 
christina
answered 4 Months ago
58

Remove the MEX endpoint and leave . Mex endpoints require anonymous authentication to be enabled.

Friday, September 24, 2021
 
Ctpelnar1988
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share