• Calling services from Silverlight 2 part 2


    In a previous post, I wrote about some samples of calling various types of services from Silverlight 2.  In the code, I was using constructors in my ASMX and WCF services with specifying a binding type and endpoint address.

    It was called out to me that in other demonstrations, people did not use this construct.  While the method I demonstrated works (explicitly specifying the binding and endpoint), in some cases it may not be necessary.  One such case would be if you only have one endpoint and it is basicHttpBinding.

    The error in my code/instructions was about changing the binding information in web.config.  The information is correct, however I wasn't clear on when/what you needed to do.  For example, the default information in web.config for the Silverlight project created for you uses wsHttpBinding.  If you add a service reference in your Silverlight project PRIOR to changing that binding information, your generated proxy will require you to specify a binding and endpoint as Silverlight doesn't support wsHttpBinding and it would be trying to use that as a default method.

    So the appropriate way is to change the binding type in your web.config FIRST.  Then generate the service reference in your Silverlight application and your proxy code generated will then allow you to new up the service using:

    WcfServiceClient wcf = new WcfServiceClient();

    for both WCF and ASMX services...which is probably more familiar to most web developers implementing services in their applications.

    Again, EITHER way is fine.  Providing no information in the constructor will use the default binding/endpoint information for that service, and if it isn't supported, you'll get a nasty exception.  Whether or not it is best practice to always explicitly call it out in your code is up to you.  I'd argue it is.  In looking at the code above do you know what binding/endpoint is being used at the time of the service call?  No.  You could make some reasonable assumptions (hey, I'm in Silverlight and I know I must use basicHttpBinding), but for maintainability, maybe someone else coming to the code doesn't have the same understanding.

    To each his own.  Either way, I hope this clarifies and I've updated my post with the note about this as well.

    Wednesday, March 19, 2008 8:25 AM

    PostTypeIcon

Comments.

  • nick said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    Hi Tim,

    Everything works fine for me except when I publish website, the webserive seems to have a reference to the machine name instead of IP address. In my case it is called it001 and so, the xap archive downloaded on the client has absolutely no clue what it001 means. I manually changed the reference by going through every file but I am looking for a quicker way to do this. VS2008 update service option will not do the job if I specify the webservice address with the public ip. Any advice?

    3/19/2008 10:25 AM
  • timheuer said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    nick: yes, if you add a reference to a web service, it uses *that* WSDL. So VS2008, Silverlight or any other tool doesn't automatically change anything when you upload the project(s). The reference to the service you added will still be there. You can go into the generated proxy and change the base uri for the service or also configure the endpoints in your web.config.

    3/19/2008 12:16 PM
  •  said:
    Gravatar
    # <br /> <br /> Calling services from Silverlight 2 part 2<br /> <br />


    <br /> <br /> Calling services from Silverlight 2 part 2<br /> <br />

    3/20/2008 1:53 AM
  • Jonas said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    Hi Tim,
    I am currently have three projects, 1)WcfApp;2)SilverlightApp;3)AspNetWebApp;

    SilverlightApp has service reference to WcfApp, and has the settings/Binding for service in clientConfig file.
    AspNetWebApp is the host app for SilverlightApp,
    but no serviceReference or webReference to WcfApp that means no client settings in web.config of AspNetWebApp.

    It works fine in my local.

    But if I deploy them to Production server,
    How can I change the settings like endpointaddress...?

    I am not really clear about which config file will be read when the silverlight project(.zap) hosted in aspx page.

    Thanks

    3/24/2008 7:08 PM
  • Nick said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    I use this code to generate my proxy - as you can see it automatically builds the binding based on the url of the webserver that is hosting my silverlight control. That way when I run in debug it uses the debug service, and when it is deployed it uses the deployed service.

    public static MantisServiceClient GetServiceProxy()
    {
    BasicHttpBinding binding = new BasicHttpBinding();
    binding.MaxReceivedMessageSize = 2147483647; // int's max size
    binding.MaxBufferSize = 2147483647; // int's max size

    BaseUri = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri;
    int lastSlash = BaseUri.LastIndexOf("/");

    BaseUri = BaseUri.Substring(0, lastSlash + 1);


    EndpointAddress addr = new EndpointAddress(BaseUri + "MantisService.svc");
    return new MantisServiceClient(binding, addr);
    }

    3/28/2008 5:04 AM
  • rk10000 said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    Tim,
    Whats the workaround for debugging webservices? My app seems to respect breaking in asp.net apps, and its associated silverlight project but not on the webservices called asynchronously from SL. Any tips?

    4/6/2008 7:12 AM
  • timheuer said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    rk10000: it works fine for me, are you sure both of your projects generated debug symbols?

    4/6/2008 10:11 AM
  • rk10000 said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    It would seem so; The SL pdb files are visible in the clientbin folder itself. As for the asp.net project there are a couple of pdb files created C:\Users\[Username]\AppData\Local\Temp\Temporary ASP.NET Files\website1 (includes Add_Code pdb file there).

    4/6/2008 3:46 PM
  • rk10000 said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    Got it working. Had to manually delete the contents of the asp.net temp folder (for some reason clean build just did not cut it); and behold all worked as expected :)
    Thanks for your pointer Tim!

    4/8/2008 8:50 PM
  • suyog said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    hi Tim,
    when i return data more than 65kb from service,
    i am getting MaxReceivedMessageSize exceptions.
    i tried to set MaxReceivedMessageSize values from code as well as config file, still it showing exceptions
    i tried code as u told :

    BasicHttpBinding binding = new BasicHttpBinding();
    binding.MaxReceivedMessageSize = 2147483647; // int's max size
    binding.MaxBufferSize = 2147483647; // int's max size

    5/22/2008 1:22 AM
  • hemsheel said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    We are trying to call a web service from Silverlight . We get the following error.

    The remote server returned an unexpected response:(404) Not found.
    at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
    at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
    at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
    at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
    at TestSilverLightWebSrv.TestServiceSL.ServiceSoapClient.ServiceSoapClientChannel.EndDispalyName(IAsyncResult result)
    at TestSilverLightWebSrv.TestServiceSL.ServiceSoapClient.TestSilverLightWebSrv.TestServiceSL.ServiceSoap.EndDispalyName(IAsyncResult result)
    at TestSilverLightWebSrv.TestServiceSL.ServiceSoapClient.EndDispalyName(IAsyncResult result)
    at TestSilverLightWebSrv.TestServiceSL.ServiceSoapClient.OnEndDispalyName(IAsyncResult result)
    at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

    Environment
    ----------------
    .Net Framework 3.5
    VS2008
    Silverlight 2 beta1
    II6.0
    Windows Server 2003

    Service and consuming environment run on the same machine

    Can you verify and let us the reason and the possible solution for the same.

    6/12/2008 11:07 PM
  • Ramesh said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    hi
    all,


    be reply as early as possible ........

    the same error found me also
    The remote server returned an unexpected response:(404) Not found.

    having same enviroment....

    Ramesh Nikam

    6/12/2008 11:28 PM
  • Ramesh said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    finaly i got the solution i don't know how its give me correct out put may i use policy files for that.


    Ramesh Nikam

    6/13/2008 2:09 AM
  • timheuer said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    @hemsheel/@ramesh: see http://timheuer.com/blog/archive/2008/06/10/silverlight-services-cross-domain-404-not-found.aspx for some troubleshooting steps.

    6/13/2008 6:47 AM
  • Turkey said:
    Gravatar
    # re: Calling services from Silverlight 2 part 2


    Thanx You.. Perfect Docs

    7/30/2008 10:47 PM

Your Reply.

  Comment Form  

Fields denoted with a "*" are required.

*Your name:
Subject:
Your blog:
Your email:  (will not be displayed)
*Your message:

 
Please add 6 and 7 and type the answer here: