Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加服务引用失败,尽管服务正在运行_C#_Wcf - Fatal编程技术网

C# 添加服务引用失败,尽管服务正在运行

C# 添加服务引用失败,尽管服务正在运行,c#,wcf,C#,Wcf,我尝试实现WCF下载上传服务,我只想从我的客户端文件发送到我的服务。 因此,我提出了这个问题 这是我的服务: [ServiceContract] public interface IFileTransferWindowsService { [OperationContract] void UploadDocument(string fPath, byte[] data); [OperationContract] byte[] DownloadDocument(s

我尝试实现
WCF下载上传服务
,我只想从我的客户端文件发送到我的服务。 因此,我提出了这个问题

这是我的服务:

[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>
我还创建了
Windows服务项目
,并尝试安装我的服务(通过installutil)并运行此服务。 之后,我尝试通过添加服务引用连接此服务,但连接失败:

无法识别URI前缀。 元数据包含无法解析的引用:'net。tcp://10.61.41.51:8532/FileTransferWindowsService/'. 无法连接到网络。tcp://10.61.41.51:8532/FileTransferWindowsService/. 连接尝试持续了00:00:01.0011001的时间跨度。TCP错误代码10061:无法建立连接,因为目标计算机主动拒绝它10.61.41.51:8532。 无法建立连接,因为目标计算机主动拒绝它10.61.41.51:8532
如果服务是在当前解决方案中定义的,请尝试构建解决方案并再次添加服务引用。

我认为问题在于您没有设置连接的安全级别。默认情况下,NetTcpBinding尝试在传输层保护连接,但您没有在客户端中指定

最简单的解决方案是在服务器上将安全级别设置为“无”。添加这一行

ntcp.Security.Mode = SecurityMode.None;

在StartWCFService()方法中

防火墙允许这个端口吗?8532是,这不是防火墙问题检查服务是否正在运行。可能发生了一些异常,服务未运行。该服务正在运行。请尝试使用telnet进行连接,以检查连接是否可以稳定。我将其放在OnStart()方法中,不幸的是,这没有帮助。鉴于您的NetCpBinding对象不在该方法的作用域中,为什么要将其放在OnStart()方法中?
ntcp.Security.Mode = SecurityMode.None;