Host a WCF Service in IIS 7 & Windows 2008 - The right way (IIS7上部署WCF)
2009-06-10 16:26
501 查看
原文:http://blah.winsmarts.com/2008-4-Host_a_WCF_Service_in_IIS_7_-and-amp;_Windows_2008_-_The_right_way.aspx
Okay boys'n'girls, and everyone else - listen up. I am going to cover some information here in this blog post that is nowhere else on the web. That is not surprising, consider that less than 2 people in the world are actually using WCF + .NET 3.5 + IIS 7.
Anyway, deploying a WCF service using .NET 3.5 framework to IIS 7 requires you to jump through a couple of hoops ..
Knock Knock
who'se there
Alex
Alex who?
Alex'plain why IIS 7 can be a real pain in the donkey.
Now, we already wrote up a WCF Service over here. Actually, what you wrote, was a WCF Service Library - the guts of the service itself are actually a DLL. And that is great, because now the host can be anything, independent of the service implementation itself. So the same service, can be hosted in IIS, WAS, or your custom host. NEAT.
Well, in this blogpost, I am going to cover hosting the service in IIS 7. I saw this MSDN article talking about hosting the WCF service in IIS. I thought that article was very sub-optimal. Here are my reasons:
a) It won't work for IIS 7 (Doh!). IMO it's quite bad that an MSDN article isn't upto the times - and they expect us devs to be up on all this cool new shiny stuff :-).
b) It talks of creating an App_Code directory, and throwing a .cs file in there - this is okay for your development environments, but not for production code deployment. Your deployment process must not require you to work in .cs.
c) That article literally talks about writing a new WCF service in IIS - Dangit!
I'm going to instead talk about - you've developed a service, now how the heck we deploy it. Well, here are the steps.
Build your Service Library in release mode.
Go to your Win2k8 server, and add the "Web Server" role - make sure ASP.NET is enabled.
Create a dir on your disk - I put mine in c:\code\HelloWorld <-- this will be the physical directory that will run my WCF service.
Fix the security on this dir by adding the following permissions
Ensure that "Network Service" has Read & Execute + List folder contents + Read.
Ensure that IIS_IUSRS have Read & Execute + List folder contents + Read.
Now, go to IIS MGR, and add a new application pool called "Hello World". Ensure that this Application Pool is set to the settings as shown below -
Next, create a website in IIS7 with the following settings -
Are we done yet? Heck no! Apparently, WCF .. doesn't work with IIS 7 OOTB. So you need to run command prompt as administrator, and run the following command -
"%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r -y
Now, the DLL that you created in step #1, copy that to C:\Code\HelloWorld\Bin .. where C:\Code\HelloWorld is the physical path of my website.
Next, create a file called HelloWorld.svc at C:\Code\HelloWorld with the following text in it -
<% @ServiceHost Service="MyServices.HelloWorld" %>
Note that it looks very much like ASP.NET code behind .. shocking huh?
Next, create a web.config with the following -
As you can see, I have set a base address of http://localhost:8000 <-- change that to what suits you. Also, I have created 2 endpoints, one uses wsHttpBinding, and the other uses mexHttpBinding.
Now browse to this URL - http://localhost:8000/HelloWorld.svc, you should see the service hosted as shown below -
Thats it! You can either use svcutil now, or you can hand-create a channel/proxy to use the above service as shown in this earlier blogpost of writing a WCF client.
___________________________________
There are other ways of hosting a WCF service too. You could host them using WAS - Windows Activation Service. Hosting them in WAS or IIS means, you don't have to worry about the host lifetime. IIS and Win2k8/Vista take care of the host lifetime for you. However, for the rather interesting situation of two WF's talking to each other, you may need to have an EXE that is both a WF host, and a WCF host, as I described here. In such instances, you may need to write your very own host. As it turns out .. it's rather easy to write your own WCF host.
看视频
Okay boys'n'girls, and everyone else - listen up. I am going to cover some information here in this blog post that is nowhere else on the web. That is not surprising, consider that less than 2 people in the world are actually using WCF + .NET 3.5 + IIS 7.
Anyway, deploying a WCF service using .NET 3.5 framework to IIS 7 requires you to jump through a couple of hoops ..
Knock Knock
who'se there
Alex
Alex who?
Alex'plain why IIS 7 can be a real pain in the donkey.
Now, we already wrote up a WCF Service over here. Actually, what you wrote, was a WCF Service Library - the guts of the service itself are actually a DLL. And that is great, because now the host can be anything, independent of the service implementation itself. So the same service, can be hosted in IIS, WAS, or your custom host. NEAT.
Well, in this blogpost, I am going to cover hosting the service in IIS 7. I saw this MSDN article talking about hosting the WCF service in IIS. I thought that article was very sub-optimal. Here are my reasons:
a) It won't work for IIS 7 (Doh!). IMO it's quite bad that an MSDN article isn't upto the times - and they expect us devs to be up on all this cool new shiny stuff :-).
b) It talks of creating an App_Code directory, and throwing a .cs file in there - this is okay for your development environments, but not for production code deployment. Your deployment process must not require you to work in .cs.
c) That article literally talks about writing a new WCF service in IIS - Dangit!
I'm going to instead talk about - you've developed a service, now how the heck we deploy it. Well, here are the steps.
Build your Service Library in release mode.
Go to your Win2k8 server, and add the "Web Server" role - make sure ASP.NET is enabled.
Create a dir on your disk - I put mine in c:\code\HelloWorld <-- this will be the physical directory that will run my WCF service.
Fix the security on this dir by adding the following permissions
Ensure that "Network Service" has Read & Execute + List folder contents + Read.
Ensure that IIS_IUSRS have Read & Execute + List folder contents + Read.
Now, go to IIS MGR, and add a new application pool called "Hello World". Ensure that this Application Pool is set to the settings as shown below -
Next, create a website in IIS7 with the following settings -
Are we done yet? Heck no! Apparently, WCF .. doesn't work with IIS 7 OOTB. So you need to run command prompt as administrator, and run the following command -
"%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r -y
Now, the DLL that you created in step #1, copy that to C:\Code\HelloWorld\Bin .. where C:\Code\HelloWorld is the physical path of my website.
Next, create a file called HelloWorld.svc at C:\Code\HelloWorld with the following text in it -
<% @ServiceHost Service="MyServices.HelloWorld" %>
Note that it looks very much like ASP.NET code behind .. shocking huh?
Next, create a web.config with the following -
1: <?xml version="1.0" encoding="utf-8"?>
2: <configuration>
3: <system.serviceModel>
4: <services>
5: <service behaviorConfiguration="MyServices.HelloWorldBehavior" name="MyServices.HelloWorld">
6: <endpoint address="" binding="wsHttpBinding" contract="MyServices.IHelloWorld" />
7: <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
8: <host>
9: <baseAddresses>
10: <add baseAddress="http://localhost:8000/" />
11: </baseAddresses>
12: </host>
13: </service>
14: </services>
15: <behaviors>
16: <serviceBehaviors>
17: <behavior name="MyServices.HelloWorldBehavior">
18: <serviceMetadata httpGetEnabled="true" />
19: <serviceDebug includeExceptionDetailInFaults="false" />
20: </behavior>
21: </serviceBehaviors>
22: </behaviors>
23: </system.serviceModel>
24: </configuration>
As you can see, I have set a base address of http://localhost:8000 <-- change that to what suits you. Also, I have created 2 endpoints, one uses wsHttpBinding, and the other uses mexHttpBinding.
Now browse to this URL - http://localhost:8000/HelloWorld.svc, you should see the service hosted as shown below -
Thats it! You can either use svcutil now, or you can hand-create a channel/proxy to use the above service as shown in this earlier blogpost of writing a WCF client.
___________________________________
There are other ways of hosting a WCF service too. You could host them using WAS - Windows Activation Service. Hosting them in WAS or IIS means, you don't have to worry about the host lifetime. IIS and Win2k8/Vista take care of the host lifetime for you. However, for the rather interesting situation of two WF's talking to each other, you may need to have an EXE that is both a WF host, and a WCF host, as I described here. In such instances, you may need to write your very own host. As it turns out .. it's rather easy to write your own WCF host.
看视频
相关文章推荐
- Host a WCF Service in IIS 7 & Windows 2008 - The right way
- Calling a WCF Service using jQuery in SharePoint - the correct way
- WCF : 修复 Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service 问题
- Emulating Raspberry Pi in Windows the easy way
- Managing DbContext the right way with Entity Framework 6: an in-depth guide
- 部署第一个windows service 2008 只读域控制器
- WCF Failed to invoke the service. Possible causes: The service is offline or inaccessible
- Windows 2008 R2上配置IIS7或IIS7.5中的URLRewrite(URL重写)实例
- [WCF] 部署到iis, 无法找到 ServiceHost 指令中作为服务属性值提供的类型
- Windows 2008之IIS 7下PHP部署攻略
- Hosting WCF Service in IIS
- Windows 2008 IIS 7 WCF 404.17 ERROR
- Changes in behavior of the SysPrep and RIPREP tools after you install Windows XP Service Pack 2
- MySQL Point in Time Recovery the Right Way
- [已解决][WCF]provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.
- Windows 2008下在IIS中寄宿WCF MSMQ的方法
- Hosting WCF Service with netTcpBinding in Windows Service
- windows 2008环境下利用IIS7.5部署web应用程序
- 如何让带证书的WCF服务 部署在以network service 帐户运行的Windows Service中 原创
- Windows 2008 IIS 7 WCF 404.17 ERROR