Geeks With Blogs
My lessons learned while developing!

Getting this silly thing to work turned out to be a painful experience, so I am recording my lessons learned so that if some other downtrodden programmer wants to see what I did, they will be able to figure it out faster (hopefully):

Ok, so say you have a silverlight application and you want to talk to a database. Silverlight is Client-based, and Databases are Server based. This is a problem. You can either pass the values in initially (not very good for interactive stuff) or you can make a WCF service.

I needed interactivity, so the best way to make it work is with a WCF service. There are many ways to implement one of these, but the way I picked and got working was having the service be part of my .Web project. (I thought it was the easiest looking one)

First, I went to my .Web project in my solution and told it I wanted to add a new item, and picked the "Silverlight-enabled WCF Service" from the "Silverlight" category of the installed templates. Doing so adds a .svc file and an associated .cs file to your project and has an example method "DoWork()" that you can use to create other methods that can be called from silverlight.

Basically you can do whatever you would normally do from a .cs file, as long as you have appropriate references and assemblies and put "[OperationContract]" above it, like the example method that it comes with.  Go ahead and do your thing like you would normally do.

Then, you need to go over to your service references in your silverlight project and tell it you want to add a service reference. Since you are still in developing mode at this point, just click on "Discover" and pick your service, and name it whatever you want, like "myWCFService" or something. That is what I used, so my example code will have it named as such.

Now, in your silverlight project, you will be able to create a myWCFService client and call asynchronous methods on it like so in your .cs files:

myWCFService.Service1Client client = new myWCFService.Service1Client();

client.getMapPointsCompleted +=new EventHandler<myWCFService.getMapPointsCompletedEventArgs>(client_getMapPointsCompleted);
client.getMapPointsAsync();

(and of course define the async callback method--in this case "client_getMapPointsCompleted"-- in your code)

Now, you will be able to develop it and make it work on your localhost without any issue. The problem (and the part that annoyed me for several hours is trying to get the silly thing to work on a production server):

For me, I was making my project for an intranet, and the sysadmins wanted to have it be https, with windows authentication. When you make the service like I described above, it defaults to http with no security whatsoever. Also, the service will point to localhost, which is fine and good for developing, but will need to be changed when we move it to production.

I spent a lot of time looking at different solutions for the problem. The one I came up with was to change my system.serviceModel in web.config to the following:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpEndpointBinding">
          <security mode="Transport">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
        multipleSiteBindingsEnabled="true" />
    <services>
      <service name="[**put the name of your .web project here**].Service1">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding"
            name="BasicHttpEndpoint" contract="[**put the name of your .web project here**].Service1" />
      </service>
    </services>
  </system.serviceModel>

Most of that should be generated for you. The main change for me was changing it from "customBinding" to "basicHttpBinding," setting the security mode to "transport," the credential type to "windows" and deleting the mex binding (not sure what it was supposed to do, but it was making it so that I couldn't change the credential type from anonymous, and it works fine without the mex binding).

After I did this, I published my .Web project to the server, making sure that IIS's settings matched up with it being https with windows authentication, etc. I then went to

https://[addressOfMysilverlightapplication]/Service1.svc

to make sure that my service worked. If it doesn't show you an error when you did that, you got the service working. Good job. You are most of the way there. If it gives you an error, you obviously did something wrong with your web.config or your IIS 7 settings or you typed in the url wrong. Fix the url or Google the error and fix.

Now to make it so that your production silverlight application will talk to this service. Remember the service reference we made earlier? Delete it. Make a new service reference, but this time, instead of clicking on "discover," enter the https address you just used to verify your service worked and name it the same as the one you just deleted.

Then, go to your ServiceReferences.ClientConfig and change it so that it is a relative address (just in case you move it to a different location on the server or a different server later, it won't break). Mine looked like this:

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpEndpoint" maxBufferSize="2147483647"
                    maxReceivedMessageSize="2147483647">
                    <security mode="Transport" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="../Service1.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpoint"
                contract="myWCFService.Service1" name="BasicHttpEndpoint" />
        </client>
    </system.serviceModel>
</configuration>

If you don't have your service on the root of your project, you will have to type in something different for your address.

Rebuild and publish it again, and then take a look at your silverlight application on the server. It should work perfectly now, using https and windows authentication while being able to use a WCF service.

I hope this helped someone. Please feel free to leave a comment.

Posted on Thursday, January 20, 2011 9:39 AM | Back to top


Comments on this post: Getting Silverlight-enabled WCF Service to work with IIS 7 and windows server 2008 with https and windows authentication

# re: Getting Silverlight-enabled WCF Service to work with IIS 7 and windows server 2008 with https and windows authentication
Requesting Gravatar...
I am exactly in the same situation you had.
The question is how did you publish the Web Project to the server?
Left by newdeveloper on Jul 28, 2011 10:04 AM

# re: Getting Silverlight-enabled WCF Service to work with IIS 7 and windows server 2008 with https and windows authentication
Requesting Gravatar...
This just saved me hours of frustration. Well, additional hours of frustration. I didn't need HTTPS or Windows Auth, I just needed my Silverlight WCF service to work. This article is great!!
Left by Bill Sambrone on Dec 12, 2011 3:31 PM

# re: Getting Silverlight-enabled WCF Service to work with IIS 7 and windows server 2008 with https and windows authentication
Requesting Gravatar...
I used wcf service in my sliverlight application ,i got this script error " Unhandled Error in Silverlight Application An exception occurred during the operation, making the result invalid."
i have used yours coding follow, error shown in my production server only
Left by Sanjeev on Nov 24, 2012 4:17 AM

# re: Getting Silverlight-enabled WCF Service to work with IIS 7 and windows server 2008 with https and windows authentication
Requesting Gravatar...
Thanx. This article is great!!
Left by Satya on Aug 30, 2013 5:39 AM

Your comment:
 (will show your gravatar)


Copyright © robertphyatt | Powered by: GeeksWithBlogs.net