WebServices不与App交互

发布于 2024-09-02 01:12:43 字数 2827 浏览 0 评论 0原文

问题: (解决方案在最后) 我在 Web 项目中获得了 Silverlight 应用程序

网页

银光

Web 包含一个服务:

[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 = 空。

服务本身有效,经过测试。 通过上面的代码获取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 服务。 具体原因尚不清楚,但我认为是由上述情况引起的。 因此,开始调试,等到网站正在加载并弹出“异常”,然后“停止”调试,并继续测试网站,而不必担心异常。 缺点:无法调试。

Problem:
(Solution at the end)
I got a Silverlight App with-in a Web Project

Web

Silverlight

The web contains a service:

[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"];
        
    }
  }

And I got a class in my Silverlight App using the Service:

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;
    }

}

After Client is created, SetIp() is called, and Client.Ip is added to a text box.
Nothing happens.
Ip = null.

Service itselfs works, tested it.
Getting Ip by the above code works.

Gettings Ip via service through Silverlight App does not work.

<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>

Any ideas?

regards,

Solution:
Creating a Silverlight Application within VS 2010 (Ultimate) causes VS to use the same test-server for the silverlight application and the website.
This is no problem until VS uses the Silverlight configuration to set up the test server.
The silverlight client will now not be able to correctly access the webservers webservice.
The exact reason is not known, but I think it's caused by the above described situation.
So start debug, and wait until website is loading and "Exception" pops up, then "Stop" debugging, and continue testing the website withouth worrying about exceptions.
Disadvantage: No debugging.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少跟Wǒ拽 2024-09-09 01:12:43

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

如果此标头不存在,则调用的结果

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

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

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

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

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

无论如何,您可能应该编写代码来处理以下事实:这些都可能实际上没有设置任何内容。您不能假设每个代理或 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();
    }

I guess the key thing is to confirm that the request to the Web Service does include the Http header HTTP_X_FORWARDED_FOR which is normally added by a proxy server or load balancer.

If this header does not exist then the result of the call to

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

is null, which is what you are seeing. Since the end point you are showing in the configuration points to localhost, you are definately not going through a proxy or a load balancer so there would not be a HTTP_X_FORWARDED_FOR header added.

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

If you are not going through a proxy or load balancer you can look use REMOTE_ADDR (with varying degree of success)

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

Regardless, you should probably write your code to handle the fact that neither of these might actually have anything set. You cannot assume every proxy or lad balancer will add the HTTP_X_FORWARDED_FOR header, unless you controll all infrastrucutre components between the client and the server.

Update: Based on the code you provided, here are the changes I made.

    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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文