IIS服务寄宿

简介:

本文参考:http://log.medcl.net/item/2010/03/wcf-extensions-under-iis7-configuration-issues/

 

     在学习IIS服务寄宿时参考了文章:如何在IIS中承载WCF服务http://msdn.microsoft.com/zh-cn/library/ms733766.aspx,并按照上面的教程进行配置,

 

PS:2011-5-28

不按上面的教程进行配置也是可以的,下面我讲一下我的配置过程。

步骤1:首先创建一个解决方案叫做IISHostWCF。

步骤2:在这个解决方案中创建一个”控制台应用程序“Client来测试我们通过IIS寄宿的服务,然后在创建一个CalcService的“WCF服务库“

步骤3:在创建好WCF服务库以后他会自动帮助我们创建契约IService1.cs(接口)以及契约实现Service1.cs

我们在WCF服务哭下创建一个文件夹叫做App_Code,然后将IService1.cs和Service1.cs都放到这个App_Code文件夹中去。

步骤4:将自动生成的配置文件App.config改名为Web.config(这一步很重要,不然会报错说找不到ServiceHost指定的路径)

步骤5:其他内容可以参照前面。

 

 

最后通过http://localhost/IISHostedCalc/Service.svc访问时报错,错误内容为:“由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。

解决方法:

以管理员运行命令:

[c-sharp]  view plain copy print ?
  1. C:/Windows/Microsoft.NET/Framework/v3.0/Windows Communication Foundation/ServiceModelReg.exe -i  

运行结果如下图所示:

再次登录 http://localhost/IISHostedCalc/Service.svc,运行经过如下图所示

表明服务没有发布成功,按照提示进行修改,修改以后的Web.config为:

[c-sharp]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <behaviors>  
  5.       <serviceBehaviors>  
  6.         <behavior name="MyServiceTypeBehaviors" >  
  7.           <serviceMetadata httpGetEnabled="true" />  
  8.         </behavior>  
  9.       </serviceBehaviors>  
  10.     </behaviors>  
  11.     <services>  
  12.       <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="MyServiceTypeBehaviors" >  
  13.         <!-- This endpoint is exposed at the base address provided by host:  http://localhost/servicemodelsamples/service.svc  -->  
  14.         <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" />  
  15.       </service>  
  16.     </services>  
  17.   </system.serviceModel>  
  18. </configuration>  

再次登录http://localhost/IISHostedCalc/Service.svc,会出现如下界面

 SvcUtil.exe应用

可以参考ServiceModel 元数据实用工具 (Svcutil.exe)

我们可以在C:/Program Files/Microsoft SDKs/Windows/v6.0A/bin目录下找到SvcUtil.exe,将这个程序拷贝到D盘根目录下,然后打开cmd,切换到d盘根目录下,输入命令:

[c-sharp]  view plain copy print ?
  1. svcutil.exe http://xuwei/IISHostedCalc/service.svc?wsdl  

运行完以后,在d盘根目录下就会自动生成CalculatorService.cs和output.config文件。

 

在CalculatorService.cs中定义了CalculatorClient这个类,我们在客户端应用程序CalculatorServiceClient中就可以直接通过Calculator这个类创建调用服务的客户端对象。

CalculatorService.cs:

[c-sharp]  view plain copy print ?
  1. //------------------------------------------------------------------------------  
  2. // <auto-generated>  
  3. //     此代码由工具生成。  
  4. //     运行时版本:2.0.50727.4952  
  5. //  
  6. //     对此文件的更改可能会导致不正确的行为,并且如果  
  7. //     重新生成代码,这些更改将会丢失。  
  8. // </auto-generated>  
  9. //------------------------------------------------------------------------------  
  10.   
  11. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""3.0.0.0")]  
  12. [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ICalculator")]  
  13. public interface ICalculator  
  14. {  
  15.       
  16.     [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Add", ReplyAction="http://tempuri.org/ICalculator/AddResponse")]  
  17.     double Add(double n1, double n2);  
  18.       
  19.     [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Subtract", ReplyAction="http://tempuri.org/ICalculator/SubtractResponse")]  
  20.     double Subtract(double n1, double n2);  
  21.       
  22.     [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Multiply", ReplyAction="http://tempuri.org/ICalculator/MultiplyResponse")]  
  23.     double Multiply(double n1, double n2);  
  24.       
  25.     [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Divide", ReplyAction="http://tempuri.org/ICalculator/DivideResponse")]  
  26.     double Divide(double n1, double n2);  
  27. }  
  28. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""3.0.0.0")]  
  29. public interface ICalculatorChannel : ICalculator, System.ServiceModel.IClientChannel  
  30. {  
  31. }  
  32. [System.Diagnostics.DebuggerStepThroughAttribute()]  
  33. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""3.0.0.0")]  
  34. public partial class CalculatorClient : System.ServiceModel.ClientBase<ICalculator>, ICalculator  
  35. {  
  36.       
  37.     public CalculatorClient()  
  38.     {  
  39.     }  
  40.       
  41.     public CalculatorClient(string endpointConfigurationName) :   
  42.             base(endpointConfigurationName)  
  43.     {  
  44.     }  
  45.       
  46.     public CalculatorClient(string endpointConfigurationName, string remoteAddress) :   
  47.             base(endpointConfigurationName, remoteAddress)  
  48.     {  
  49.     }  
  50.       
  51.     public CalculatorClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :   
  52.             base(endpointConfigurationName, remoteAddress)  
  53.     {  
  54.     }  
  55.       
  56.     public CalculatorClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :   
  57.             base(binding, remoteAddress)  
  58.     {  
  59.     }  
  60.       
  61.     public double Add(double n1, double n2)  
  62.     {  
  63.         return base.Channel.Add(n1, n2);  
  64.     }  
  65.       
  66.     public double Subtract(double n1, double n2)  
  67.     {  
  68.         return base.Channel.Subtract(n1, n2);  
  69.     }  
  70.       
  71.     public double Multiply(double n1, double n2)  
  72.     {  
  73.         return base.Channel.Multiply(n1, n2);  
  74.     }  
  75.       
  76.     public double Divide(double n1, double n2)  
  77.     {  
  78.         return base.Channel.Divide(n1, n2);  
  79.     }  
  80. }  
 

out.config

[c-sharp]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <bindings>  
  5.       <wsHttpBinding>  
  6.         <binding name="WSHttpBinding_ICalculator" closeTimeout="00:01:00"  
  7.             openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"  
  8.             bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"  
  9.             maxBufferPoolSize="524288" maxReceivedMessageSize="65536"  
  10.             messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"  
  11.             allowCookies="false">  
  12.           <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"  
  13.               maxBytesPerRead="4096" maxNameTableCharCount="16384" />  
  14.           <reliableSession ordered="true" inactivityTimeout="00:10:00"  
  15.               enabled="false" />  
  16.           <security mode="Message">  
  17.             <transport clientCredentialType="Windows" proxyCredentialType="None"  
  18.                 realm="">  
  19.               <extendedProtectionPolicy policyEnforcement="Never" />  
  20.             </transport>  
  21.             <message clientCredentialType="Windows" negotiateServiceCredential="true"  
  22.                 algorithmSuite="Default" establishSecurityContext="true" />  
  23.           </security>  
  24.         </binding>  
  25.       </wsHttpBinding>  
  26.     </bindings>  
  27.     <client>  
  28.       <endpoint address="http://xuwei/IISHostedCalc/service.svc" binding="wsHttpBinding"  
  29.           bindingConfiguration="WSHttpBinding_ICalculator" contract="ICalculator"  
  30.           name="WSHttpBinding_ICalculator">  
  31.         <identity>  
  32.           <servicePrincipalName value="host/xuwei" />  
  33.         </identity>  
  34.       </endpoint>  
  35.     </client>  
  36.   </system.serviceModel>  
  37. </configuration>  
 

创建测试客户端

将CalculatorService.cs和out.config拷贝到CalculatorServiceClient项目下。然后添加现有项CalculatorService,添加完以后再添加应用程序配置文件App.config,将out.config中的内容拷贝到App.config中去,Program.cs代码如下:

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace CalculatorServiceClient  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             CalculatorClient client = new CalculatorClient();  
  12.             // 使用 "client" 变量在服务上调用操作。  
  13.             Console.WriteLine("x+y={2} when x={0} and y={1}", 1, 2, client.Add(1, 2));  
  14.             Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, client.Subtract(1, 2));  
  15.             Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, client.Multiply(1, 2));  
  16.             Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, client.Divide(1, 2));  
  17.             Console.Read();  
  18.             // 始终关闭客户端。  
  19.             client.Close();  
  20.         }  
  21.     }  
  22. }  

运行这个CalculatorServiceClient控制台应用程序,运行结果如下:



本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2011/04/25/2297024.html,如需转载请自行联系原作者



目录
相关文章
|
3月前
|
开发框架 监控 安全
Windows Defender 导致 Web IIS 服务异常停止排查
某日凌晨IIS服务异常停止,经查为Windows Defender安全补丁KB2267602触发引擎更新,导致系统资源波动,进而引发应用池回收。确认非人为操作,系统无重启。通过分析日志与监控,定位原因为Defender更新后扫描加重负载。解决方案:将IIS及.NET相关路径添加至Defender排除列表,避免业务影响。
528 116
|
网络协议 文件存储 数据安全/隐私保护
【搭建服务器】Win10 IIS搭建webdav服务以及公网访问教程 - 挂载webdav
【搭建服务器】Win10 IIS搭建webdav服务以及公网访问教程 - 挂载webdav
|
安全 C#
【Azure 应用服务】在安全漏洞扫描中发现有泄露服务器IIS版本的情况,如何实现屏蔽服务版本号信息呢?
【Azure 应用服务】在安全漏洞扫描中发现有泄露服务器IIS版本的情况,如何实现屏蔽服务版本号信息呢?
628 1
|
网络协议 安全 文件存储
Windows如何使用IIS服务搭建本地WebDAV网站并实现远程管理储存文件
Windows如何使用IIS服务搭建本地WebDAV网站并实现远程管理储存文件
548 6
|
开发框架 .NET Apache
windows安装IIS服务
windows安装IIS服务
242 0
C#建立最简单的web服务,无需IIS
C#建立最简单的web服务,无需IIS
|
XML 数据格式 Windows
IIS服务启动提示当文件已存在时,无法创建该文件,183
IIS服务启动提示当文件已存在时,无法创建该文件,183
631 0
|
Windows
windows server 2008 r2 IIS服务403-禁止访问访问被拒绝
windows server 2008 r2 IIS服务403-禁止访问访问被拒绝
355 0
|
C++
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
281 0
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
|
文件存储 SQL .NET
在 NAS SMB 卷上用 Visual Studio 调试 ASP.NET + SQLServer 项目并部署到 IIS 服务
阿里云 NAS SMB 卷具有高可用高吞吐大容量等特点,可以很好地解决网站和数据库项目的存储问题。本文将介绍如何在 NAS SMB 卷上用 Visual Studio 调试 ASP.NET + SQLServer 项目并把项目部署到 IIS 服务作为正式服务开放出去。
2881 0
在 NAS SMB 卷上用 Visual Studio 调试 ASP.NET + SQLServer 项目并部署到 IIS 服务