编辑此内容以重新关注实际问题。我保留了消息底部的原始问题,但更改了标题和内容以反射(reflect)实际发生的情况。
我需要重写通过 ServiceRoute 机制添加到 MVC3 项目的 WCF 服务的 maxReceivedMessageSize。在 web.config 中指定绑定(bind)不起作用。如何做到这一点。
<小时/>最初的问题低于此线,但基于我看到的大量误报而具有误导性。
您好,我使用了一些示例将文件流上传服务添加到我的 MVC3 项目中。如果我使用默认绑定(bind)(即未在 web.config 中定义),只要我不超过 64k 默认大小,该服务就可以工作。当我尝试定义自己的绑定(bind)以增加大小时,我的跟踪中出现内容类型不匹配的情况,并且响应中出现 HTTP415 不支持的媒体类型。我正在尝试通过 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)));
. . .
}
最佳答案
我认为问题在于绑定(bind)中 messageEncoding
的 mtom
声明。尝试将 messageEncoding 更改为 Text
。
关于wcf - 如何在 WCF 中为非自定义绑定(bind)定义 maxReceivedMessageSize 时使用 ServiceRoutes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7638620/