编辑此内容以重新关注实际问题。我在邮件底部保留了原始问题,但更改了标题和内容以反映实际发生的情况。
我需要通过ServiceRoute机制覆盖添加到MVC3项目的WCF服务的maxReceivedMessageSize。在web.config中指定绑定不起作用。如何做到这一点。
最初的问题是在这条线下面,但是基于我看到的许多误报而误导。
您好我使用了一些示例将文件流上传服务添加到我的MVC3项目中。如果我使用默认绑定(即,未在web.config中定义),只要我没有超过64k默认大小,服务就会起作用。当我尝试定义自己的绑定以增加大小时,我的跟踪中的内容类型不匹配以及响应中的HTTP415 Unsupported Media Type。我试图通过HTTP通过fiddler调用它,而不是使用WCF客户端。 以下是跟踪中的错误:
Content Type image/jpeg was sent to a service expecting multipart/related;type="application/xop+xml". The client and service bindings may be mismatched.
这是web.config服务模型部分
<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"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
这是服务:
[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;
}
}
这是我在MVC3路线中公开它的地方:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new ServiceRoute("FileService", new WebServiceHostFactory(), typeof(FileService)));
. . .
}
答案 0 :(得分:0)
我认为问题在于绑定中mtom
的{{1}}声明。尝试将messageEncoding更改为messageEncoding
。
答案 1 :(得分:0)
答案最终是三个不同的堆栈溢出帖子的组合。没有人自己解决了这个问题,但每个人都提供了关于什么是讨价还价的重要线索。
似乎如果添加ServiceRoute,则会忽略web.config绑定信息。这篇SO帖子让我了解了这个函数似乎没有文档的行为:Unable to set maxReceivedMessageSize through web.config
然后我使用这篇文章来确定如何以编程方式覆盖绑定的maxreceivedmesssagesize:Specifying a WCF binding when using ServiceRoute。
不幸的是,代码形式#2没有开箱即用(不确定ServiceRoute的绑定行为是否已经改变或者是什么产生了影响)。事实证明,如果你指定一个ServiceRoute,它会自动创建为一个CustomBinding,它不能转换为#2中使用的WebHTTPBinding类型。所以这篇文章:How to set the MaxReceivedMessageSize programatically when using a WCF Client?帮助我确定了如何更改#2中的代码以将此功能添加到自定义绑定。