开发者

C# WCF Endpoint Bindings and File Upload

开发者 https://www.devze.com 2023-03-13 11:20 出处:网络
I am having some issue configuring the maxRecievedMessageSite property of a basicHttpBinding. I want to create an endpoint to upload potentially large files however the current (I assume default) rest

I am having some issue configuring the maxRecievedMessageSite property of a basicHttpBinding. I want to create an endpoint to upload potentially large files however the current (I assume default) restriction is 64K. For items such as images and large documents, this limit just won't work. I have created a new binding from the basicHttpBinding but i keep getting errors in the stack trace letting me know that I have exceeded the limit. Below I have included the relavent parts from my configuration file, Global.asax file, my operational contract, and my service implementation. (all very simple, just trying to get it to work).

Configuration File

<system.webServer>
  <system.serviceModel>
    <services>
      <!-- CarImage Service Configuration -->
      <service name="CarImageService">
        <endpoint address="" 
                  binding="basicHttpBinding" 
                  bindingConfiguration="FileTransferServicesBinding"
                  contract="ICarImageService" />
      </service>
    </services>

    <bindings>
      <basicHttpBinding>
        <!-- buffer: 64KB; max size: 64MB -->
        <binding name="FileTransferServicesBinding"
                 closeTimeout="00:01:00"
                 openTimeout="00:01:00"
                 receiveTimeout="00:01:00"
                 sendTimeout="00:01:00"
                 transferMode="Streamed"
                 messageEncoding="Mtom"
                 maxBufferSize="999999"
                 maxReceivedMessageSize="67108864"
                 maxBufferPoolSize="99999">
           <security mode="None" >
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

</system.serviceModel>

Global.asax File

namespace Mavizon.Mavia.Services
{
    public class Global : HttpApplication
    {
        private string FilePath = ConfigurationManager.AppSettings["ImageFolder"];

        void Application_Start(object sender, EventArgs e)
        {
            if (!Directory.Exists(FilePath))
            {
                Directory.CreateDirectory(FilePath);
            }
            RegisterRoutes();
        }

        private void RegisterRoutes()
        {
            RouteTable.Routes.Add(new ServiceRoute("test", new WebServiceHostFactory(), typeof(CarImageService)));
        }
    }
}

Operational Contract

namespace Mavizon.Mavia.Services.OperationContracts
{
    [ServiceContract]
    public interface ICarImageService
    {
        [OperationContract]
        [FaultContract(typeof(ErrorFault))]
        [WebInvoke(Method = "POST", UriTemplate = "/")]
        void UploadImage(Stream request);
    }
}

My (very) Simple implementation class

namespace Mavizon.Mavia.Services.WebServices
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CarImageService : OperationContracts.ICarImageService
    {
        public void UploadImage(Stream request)
        {

        }
    }
}

** Here is my stack trace informaiton **

The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.
<StackTrace>   
   at System.ServiceModel.Channels.HttpInput.ThrowHttpProtocolException(String message, HttpStatusCode statusCode, String statusDescription)
   at System.ServiceModel.Channels.HttpInput.ThrowMaxReceivedMessageSizeExceeded()
   at System.ServiceModel.Channels.HttpInput.GetMessageBuffer()
   at System.ServiceModel.Channels.HttpInput.ReadBufferedMessage(Stream inputStream)
   at System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(Exception&amp;amp; requestException)
   at System.ServiceModel.Channels.HttpRequestContext.CreateMessage()
   at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
   at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
   at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
   at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
   at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
   at System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state)
   at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequestWithFlow(Object state)
   at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
   at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error,开发者_JS百科 UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace><ExceptionString>System.ServiceModel.ProtocolException: The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.</ExceptionString><DataItems><Data><Key>System.ServiceModel.Channels.HttpInput.HttpStatusCode</Key><Value>BadRequest</Value></Data></DataItems></Exception></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2011-06-15T18:56:04.3623047Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{c05370d6-4c2f-4320-882f-365bf055e88f}" RelatedActivityID="{1782abf6-9c78-487d-91b0-29586d6140ad}" /><Execution ProcessName="WebDev.WebServer40" ProcessID="2268" ThreadID="5" /><Channel/><Computer>JOHNMURRAY027D</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Stop">0</SubType><Level>255</Level><TimeCreated SystemTime="2011-06-15T18:56:04.3623047Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{c05370d6-4c2f-4320-882f-365bf055e88f}" /><Execution ProcessName="WebDev.WebServer40" ProcessID="2268" ThreadID="5" /><Channel/><Computer>JOHNMURRAY027D</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Stop"><TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Activity boundary.</Description><AppDomain>f5dae6e-2-129526377562529297</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Processing message 1.</ActivityName><ActivityType>ProcessMessage</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Resume">0</SubType><Level>255</Level><TimeCreated SystemTime="2011-06-15T18:56:04.3623047Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{1782abf6-9c78-487d-91b0-29586d6140ad}" /><Execution ProcessName="WebDev.WebServer40" ProcessID="2268" ThreadID="5" /><Channel/><Computer>JOHNMURRAY027D</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Resume"><TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Activity boundary.</Description><AppDomain>f5dae6e-2-129526377562529297</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Listen at 'http://localhost:2947/test'.</ActivityName><ActivityType>ListenAt</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2011-06-15T18:59:36.5878906Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" RelatedActivityID="{a7f8d8f5-ff82-4283-9ea2-4eb8a55a51a5}" /><Execution ProcessName="WebDev.WebServer40" ProcessID="2268" ThreadID="5" /><Channel/><Computer>JOHNMURRAY027D</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Start">0</SubType><Level>255</Level><TimeCreated SystemTime="2011-06-15T18:59:36.5878906Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{a7f8d8f5-ff82-4283-9ea2-4eb8a55a51a5}" /><Execution ProcessName="WebDev.WebServer40" ProcessID="2268" ThreadID="5" /><Channel/><Computer>JOHNMURRAY027D</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Start"><TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Activity boundary.</Description><AppDomain>f5dae6e-2-129526377562529297</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Close ServiceHost 'Mavizon.Mavia.Services.WebServices.CarImageService'.</ActivityName><ActivityType>Close</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2011-06-15T18:59:36.5878906Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{a7f8d8f5-ff82-4283-9ea2-4eb8a55a51a5}" RelatedActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="WebDev.WebServer40" ProcessID="2268" ThreadID="5" /><Channel/><Computer>JOHNMURRAY027D</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Stop">0</SubType><Level>255</Level><TimeCreated SystemTime="2011-06-15T18:59:36.5878906Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{a7f8d8f5-ff82-4283-9ea2-4eb8a55a51a5}" /><Execution ProcessName="WebDev.WebServer40" ProcessID="2268" ThreadID="5" /><Channel/><Computer>JOHNMURRAY027D</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Stop"><TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Activity boundary.</Description><AppDomain>f5dae6e-2-129526377562529297</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Close ServiceHost 'Mavizon.Mavia.Services.WebServices.CarImageService'.</ActivityName><ActivityType>Close</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2011-06-15T18:59:36.5878906Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{a7f8d8f5-ff82-4283-9ea2-4eb8a55a51a5}" RelatedActivityID="{1782abf6-9c78-487d-91b0-29586d6140ad}" /><Execution ProcessName="WebDev.WebServer40" ProcessID="2268" ThreadID="5" /><Channel/><Computer>JOHNMURRAY027D</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2011-06-15T18:59:36.5878906Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{1782abf6-9c78-487d-91b0-29586d6140ad}" RelatedActivityID="{a7f8d8f5-ff82-4283-9ea2-4eb8a55a51a5}" /><Execution ProcessName="WebDev.WebServer40" ProcessID="2268" ThreadID="5" /><Channel/><Computer>JOHNMURRAY027D</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Stop">0</SubType><Level>255</Level><TimeCreated SystemTime="2011-06-15T18:59:36.5878906Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{1782abf6-9c78-487d-91b0-29586d6140ad}" /><Execution ProcessName="WebDev.WebServer40" ProcessID="2268" ThreadID="5" /><Channel/><Computer>JOHNMURRAY027D</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Stop"><TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Activity boundary.</Description><AppDomain>f5dae6e-2-129526377562529297</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Listen at 'http://localhost:2947/test'.</ActivityName><ActivityType>ListenAt</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent>

** And the raw request that I am making is as follows **

POST http://localhost:2947/test/ HTTP/1.1
User-Agent: Fiddler
Host: localhost:2947
Content-Length: 150732

/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/bAIQABgQEBAUEBgUFBgkGBQYJCwgGBggLDAoKCwoKDBAMDAwMDAwQDA4PEA8ODBMTFBQTExwbGxscHx8fHx8fHx8fHwEHBwcNDA0YEBAYGhURFRofHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8f/8AAEQgDAgQAAwERAAIRAQMR...(on and on and on..lol)

Any help is greatly appreciated!


Two problems:

  1. You're using WCF configuration for your service endpoint with an address of "", but then using code to spin the service up using ASP.NET routing at an address of "/test". You should just need to align the config address with the routing address for the endpoint configuration to be picked up.

  2. You're configuring your service with a basicHttpBinding, but at runtime you're specifying WebServiceHostFactory which will use a webHttpBinding (i.e. REST support). You want to change your config to webHttpBinding. You would have gotten this error next had it found the endpoint for your service because the binding types would have mismatched.

UPDATE

I realized this morning that #1 was bad advice. You can't actually mix/match WCF service/endpoint configuration with ServiceRoute registration. So you have two options to apply a specific binding configuration to your ServiceRoutes:

  1. Configure a default for webHttpBinding. This is definitely the easiest way to do it as it's all config and no code changes required to what you're already doing. See below for an example of this.

  2. Write custom WebServiceHostFactory and WebServiceHost subclasses where you then override the creation of the host and configure the specific binding information you want. This is a better approach when you're going to be writing a bunch of service projects since, instead of all the projects having to all make sure to make the correct configuration entries are applied, they can just share the factory implementation via reference and just do new MyWebServiceHostFactory().

Example default binding config:

<system.serviceModel>
    <bindings>
        <webHttpBinding>
            <!-- no name attribute on a binding means it's the default for the biding type -->
            <binding transferMode="Streamed"
                     maxReceivedMessageSize="67108864">
               <!-- other config here -->
            </binding>
        </webHttpBinding>
    </bindings>
</system.serviceModel>
0

精彩评论

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

关注公众号