开发者

How to use ServiceRoutes while defining maxReceivedMessageSize for non-custom bindings in WCF

开发者 https://www.devze.com 2023-04-10 01:45 出处:网络
Editing this to refocus on the actual issue.I\'ve preserved the origional question at the bottom of the message but changing the title and content to reflect what was really happening.

Editing this to refocus on the actual issue. I've preserved the origional question at the bottom of the message but changing the title and content to reflect what was really happening.

I need to override the maxReceivedMessageSize for a WCF service added to an MVC3 project via the ServiceRoute mechanism. Specifing the binding in the web.config doesn't work. How does one do this.


Initial question is below this line but is misleading based on lots of false positives I was seeing.

Hi I have used some examples to add a file streaming upload service to my MVC3 project. If I use the default bindings (i.e., not defined in web.config) the service works as long as I don't exceed the 64k default size. When I try and define my own binding to increase the size I get a content-type mismatch in my trace and a HTTP415 Unsupported Media Type in the response. I'm trying to call this via fiddler via HTTP and am not using a WCF client. Here is the error in the trace:

Content Type image/jpeg was sent to a service expecting multipart/related;type="application/xop+xml".  The client and service bindings may be mismatched.

Here is the web.config service model section

 <system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="NewBehavior0" />
  </endpointBehaviors>
</behaviors>
<services>
  <service name="AvyProViewer.FileService">
    <endpoint address="UploadFile" binding="basicHttpBinding" bindingConfiguration=""
      contract="AvyProViewer.FileService"  />
  </service>
</services>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<bindings>
  <basicHttpBinding>
    <binding name="NewBinding0" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
      messageEncoding="Mtom" transferMode="StreamedRequest">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
        maxArrayL开发者_如何学Goength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>

Here is the service:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class FileService
{      
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "UploadFile")]       
    public string UploadFile(Stream fileStream)       
    {
        string path = HostingEnvironment.MapPath("~");  
        string fileName = Guid.NewGuid().ToString() + ".jpg";
        FileStream fileToupload = new FileStream(path + "\\FileUpload\\" + fileName, FileMode.Create);

        byte[] bytearray = new byte[10000];
        int bytesRead, totalBytesRead = 0;
        do
        {
            bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);

        fileToupload.Write(bytearray, 0, bytearray.Length);
        fileToupload.Close();
        fileToupload.Dispose();
        return fileName;
    }
}

And here is where I expose it in my MVC3 routes:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.Add(new ServiceRoute("FileService", new WebServiceHostFactory(), typeof(FileService)));
        . . .
}


I think the issue is with the mtom declaration for messageEncoding in your binding. Try changing messageEncoding to Text.


Answer ended up being a combination of three different stack overflow posts. None by themselves solved the question but each provided crucial clues as to what was happing.

  1. It seems that if you add a ServiceRoute the web.config binding information is ignored. This SO post clued me in to what seems to be undocumented behavior of this function: Unable to set maxReceivedMessageSize through web.config

  2. I then used this post to determine how to programatically override the maxreceivedmesssagesize for the binding: Specifying a WCF binding when using ServiceRoute.

  3. Unfortunately the code form #2 didn't work out of the box (not sure if the binding behavior for ServiceRoute has changed or what makes the difference). Turns out that if you specify a ServiceRoute its automatically created as a CustomBinding which can't be cast to the WebHTTPBinding type used in #2. So this post: How to set the MaxReceivedMessageSize programatically when using a WCF Client? helped me determine how to change the code in #2 to add this capability to a custom binding.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号