【问题标题】:WebServices does not interact with AppWebServices 不与 App 交互
【发布时间】:2010-05-19 18:55:54
【问题描述】:

问题: (最后解决) 我在 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;
    }

}

创建Client后,调用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 (Ultimate) 中创建 Silverlight 应用程序会导致 VS 为 silverlight 应用程序和网站使用相同的测试服务器。 在 VS 使用 Silverlight 配置设置测试服务器之前,这没有问题。 silverlight 客户端现在将无法正确访问 webservers web 服务。 确切的原因尚不清楚,但我认为这是由上述情况引起的。 所以开始调试,等到网站正在加载,弹出“异常”,然后“停止”调试,继续测试网站,不用担心异常。 缺点:无法调试。

【问题讨论】:

  • 为什么不在客户端和服务器上都使用 WCF?

标签: c# .net silverlight web-services asmx


【解决方案1】:

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

如果这个头不存在那么调用的结果就是

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

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

http://localhost:2090/svChat.Web/GetIPService.asmx

如果您没有通过代理或负载平衡器,您可以使用 REMOTE_ADDR(成功程度不同)

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

无论如何,您可能应该编写代码来处理这些实际上都没有设置任何内容的事实。您不能假设每个代理或小伙子平衡器都会添加 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();
    }

【讨论】:

  • @daemonfire300,我做了一个快速测试应用程序,它运行良好。您可以考虑使用 Fiddler 之类的工具来查看 HTTP 流量。 fiddler2.com/fiddler2
  • @daemonfire300,是的,我使用了 ASMX Web 服务。你可以在这里下载测试代码cid-5bb13275dc6b8248.skydrive.live.com/self.aspx/Public/…,注意你需要更新参考,因为我使用了内部的VS web服务器,所以端口号会在你的机器上改变。
  • @daemonfire300,如果你把代码放在某个地方,我很乐意看看。
  • @daemonfire300,好的,我看了一下。我发现我认为是问题所在。在 MainPage 的构造函数中调用 LoadXMLFile(),在其中尝试访问 this.MainClient.Ip.ToString(),但 this.MainClient.Ip 仍然为空,因为调用 scIpClient.GetIpAsync 的 Page_Loaded 尚未运行。似乎您在项目中引用了一个不同的服务,GetIpAsync 期待一些我没有的 Request 对象,因此我删除了 Service 引用并重新添加它,一切正常。跨度>
  • @daemonfire300,好的,上传到我的skydrive cid-5bb13275dc6b8248.skydrive.live.com/self.aspx/Public/… 很高兴,希望你能成功。
猜你喜欢
  • 2011-11-01
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-05
  • 2023-03-14
  • 2013-01-03
  • 1970-01-01
相关资源
最近更新 更多