WCF Services Sample:Config over both HTTP and HTTPS

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

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.

iis-app-setup-1.jpg

iis-app-setup-2.jpg

Next, let's test the WCF service via the new URL: http://localhost/ServiceHost/Service1.svc

iis-app-setup-3.jpg

4. Modify the Silverlight Client Code

First, we need to add a WCF service reference to the new service address we hosted above.

wcf-add-service-reference-2.jpg

wcf-service-references.jpg

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.

wcf-service-cross-domain-error.jpg

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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值