虽然服务正在运行但添加服务引用失败

时间:2013-10-30 13:40:01

标签: c# wcf

我尝试实现WCF download upload service,我想要做的就是从我的客户端文件发送到我的服务。 所以我设想这个guide

这是我的服务:

[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 如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。

1 个答案:

答案 0 :(得分:0)

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

最简单的解决方案是在服务器上将安全级别设置为NONE。添加此行

ntcp.Security.Mode = SecurityMode.None;
在您的StartWCFService()方法中