参照webcast的一个实例想了解一下wcf开发Data Contract中的KnowType相关使用,结果源代码无法正常编译,看来看去可能是Service References没有更新,哪知道Service References下面的东西是localhost.map、localhost.cs,自己都不知道这个map为何物啊,(稍后还要再去学习一下这个东西),还是先自己重新创建一个实例工程慢慢来吧,折腾了许久才好,原来自己之前所谓会使用wcf都是在自欺欺人啊,所谓”知其然而不知其所以然“,失败....
这里给自己留一个”足迹“吧,等真正懂的时候也好知道自己原来是多”无知“,O(∩_∩)O~
WCF服务类库:WcfServiceLibrary
只有接口IGigManagerService和其实现类GigManagerService,没有配置文件
IGigManagerService.cs:
[ServiceContract(Name = "GigManagerServiceContract", Namespace = "http://www.thatindigogirl.com/samples/2006/06", SessionMode = SessionMode.Required)]
[ServiceKnownType(typeof(GigInfo))]
public interface IGigManagerService
{
[OperationContract]
[ServiceKnownType(typeof(GigInfo))]
void SaveGig(LinkItem item);
[OperationContract]
[ServiceKnownType(typeof(GigInfo))]
LinkItem GetGig();
}
宿主工程:Host(需要添加对WcfServiceLibrary的引用)
是console项目,有配置文件App.config和Program.cs,
App.config中主要的配置节:
<system.serviceModel>
<services>
<service name="WcfServiceLibrary1.GigManagerService" behaviorConfiguration="metadataBehaver" >
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/"/>
</baseAddresses>
</host>
<endpoint binding="wsHttpBinding" contract="WcfServiceLibrary1.IGigManagerService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehaver">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Program.cs:
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary1.GigManagerService)))
{
host.Open();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate Host");
Console.ReadLine();
}
}
web客户端:GigEntryWeb(添加服务引用GigManagerService)
配置文件Web.config的主要配置节:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_GigManagerServiceContract" >
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_GigManagerServiceContract"
contract="GigManagerService.GigManagerServiceContract" name="WSHttpBinding_GigManagerServiceContract">
<identity>
<userPrincipalName value="betty-PC\betty" />
</identity>
</endpoint>
</client>
</system.serviceModel>
客户端初始化:
GigManagerService.GigManagerServiceContractClient m_proxy = new GigManagerService.GigManagerServiceContractClient();//其中GigManagerService是服务引用名称