添加服务引用给出异常:无法连接到远程服务器 [英] Add service reference gives Exception: Unable to connect to remote server

查看:35
本文介绍了添加服务引用给出异常:无法连接到远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从控制台应用程序客户端调用时,我的 WCF 服务返回结果.但是,它显示

My WCF service returns result when calling from console application client. However, it's showing

异常:无法连接到远程服务器

Exception: Unable to connect to remote server

实际错误:

未能调用该服务.可能原因:服务离线或无法访问;客户端配置不匹配代理人;现有代理无效.请参阅堆栈跟踪更多详情.您可以尝试通过启动新代理、恢复默认配置,或者刷新服务.

Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service.

错误详情:

由于内部错误,服务器无法处理请求.有关错误的更多信息,请打开IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute或从配置行为)在服务器上命令将异常信息发送回客户端,或者开启根据 Microsoft .NET Framework SDK 文档和检查服务器跟踪日志.

The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.

服务器堆栈跟踪:在System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(消息回复,MessageFault 故障,字符串操作,MessageVersion 版本,FaultConverter faultConverter) 在System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime操作,ProxyRpc&rpc) 在System.ServiceModel.Channels.ServiceChannel.Call(字符串动作,Boolean oneway, ProxyOperationRuntime 操作, Object[] ins,Object[] outs, TimeSpan timeout) atSystem.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessagemethodCall、ProxyOperationRuntime 操作)在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage留言)

Server stack trace: at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter) at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

在 [0] 处重新抛出异常:在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessagereqMsg, IMessage retMsg) atSystem.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&msgData, Int32 类型)在 IJiraService.GetProjects() 在JiraServiceClient.GetProjects()

Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at IJiraService.GetProjects() at JiraServiceClient.GetProjects()

请注意,当 Fiddler 运行时,服务工作正常.但是,如果我关闭 Fiddler,则会显示异常:

这是 Fiddler 日志(当我正在获取数据时):

This is Fiddler log(while I'm getting the data):

TTP/1.1 200 连接建立日期:2015 年 8 月 6 日星期四 14:39:22 GMT代理连接:保持活动通过:1.1 localhost.localdomain

TTP/1.1 200 Connection Established Date: Thu, 06 Aug 2015 14:39:22 GMT Proxy-Connection: Keep-Alive Via: 1.1 localhost.localdomain

加密的 HTTPS 流量流经此 CONNECT 隧道.HTTPS 解密在 Fiddler 中启用,因此在此隧道中运行的解密会话将显示在 Web 会话列表中.

Encrypted HTTPS traffic flows through this CONNECT tunnel. HTTPS Decryption is enabled in Fiddler, so decrypted sessions running in this tunnel will be shown in the Web Sessions list.

安全协议:TLS密码:Aes128 128bits哈希算法:Sha1 160bits密钥交换:ECDHE_RSA (0xae06) 256bits

Secure Protocol: Tls Cipher: Aes128 128bits Hash Algorithm: Sha1 160bits Key Exchange: ECDHE_RSA (0xae06) 256bits

== 服务器证书 ==========[主题]CN=*.atlassian.net, O="Atlassian Network Services, Inc.", L=旧金山,S=加利福尼亚,C=美国

== Server Certificate ========== [Subject] CN=*.atlassian.net, O="Atlassian Network Services, Inc.", L=San Francisco, S=California, C=US

[发行人]CN=DigiCert SHA2 高保证服务器 CA,OU=www.digicert.com,O=DigiCert Inc,C=US

[Issuer] CN=DigiCert SHA2 High Assurance Server CA, OU=www.digicert.com, O=DigiCert Inc, C=US

[序列号]08E828A2F8C521A2DC7121A28E191837

[Serial Number] 08E828A2F8C521A2DC7121A28E191837

[以前没有]2014 年 9 月 9 日上午 5:30:00

[Not Before] 9/9/2014 5:30:00 AM

[之后]2017/11/15 下午 5:30:00

[Not After] 11/15/2017 5:30:00 PM

[指纹]EA57BE3C6CDA33E6D875889944EE61284E39D91D

[Thumbprint] EA57BE3C6CDA33E6D875889944EE61284E39D91D

HTTP/1.1 200 正常服务器:nginx日期:2015 年 8 月 6 日星期四 14:39:26 GMT内容类型:application/json;charset=UTF-8传输编码:分块连接:保持连接变化:接受编码X-AREQUESTID:39x4118x1X-ASEN:SEN-2425233设置-Cookie:JSESSIONID=8B68E46928883CA9F99382A67C228541;路径=/;安全的;仅Http设置 Cookie:studio.crowd.tokenkey="";域=.clientname.atlassian.net;到期日=周四,1970 年 1 月 1 日 00:00:10 GMT;路径=/;安全的;仅HttpX-Seraph-LoginReason: OUT设置-Cookie:studio.crowd.tokenkey=YcO0N1IItmmGYah6bzgN0w00;域=.clientname.atlassian.net;路径=/;安全的;仅HttpX-Seraph-LoginReason: OK设置-Cookie:atlassian.xsrf.token=ALMX-0SVV-VVCK-3Y73|c420e5bfab5c997ccdfa21ffa129d60a69af0013|lin;路径=/;安全的X-ASSESSIONID: b2v6itX-AUSERNAME:管理员X-ATENANT-ID:clientname.atlassian.net缓存控制:无缓存、无存储、无转换X-Content-Type-Options: nosniff严格传输安全:max-age=315360000;includeSubDomains

HTTP/1.1 200 OK Server: nginx Date: Thu, 06 Aug 2015 14:39:26 GMT Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding X-AREQUESTID: 39x4118x1 X-ASEN: SEN-2425233 Set-Cookie: JSESSIONID=8B68E46928883CA9F99382A67C228541; Path=/; Secure; HttpOnly Set-Cookie: studio.crowd.tokenkey=""; Domain=.clientname.atlassian.net; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; Secure; HttpOnly X-Seraph-LoginReason: OUT Set-Cookie: studio.crowd.tokenkey=YcO0N1IItmmGYah6bzgN0w00; Domain=.clientname.atlassian.net; Path=/; Secure; HttpOnly X-Seraph-LoginReason: OK Set-Cookie: atlassian.xsrf.token=ALMX-0SVV-VVCK-3Y73|c420e5bfab5c997ccdfa21ffa129d60a69af0013|lin; Path=/; Secure X-ASESSIONID: b2v6it X-AUSERNAME: admin X-ATENANT-ID: clientname.atlassian.net Cache-Control: no-cache, no-store, no-transform X-Content-Type-Options: nosniff Strict-Transport-Security: max-age=315360000;includeSubDomains

如上所述,Fiddler 日志是我必须在 web.config 文件中更改的内容才能使其正常工作?

从 WCF 测试客户端调用时.这是我尝试过的:

when called from WCF test client. Here is what I have tried:

Web.config:

Web.config:

<?xml version="1.0"?>
  <configuration>
  <!--<system.net>
   <defaultProxy useDefaultCredentials="true" >
  </defaultProxy>
 </system.net>-->
<appSettings>
<add key="UserName" value="admin"/>
<add key="Password" value="admin"/>
<add key="resturl" value="https://Clientname.atlassian.net/rest/api/2/"/>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
 <system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5"/>
 </system.web>
 <system.serviceModel>
  <bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IJiraService"/>
    </basicHttpBinding>
    </bindings>
   <client>
     <endpoint address="http://localhost:19065/JiraService.svc"  binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IJiraService" contract="ServiceReference1.IJiraService"
       name="BasicHttpBinding_IJiraService" />
    </client>
    <behaviors>
     <serviceBehaviors>
       <behavior>
         <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
         <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <!-- 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="false"/>
       </behavior>
       </serviceBehaviors>
      </behaviors>
      <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
   </protocolMapping>    
   <serviceHostingEnvironment aspNetCompatibilityEnabled="true"   multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
   <!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
  -->
  <directoryBrowse enabled="true"/>
 </system.webServer>

</configuration>

这是我在代码中的请求方式:

Here is how I'm requesting in my code:

public class Service : IService
{
    private string GetRestURL()
    {
        return System.Configuration.ConfigurationManager.AppSettings["resturl"];
    }

    private string GetUserName()
    {
        return System.Configuration.ConfigurationManager.AppSettings["MyUserName"];
    }

    private string GetPassword()
    {
        return System.Configuration.ConfigurationManager.AppSettings["MyPassword"];
    }

 public string method()
 {
       HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        // HttpClient 


        request.ContentType = "application/json; charset=UTF-8";
        //request.ContentType = "text/xml; charset=UTF-8";
        /*request.Headers.Add("X-Atlassian-Token", "nocheck");
        request.UseDefaultCredentials = true;
        request.KeepAlive = true;*/
        request.Method = method;

        /*if (data != null)
        {
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream(),Encoding.ASCII))
            {
                writer.Write(data);
            }
        }*/
        string base64Credentials = GetEncodedCredentials();
        request.Headers.Add("Authorization", "Basic " + base64Credentials);



     try
     {
        request.Headers.Add("Authorization", "Basic " + base64Credentials);

        response = request.GetResponse() as HttpWebResponse; //here its NULL

        if (response.StatusCode != HttpStatusCode.OK)
        {
                throw new Exception(String.Format("Server error (HTTP {0}:{1}).",response.StatusCode,response.StatusDescription));
        }
    }
    catch (Exception ex) //Here I'm getting exception
    {
        throw new Exception(ex.Message);
    }
 }

这是我的方法签名:

[ServiceContract]
public interface IService
{
    [OperationContract]
    List<ProjectDescription> GetProjects();
 }

注意:方法"是从GetProjects"调用的

Note: the "method" is getting called from "GetProjects"

这是我在通过调用 WCF 测试客户端进行调试时遇到的内部异常:

This is the inner exception I'm getting when debugging by invoking WCF test client:

这是我内心的异常

连接尝试失败,因为连接方在一段时间后没有正确响应,或建立连接失败,因为连接的主机未能响应 165.254.xxx.xxx:443

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 165.254.xxx.xxx:443

注意:当我从控制台应用程序调试时没有调试异常.正如我所说,控制台应用程序运行良好,我可以从服务返回结果.

NOTE: There is no debug exception when I'm debugging from console application. as I said, console application is working fine and I'm able to return the result from the service.

更新:当我在控制台应用程序中将此服务添加为添加引用"时,它工作正常.但是,当我在同一个控制台应用程序中添加为添加服务引用"时,它显示了相同的错误.是我的网络阻止了某些东西吗?请帮忙.

UPDATE: When I'm adding this service in console application as "Add reference", it's working fine. However, when I add as "Add service reference" in the same console application, it's showing me same error. Is it my web is blocking something? Please help.

如何解决这个问题?我还需要在 web.config 中添加其他内容吗?或者我需要在某处允许该 URL 作为受信任的 URL 吗?

How to correct this? Is there something else I need to add in web.config? OR do I need to allow the URL as trusted URL somewhere?

推荐答案

通过在 IIS 中添加代理解决的问题.:)

Issue solved by adding proxy in IIS. :)

这篇关于添加服务引用给出异常:无法连接到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆