views:

62

answers:

1

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.

+1  A: 

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();
    }
Chris Taylor
The GetIp() method does not return anything, I replaced the return with "hello world".Calling the Service directly from the webpage works, but not via Silverlight...
daemonfire300
@daemonfire300, I did a quick test application at it worked fine. You might consider using a tool like Fiddler to view the HTTP traffic. http://www.fiddler2.com/fiddler2/
Chris Taylor
funny even a new and empty project returns nothing, but not null.... strange.
daemonfire300
We talk about Web not WCF Web Service right?
daemonfire300
@daemonfire300, yes I used an ASMX web service. You can download the test code here http://cid-5bb13275dc6b8248.skydrive.live.com/self.aspx/Public/dotNET%20Samples/SilverlightApplication1.zip , Note you will need to update the reference because I used the internal VS web server so the port number will change on your machine.
Chris Taylor
I use internal test servers too, maybe that's the problem?Maybe some configuration, I can not figure out what s the problem^^
daemonfire300
@daemonfire300, if you put the code up somewhere I will gladly take a look at it.
Chris Taylor
i ll put it one skydrive: http://cid-b3779da32f745f81.skydrive.live.com/self.aspx/.Public/svChat.rar
daemonfire300
@daemonfire300, OK I took a quick look. I found what I think is the problem. In the Constructor of MainPage you call LoadXMLFile(), in which you try to access this.MainClient.Ip.ToString(), but this.MainClient.Ip is still null because Page_Loaded has not yet run which calls scIpClient.GetIpAsync. It also seemed that you have a different service referenced to the one that it is in the project, the GetIpAsync was expecting some Request object that I did not have so I removed the Service reference and re-added it and everything worked fine.
Chris Taylor
could you reupload, and insert a correct/working version?Would be very very nice.Thank you very very much for your patience.
daemonfire300
@daemonfire300, OK, uploaded to my skydrive http://cid-5bb13275dc6b8248.skydrive.live.com/self.aspx/Public/dotNET%20Samples/svChat.zipIt is a pleasure, hope you can get this to work.
Chris Taylor
I got it, it was a VS Problem. The debug Server always ran at "debug" kills the actual server the service is running on (maybe different port), and just reports null. If I run debug and kill debug without stopping the webserver, the service works properly.I think it is very strange.
daemonfire300