Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpContext.WebContentTypeMapper/GetMessageFormatForContentType中当前为null_C#_Web Services_Iis - Fatal编程技术网

C# HttpContext.WebContentTypeMapper/GetMessageFormatForContentType中当前为null

C# HttpContext.WebContentTypeMapper/GetMessageFormatForContentType中当前为null,c#,web-services,iis,C#,Web Services,Iis,我想编写一个WebContentTypeMapper,它返回依赖于uri路径的WebContentFormat 到目前为止,我使用的是HttpContext.Current.Request.PathInfo。 但是,如果存在正文超过64k的大型POST请求,则HttpContext.Current为空 我发现HttpContext.Current不打算在那里使用。但当请求只有几kb时,它在所有其他情况下都有效 在GetMessageFormatForContentType()中是否有其他方法可以

我想编写一个
WebContentTypeMapper
,它返回依赖于uri路径的
WebContentFormat

到目前为止,我使用的是
HttpContext.Current.Request.PathInfo
。 但是,如果存在正文超过64k的大型POST请求,则
HttpContext.Current
为空

我发现HttpContext.Current不打算在那里使用。但当请求只有几kb时,它在所有其他情况下都有效

GetMessageFormatForContentType()
中是否有其他方法可以找到当前请求的路径


我现在有了解决办法。由几个变化组成:

我将
RawContentTypeMapper
更改为始终返回
WebContentFormat.Raw

Web.config:

  • 添加了使用
    RawContentTypeMapper
    的绑定副本,并删除了
    contentTypeMapper
    属性。因此它使用默认映射器

  • 使用使用RawContentTypeMapper的绑定向每个地址(“abc/path1”…)添加了一个端点

  • 使用默认绑定将端点添加到基址“abc”

IMySourceCode.cs:myourcecode.cs:

使用
UriTemplate=”“
创建了一个新的
OperationContract
。在那里,我检查
HttpContext.Current.Request.PathInfo
中的“abc/path1/”、“abc/path2/”。。。并调用原始OperationContract方法

因此,所有OperationContract必须具有相同的签名(此处为void MyMethod(流数据))

添加了Global.asax以将路径“abc/path1”重写为“abc/path1/”…:


您是否尝试过扩展IIS管理器->配置编辑器->system.webServer/serverRuntime/uploadReadAheadSize的值?因为这个值的极限是46kI,我不知道。它的默认值为49152。
public class RawContentTypeMapper : WebContentTypeMapper
{
    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        string pathInfo;
        try
        {
            pathInfo = HttpContext.Current.Request.PathInfo;
        }
        catch (NullReferenceException e)
        {
            // This happens for large POST requests
            pathInfo = ...please help!
        }
        switch (pathInfo)
        {
            case "/abc/path1":
            case "/abc/path2":
                return WebContentFormat.Raw;
            default:
                return WebContentFormat.Default; // No change to default behavior
        }
    }
}
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var pathInfo = HttpContext.Current.Request.PathInfo.ToLowerInvariant();
        if (pathInfo.StartsWith("/abc/"))
        {
            foreach (var s in new[] { "/abc/path1", "/abc/path2" })
            {
                if (pathInfo == s)
                {
                    var rawUrl = HttpContext.Current.Request.RawUrl.ToLower(CultureInfo.InvariantCulture);
                    HttpContext.Current.RewritePath($"{rawUrl}/");
                    break;
                }
            }
        }
    }