Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a question to WCF masters.

First of all - I want to implement service, that will allow clients send fax.
So I need upload a file, and data like author, or dispatch date.
All client machines will be in the same local network - so I will use netTcpBinding.

And now, what is my problem?
Actually, I've already done that, it works, but the solution is not great and I've got a couple of problems:
- I can't return value, because of "MessageContract". So, when some connection problems happened, client will never knows.
- second, "MessageContract", some say that this is a bad solution and should be used rarely - don't know why.
- my third problem with that solution is, i basically can't send more than one file (in case I want to use that to send emails, with multiple attachments).

Here is my code on server-side:
Service:
C#
[ServiceContract()]
public interface IFileTransferService
{
    [OperationContract(IsOneWay = true)]
    void Upload(FileTransferRequest request);
}

MIDL
[MessageContract()]
public class FileTransferRequest
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName;
    [MessageBodyMember(Order = 1)]
    public System.IO.Stream Data;
}


And my App.config:
XML
<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding
                 name ="MyTCP"
                 closeTimeout="00:01:00"
                 openTimeout="00:01:00"
                 receiveTimeout="00:10:00"
                 sendTimeout="00:01:00"
                 transactionFlow="false"
                 transferMode="Streamed"
                 transactionProtocol="OleTransactions"
                 hostNameComparisonMode="StrongWildcard"
                 listenBacklog="10"
                 maxBufferPoolSize="524288"
                 maxBufferSize="65536"
                 maxConnections="10"
                 maxReceivedMessageSize="209715200">
          <readerQuotas maxDepth="32"
                        maxStringContentLength="8192"
                        maxArrayLength="16384"
                        maxBytesPerRead="4096"
                        maxNameTableCharCount="16384" />
          <reliableSession ordered="true"
                           inactivityTimeout="00:10:00"
                           enabled="false" />
          <security mode="None">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service name="service.FileTransferService" behaviorConfiguration="ServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8523/FileTransfer"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="MyTCP" contract="service.IFileTransferService"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="False"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>



Thank you for help,
Posted
Comments
Mark Salsbery 22-Jul-11 21:37pm    
Why do you use MessageContract? You already have the stream...maybe come up with a scheme to send multiple files on that stream. You could add messageheader item(s) to the contract with collections of file names and lengths perhaps?
mnd017 23-Jul-11 6:05am    
I could use (if I want to return data): int Upload(System.IO.Stream Data) - but now I don't have a path (I would like to have an extension of the file). When it comes to stream, I have to pass only one parameter - Stream.

Trying "DataContract" instead of "MessageContract" - not a good idea to.
Mark Salsbery 23-Jul-11 12:24pm    
I know. That's why I suggested add a collection of filenames instead of a single file name. Send all the files on your one stream. You'd need to pass the file lengths in a collection as well so you can parse the files from the stream on the receiving end (all this still using MessageContract). I was trying to give a suggestion that you could use wih your existing code shown with minimal changes :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900