In my blog "WCF Services Sample: create a Silverlight client application", I show you how to config WCF service and Silverlight to communicate over HTTP. In this blog I will introduce how to config WCF service over both HTTP and HTTPS. I still use that sample project in this blog.
1. Setup IIS to Enable SSL
Before WCF service can work over HTTPS, we must setup IIS to enable SSL. I have talked about this subject in my another blog, please refer to it for more details.
2. WCF Service Configuration
In order to make WCF service work on HTTPS, we need to change web.config as below.
<!-- Service Endpoints -->
binding="mexHttpsBinding" contract="IMetadataExchange"/>
httpsGetEnabled="true"/>
Pay attention to the bold font config.
3. Create a Web Application in IIS Default Web Site
In order to access the WCF service via HTTPS, we need to host the service in a web site with SSL enabled. Now follow me.
Open the IIS Manager, Create a web application named "ServiceHost" as below.
Next, let's test the WCF service via the new URL: http://localhost/ServiceHost/Service1.svc
4. Modify the Silverlight Client Code
First, we need to add a WCF service reference to the new service address we hosted above.
Notice that there appears a new service reference named "ServiceReference2" under the "Service References".
Next, we change MainPage.xaml.cs code.
public MainPage()
{
InitializeComponent();
EndpointAddress address = new EndpointAddress("https://yuzhangqi01/ServiceHost/Service1.svc/ssl");
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
proxy = new Service1Client(binding, address);
proxy.DummyOperationCompleted += new EventHandler(proxy_DummyOperationCompleted);
}
5. Run the Application from VS2008
Now we are ready to run the application to see what will happen.
When we click "Call Service" button, it throws the following exception.
Because this time we host the service under the default web site, we should place the 2 files "clientaccesspolicy.xml" and "crossdomain.xml" under "%System Root%_Inetpub_wwwroot" directory.
clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8"?>
crossdomain.xml
<?xml version="1.0"?>
headers="SOAPAction,Content-Type"/>
Now, let's run it again. Great, it succeed this time.
[@more@]来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13651903/viewspace-1033982/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/13651903/viewspace-1033982/