Property httpRequest' not present

前端 未结 1 1447
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 02:14

I\'m receiving this error when I run a build outside Visual Studio:

\"A property with the name \'httpRequest\' is not present\"

If I run the SAME code inside Vis

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-21 02:21

    I was having the exact same problem. I found the solution on the MSDN forum. I am posting it here as well because it was hard to find the solution, and I checked this Stack Overflow question first.

    Apparently, when you run in the debugger, something it does initializes the HttpRequestMessageProperty, but when you run in a regular runtime environment, this Message property does not get initialized. I don't know what other Message properties might be missing, but probably others as well.

    So you can write some code to create a new one and add it to the Message properties if it doesn't already exist. This seems to be safe to do. The header being added by my EndpointBehavior was indeed added and sent to the server.

    private HttpRequestMessageProperty GetHttpRequestMessageProperty(Message request)
    {
        if (!request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
        {
            request.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());
        }
    
        return request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
    }
    

    Thanks to SteveSteveSteveP on the MSDN forums for the solution!

    0 讨论(0)
提交回复
热议问题