I need to set a specific content type(application/pdf) for the binary part in an MTOM configured binding.
Is there anything in the config or in code I can do to have the content type set? For the filepart it's always "Content-Type: application/octet-stream"
Config:
<binding name="FileTransferServicesBinding"
closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00"
messageEncoding="Mtom" transferMode="Buffered"
maxBufferSize="67108864" maxReceivedMessageSize="67108864">
<security mode="Transport" />
</binding>
What's currently sent to the service. The multipart header for the file should be application/pdf instead of application/octetstream
POST https://somewebservice HTTP/1.1
MIME-Version: 1.0
Authorization: Basic authhash
SOAPAction: ""
Host: webservicehost
Content-Length: 24517
Expect: 100-continue
Accept-Encoding: gzip, deflate
--uuid:b8366a06-3ecc-4bc4-9809-8c87ad459981+id=1
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header>[The soapmessageitself]</s:Envelope>
--uuid:b8366a06-3ecc-4bc4-9809-8c87ad459981+id=1
Content-ID: <http://tempuri.org/1/636787311873257476>
Content-Transfer-Encoding: binary
Content-Type: application/octet-stream // This must be application/pdf
%PDF-1.4
%
1 0 obj
<</Type /Catalog/Pages 2 0 R>> ...
I'm having to guess that you're encoding a response to a request. If so, you just need to add the content type header to your outgoing message:
OperationContext
.Current
.OutgoingMessageHeaders
.Add ("Content-Type", "application/pdf" );
Not 100% sure, but you may need to clear any existing outgoing Content-Type header, first. I don't think it matters that your message is MTOM-encoded. That is theoretically transparent.
Also...just asking...why allow a 64MB buffered message when a streamed message takes so much less memory to transfer...and is likely to be just as fast or faster? You're pretty much guaranteed better performance in a heavily loaded service.
Update:
If you're lucky enough to be using a web api-type of binding (WebHttpBinding)...you can stream out your content pretty easily, specifying the content type. It's not that different than what I'd put in my answer above, but as you'd replied, those headers go to the wrong place.
Instead, it should be:
WebOperationContext
.Current
.OutgoingResponse
.ContentType = "application/pdf";
There's a fantastic article/example here that fully develops the idea...in a way consistent with your objectives.
If you're bound to an older soap binding, then you probably need is to write a custom message encoder. The technique is straightforward enough (if tedious). The basic idea is that you can take control of writing the body of the message in ways that are appropriate to your needs.
The encoder itself is trivial. The boilerplate that goes around it will be the tedious part. A custom encoder derives from the abstract MessageEncoder. The important things to do are to override the ContentType
, MediaType
, MessageVersion
properties and the ReadMessage
and WriteMessage
methods. There are variations of the ReadMessage
and WriteMessage
methods for dealing with streamed and buffered messages.
The ugly part is the boilerplate that you have to write to deploy your encoder. You have make an encoder factory, and a custom binding element. Not 'orrible, but noisy. There's a couple of examples of the whole ball of wax (including the factory and binding element support) in the in the example code package here...which can save you some time.