操作系统:Win7 旗舰版
开发环境:VS2010 旗舰版。
二、创建项目
1、创建WCF项目

如下图,创建wcf服务应用程序 wcfServiceDemo。

可以看到系统自动创建好了WCF Demo,分别创建契约接口IService1及以svc为后缀的服务Service1.svc。
服务契约 IService1代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfServiceDemo
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: 在此添加您的服务操作
}
// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
服务Service1实现此契约接口,代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfServiceDemo
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
2、运行
我们运行WcfServiceDemo项目,看到正常的界面如下:

ok,一切正常。
3、创建类库项目
现在创建wcf服务类库MyWcfServiceDemo,服务项目WcfServiceDemo引用此类库,并删除WcfServiceDemo中原有的文件IService.cs及文件Service1.svc.cs。
WCF类库如下所示,类库中的IService1.cs及文件Service1.cs如原WcfServiceDemo项目中的IService1.cs和Service1.cs一样,都是系统自动创建。

现在我们打开WcfServiceDemo项目Service1.svc文件,内容如下:
<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceDemo.Service1" CodeBehind="Service1.svc.cs" %>
此时我们已经删除了Service1.svc.cs文件,所以可以修改为:
<%@ ServiceHost Language="C#" Service="MyWcfServiceDemo.Service1" %>
修改完毕后保存下,右键该文件,选择【在浏览器中查看】,ok,浏览器中的界面如步骤【2】 下的一样,如果配置无误的话!
4、发布
完成第【3】步后是不是就大功告成了?No! 我们需要发布出去测试下服务是否好使?
现在我们打开项目WcfServiceDemo的配置文件Web.config,如下所示,我们发现信息并不完善,该如何完善服务端的配置项呢?
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<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>
大家可能注意到了,浏览Service1.svc界面时有这个提示:

ok,我们以管理员身份打开Visual studio 命令提示(2010)窗口,运行此命令。

找到output.config(应该应用到客户端配置)文件(看上图路径),参考其中的 <system.serviceModel>配置节并编辑WcfServiceDemo项目配置文件,如下所示:
Services(服务)配置节:

Bindings 绑定配置节:

behaviors行为配置节:

服务端配置文件完整的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="authenticationAudit">
<!--<serviceCredentials>
<windowsAuthentication includeWindowsGroups="true"/>
</serviceCredentials>
<serviceSecurityAudit auditLogLocation ="Application"
serviceAuthorizationAuditLevel ="SuccessOrFailure"/>-->
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings >
<wsHttpBinding>
<binding name="MyWcfServiceDemo_Binding"
closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false"
maxBufferPoolSize="52428800" maxReceivedMessageSize="6553600"
messageEncoding="Text" textEncoding="utf-8" hostNameComparisonMode="StrongWildcard"
useDefaultWebProxy="true" allowCookies="false" >
</binding>
</wsHttpBinding>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1"
closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false"
maxBufferPoolSize="52428800" maxReceivedMessageSize="6553600"
messageEncoding="Text" textEncoding="utf-8" hostNameComparisonMode="StrongWildcard"
useDefaultWebProxy="true" allowCookies="false" >
</binding>
</basicHttpBinding>
</bindings>
<services >
<service name="MyWcfServiceDemo.Service1" behaviorConfiguration="authenticationAudit">
<host>
<baseAddresses>
<add baseAddress="http://localhost:19751/Service1.svc" />
</baseAddresses>
</host>
<endpoint address="" bindingName="MyWcfServiceDemo_Binding" binding="wsHttpBinding" name="WCFServiceDemo"
contract="MyWcfServiceDemo.IService1">
</endpoint>
<endpoint address="basic" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="MyWcfServiceDemo.IService1"
name="BasicHttpBinding_IService1" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
ok,一切配置成功后,在浏览器中浏览Service1.svc,一切正常!
现在我们已经会将WCF应用程序逻辑通过WCf类库封装,并成功配置服务端文件!
293

被折叠的 条评论
为什么被折叠?



