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;
        }
    }
}

应用程序配置:

<?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 回答