2010-05-19 67 views
0

问题: (在最终解决方案) 我有一个Silverlight应用程序:在Web项目Web服务不会与应用互动

网络

的Silverlight

该网站包含服务:

[WebService(Namespace = "svChat")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
//[System.Web.Script.Services.ScriptService] 
public class GetIPService : System.Web.Services.WebService 
{ 

    public GetIPService() 
    { 

     //Uncomment the following line if using designed components 
     //InitializeComponent(); 
    } 

    [WebMethod] 
    public string GetIp() 
    { 
     return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 

    } 
    } 

我得到了使用本服务在我的Silverlight应用程序的类:创建客户端

public class Client 
{ 
    private string ip; 
    private string created; 

    #region Properties 
    public string Ip 
    { 
     get { return ip; } 
     set { ip = value; } 
    } 

    public string Created 
    { 
     get { return created; } 
     set { created = value; } 
    } 
    #endregion 

    public Client() 
    { 
    } 

    public void SetIp() 
    { 
     ServiceReference1.GetIPServiceSoapClient scIpClient = new svChat.ServiceReference1.GetIPServiceSoapClient(); 
     scIpClient.GetIpCompleted += new EventHandler<svChat.ServiceReference1.GetIpCompletedEventArgs>(IpService_Completed); 
     scIpClient.GetIpAsync(); 
    } 

    private void IpService_Completed(object sender, ServiceReference1.GetIpCompletedEventArgs e) 
    { 
     this.ip = e.Result; 
    } 

} 

后,SETIP()被调用,Client.Ip被添加到一个文本框。 什么都没有发生。 Ip = null。

服务本身的作品,测试它。 通过上面的代码获得Ip的作品。

通过Silverlight应用程序获取Ip服务不起作用。

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="GetIPServiceSoap" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> 
        <security mode="None" /> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:2090/svChat.Web/GetIPService.asmx" 
       binding="basicHttpBinding" bindingConfiguration="GetIPServiceSoap" 
       contract="ServiceReference1.GetIPServiceSoap" name="GetIPServiceSoap" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

任何想法?

问候,

解决方案: 内创建VS 2010(旗舰版)Silverlight应用程序会导致VS使用相同的测试服务器的Silverlight应用程序和网站。 这是没有问题的,直到VS使用Silverlight配置来设置测试服务器。 Silverlight客户端现在无法正确访问Web服务器Web服务。 确切原因未知,但我认为这是由上述情况引起的。 因此,开始调试,并等待网站加载并弹出“Exception”,然后“停止”调试,并继续测试网站,而不必担心异常。缺点:没有调试。

+1

为什么不直接在客户端和服务器上使用WCF? – 2010-05-22 18:55:33

回答

1

我猜测关键是确认对Web服务的请求确实包含通常由代理服务器或负载平衡器添加的Http标头HTTP_X_FORWARDED_FOR

如果这个头不存在,则调用的结果

HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 

为空,这是你所看到的。由于您在配置点中显示的端点显示为localhost,因此您肯定不会通过代理或负载平衡器,因此不会添加HTTP_X_FORWARDED_FOR标头。

的http:// 本地主机:2090/svChat.Web/GetIPService.asmx

如果不通过代理或负载平衡器,你可以看看使用REMOTE_ADDR会(不同的成功度)

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] 

无论如何,你应该编写代码来处理这些事实上都没有设置任何东西。除非您控制客户端和服务器之间的所有infrastrucutre组件,否则不能假定每个代理或lad均衡器都将添加HTTP_X_FORWARDED_FOR标头。

更新:根据您提供的代码,以下是我所做的更改。

public MainPage() 
    { 
     InitializeComponent(); 
     this.Loaded += new RoutedEventHandler(Page_Loaded); 
     this.MainClient = new Client(); 
     ClientList.Clients.Add(this.MainClient); 

     // Removed LoadXMLFile call here, constructor runs before Loaded event. 
     //LoadXMLFile(); 
    } 

    private void Page_Loaded(object sender, RoutedEventArgs e) 
    { 
     svChat.ServiceReference1.GetIPServiceSoapClient scIpClient = new svChat.ServiceReference1.GetIPServiceSoapClient(); 
     scIpClient.GetIpCompleted += new EventHandler<svChat.ServiceReference1.GetIpCompletedEventArgs>(IpService_Completed); 

     scIpClient.GetIpAsync(); 
    } 

    public void IpService_Completed(object sender, svChat.ServiceReference1.GetIpCompletedEventArgs e) 
    { 
     this.MainClient.Ip = e.Result; 
     // Probably where you should call LoadXMLFile 
     // at this point the async call has returned and 
     // the ip is intitialized. 
     LoadXMLFile(); 
    } 
+0

GetIp()方法不返回任何东西,我用“hello world”替换了返回值。 直接从网页调用服务,但不通过Silverlight ... – 2010-05-20 09:10:28

+1

@ daemonfire300,我做了一个快速测试应用程序,它工作正常。您可以考虑使用像Fiddler这样的工具来查看HTTP流量。 http://www.fiddler2.com/fiddler2/ – 2010-05-20 17:33:52

+0

有趣的是,即使是一个新的空项目也不会返回任何结果,但不是空的....奇怪。 – 2010-05-21 14:09:40