0

を実装しようとしWCF download upload serviceていますが、やりたいことは、クライアント ファイルからサービスに送信することだけです。だから私はこのガイドを見つけました

これは私のサービスです:

[ServiceContract]
public interface IFileTransferWindowsService
{
    [OperationContract]
    void UploadDocument(string fPath, byte[] data);

    [OperationContract]
    byte[] DownloadDocument(string fPath);
}

public class FileTransferWindowsService : IFileTransferWindowsService
{
    public void UploadDocument(string fPath, byte[] data)
    {
        string filePath = fPath;
        FileStream fs = new FileStream(filePath, FileMode.Create,
                                       FileAccess.Write);
        fs.Write(data, 0, data.Length);
        fs.Close();
    }

    public byte[] DownloadDocument(string fPath)
    {
        string filePath = fPath;
        // read the file and return the byte[
        using (FileStream fs = new FileStream(filePath, FileMode.Open,
                                   FileAccess.Read, FileShare.Read))
        {
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, (int)fs.Length);
            return buffer;
        }
    }
}

app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <bindings />
    <client />
    <services>
      <service name="WcfFileTransferServiceLibrary.FileTransferWindowsService">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          name="ServiceHttpEndPoint" contract="WcfFileTransferServiceLibrary.IFileTransferWindowsService" />
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          name="ServiceMexEndPoint" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://0.0.0.0:8532/FileTransferWindowsService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="false"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

サービス開始:

protected override void OnStart(string[] args)
{
    StartWCFService();
}

private void StartWCFService()
{
    try
    {
        NetTcpBinding ntcp = new NetTcpBinding();
        ntcp.MaxBufferPoolSize = 2147483647;
        ntcp.MaxReceivedMessageSize = 2147483647;
        ntcp.MaxBufferSize = 2147483647;
        ntcp.ReaderQuotas.MaxStringContentLength = 2147483647;
        ntcp.ReaderQuotas.MaxDepth = 2147483647;
        ntcp.ReaderQuotas.MaxBytesPerRead = 2147483647;
        ntcp.ReaderQuotas.MaxNameTableCharCount = 2147483647;
        ntcp.ReaderQuotas.MaxArrayLength = 2147483647;
        ntcp.SendTimeout = new TimeSpan(1, 10, 0);
        ntcp.ReceiveTimeout = new TimeSpan(1, 10, 0);
        //ntcp.OpenTimeout
        //ntcp.CloseTimeout

        svh = new ServiceHost(typeof(WcfFileTransferServiceLibrary.FileTransferWindowsService));
        ((ServiceBehaviorAttribute)
           svh.Description.Behaviors[0]).MaxItemsInObjectGraph = 2147483647;
        svh.AddServiceEndpoint(
                    typeof(WcfFileTransferServiceLibrary.IFileTransferWindowsService),
                    ntcp,
                    "net.tcp://0.0.0.0:8532/FileTransferWindowsService");
        //svh = new ServiceHost(typeof(WcfFileTransferServiceLibrary.IFileTransferWindowsService));
        svh.Open();
    }
    catch (Exception e)
    {
        Trace.WriteLine(e.Message);
    }

}

また、サービスを作成Windows service projectして(installutil経由で)インストールし、このサービスを実行しようとしました。その後、サービス参照を追加してこのサービスに接続しようとしましたが、接続に失敗しました:

URI プレフィックスが認識されません。メタデータに解決できない参照が含まれています: 'net.tcp://10.61.41.51:8532/FileTransferWindowsService/'。net.tcp://10.61.41.51:8532/FileTransferWindowsService/ に接続できませんでした。接続の試行は、00:00:01.0011001 の期間継続しました。TCP エラー コード 10061: ターゲット マシンがアクティブに拒否したため、接続できませんでした 10.61.41.51:8532。ターゲット マシンがアクティブに拒否したため、接続できませんでした 10.61.41.51:8532 サービスが現在のソリューションで定義されている場合は、ソリューションを構築して、サービス参照を再度追加してみてください。

4

1 に答える 1

0

問題は、接続のセキュリティ レベルを設定していないことだと思います。デフォルトでは、NetTcpBinding はトランスポート層で接続を保護しようとしますが、クライアントでそれを指定していません。

最も簡単な解決策は、サーバーのセキュリティ レベルを NONE に設定することです。この行を追加

ntcp.Security.Mode = SecurityMode.None;

StartWCFService() メソッドで。

于 2013-10-30T14:00:48.100 に答える