WCF 配置服务 演示

简介: 作者:jiankunking 出处:http://blog.csdn.net/jiankunking1、搭建IIS(具体步骤略)2、服务契约如下:namespace JianKunKing.NewVersion.Service{ // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“NewVersionService”。 //[

作者:jiankunking 出处:http://blog.csdn.net/jiankunking


1、搭建IIS(具体步骤略)

2、服务契约如下:
namespace JianKunKing.NewVersion.Service
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“NewVersionService”。
    //[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class NewVersionService : INewVersionService
    {
        NewVersionManager newVersionManager = new NewVersionManager();
        public string GetHello()
        {
            return newVersionManager.GetHello();
        }
    }
}

3、WCF客户端引用服务:


4、客户端调用


重点来了:

客户端配置文件:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_INewVersionService" closeTimeout="00:01:00"
                      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                      maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                      useDefaultWebProxy="true">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
              maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>

      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint
        address="http://192.168.XX.XX/JianKunKingServer/NewVersionService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_INewVersionService"
        contract="NewVersionClient.INewVersionService" name="BasicHttpBinding_INewVersionService" />
    </client>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>


WCF服务客户端配置文件的契约由以下两部分组成:
1、Client的服务引用
2、WCF服务接口(就是平时说的WCF契约)

服务端配置文件Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<system.web>
		<compilation debug="true" targetFramework="4.0" />
	</system.web>
	<system.serviceModel>
		<bindings>
			<basicHttpBinding>
				<binding 
				name="MyServiceBinding"  
				maxBufferSize="2147483647" 
				maxReceivedMessageSize="2147483647" >
					<readerQuotas 
					maxArrayLength="2147483647" 
					maxBytesPerRead="2147483647" 
					maxDepth="2147483647" 
					maxNameTableCharCount="2147483647" 
					maxStringContentLength="2147483647"/>
				</binding>
			</basicHttpBinding>
		</bindings>
		<services>
			<service name="JianKunKing.NewVersion.Service.NewVersionService">
				<endpoint 
				address="" 
				binding="basicHttpBinding" 
				bindingConfiguration="MyServiceBinding" 
				contract="JianKunKing.NewVersion.Service.INewVersionService">
				</endpoint>			
			</service>
		</services>
		<behaviors>
			<serviceBehaviors>
				<behavior>
					<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
					<serviceMetadata httpGetEnabled="true"/>
					<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
					<serviceDebug includeExceptionDetailInFaults="false"/>
				</behavior>
			</serviceBehaviors>
		</behaviors>
		<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
	</system.serviceModel>
	<system.webServer>
		<modules runAllManagedModulesForAllRequests="true"/>
	</system.webServer>
</configuration>

WCF服务实现多个契约的时候 :

public class RightManagementServer : IRightManagementServer,IUserRightService
服务端config配置:

 <service name="JianKunKing.Test.Service.RightManagementServer">
	<endpoint 
			address="" 
			binding="basicHttpBinding" 
			bindingConfiguration="MyServiceBinding" 
			contract="JianKunKing.Test.Service.IRightManagementServer">
	</endpoint>		
	<endpoint 
			address="" 
			binding="basicHttpBinding" 
			bindingConfiguration="MyServiceBinding" 
			contract="JianKunKing.Test.Service.IUserRightService">
	</endpoint>			
</service>

WCF配置演示源码:点击打开链接

WCF 配置服务介绍:点击打开链接

在一个服务器端A调用另一个服务端B的Client,比如:
服务器A是搜索引擎服务器,服务端B是数据库服务器(这两个服务器可以是物理上的一台机器也可以是物理上的两台机器),这两个服务器都有自己WCF的服务端及Client端,在搜索引擎服务器的一部分操作需要操作操作数据库,此时需要调用数据库服务器的Client来操作数据,搜索引擎服务器的具体配置如下:


<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<connectionStrings>
		<add name="JianKunKingSystemDBConnenction" connectionString="Data Source=127.0.0.1;Initial Catalog=JianKunKingDBtest;Persist Security Info=True;User ID=a;Password=a" providerName="System.Data.SqlClient" />
	</connectionStrings>
	<system.web>
		<compilation debug="true" targetFramework="4.0" />
	</system.web>
	<system.serviceModel>
		<bindings>
			<basicHttpBinding>			
				<binding name="JianKunKingServiceBinding" closeTimeout="10:01:00"
                  openTimeout="10:01:00" receiveTimeout="10:10:00" sendTimeout="10:01:00"
                  allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                  maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                  messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                  useDefaultWebProxy="true">
					<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
              maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
					<security mode="None">
						<transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
						<message clientCredentialType="UserName" algorithmSuite="Default" />
					</security>
				</binding>
			</basicHttpBinding>
		</bindings>
		<client>
			<endpoint address="http://127.0.0.1:8088/JianKunKingService/SearchEngineDataBaseService.svc" 
			binding="basicHttpBinding" 
			bindingConfiguration="JianKunKingServiceBinding" 
			contract="DataBaseService.ISearchEngineDataBaseService" />
		</client>
		<behaviors>
			<serviceBehaviors>
				<behavior>
					<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
					<serviceMetadata httpGetEnabled="true" />
					<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
					<serviceDebug includeExceptionDetailInFaults="true" />
				</behavior>
			</serviceBehaviors>
		</behaviors>
		<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
	</system.serviceModel>
	<system.webServer>
		<modules runAllManagedModulesForAllRequests="true" />
		<directoryBrowse enabled="true" />
	</system.webServer>
</configuration>


目录
相关文章
|
10月前
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
9月前
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
39 0
|
9月前
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
64 0
|
10月前
Visual Studio 2022 创建 WCF服务 找不到
Visual Studio 2022 创建 WCF服务 找不到
|
C++
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
105 0
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF使用纯代码的方式进行服务寄宿
服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境。通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用,除去上面的两种寄宿方式,还可以以纯代码的方式实现服务的寄宿工作。
853 0
|
Windows
WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。整合了原有的windows通讯的 .net Remoting,WebService,Socket的机制,并融合有HTTP和FTP的相关技术。
1050 0
WCF服务自我寄宿
WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务、Windows服务、Winform程序、控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者方便、高效提供服务调用。
998 0
|
网络架构
(纯代码)快速创建wcf rest 服务
因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照wcf配置对象调试出来了: 1.创建WebHttpBinding 2.
981 0