您的位置:首页 > 其它

WCF Part 4 : Make your service visible through metadata

2012-06-16 20:25 309 查看
Lasttimewesawhowwecouldcreateaninstanceofourservicebyhostingitusingsomeconfigurationinourapp.config.Westillneedtohaveitexposedusingmetadatathough.We'lldothisbyaddinganendpointthatexposedthis,usingourWCFABCagain.ThisendpointiscalledaMEXendpoint,fromMetadataEXchange.

Forthiswedon'thavetocreateanycode,justconfigurationagain.OpentheServiceConfigurationEditoragainonourapp.config.Openthefolder"Advanced",then"ServiceBehaviors"andchoosetoaddanewservicebehavior.We'llchangethenameNewBehaviortoHelloServiceBehavior.NowclicktheAddbuttonandselectthe'ServiceMetadata'option.



Nowwe'llconfigureournewbehaviorinandendpoint.Selectyourserviceagaininthetree.Thisissomethingyou'llprobablyforgetalotinthefuture,butyou'llhavetobindthejustconfiguredbehaviortoyourservice.YoushouldbeabletoselectitfromthelistintheBehaviorConfigurationpropertyonyourservice.

Nowlet'sactuallyaddourMEXendpoint.Selectthe"Endpoints"folderunderourserviceandright-clickit,thenchoosetoaddanewendpoint.Settheaddresstohttp://localhost:8080/HelloService/MEX/,selectforbindingthe"mexHttpBinding"andforcontractfillin"IMetadataExchange".Nowsave,exittheconfigurationeditorandyoushouldhavethefollowingconfiguration.

<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behaviorname="HelloServiceBehavior">
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<servicebehaviorConfiguration="HelloServiceBehavior"name="Classa.Wcf.Samples.Hello">
<endpointaddress="http://localhost:8080/HelloService/"binding="basicHttpBinding"
bindingConfiguration=""contract="Classa.Wcf.Samples.IHello"/>
<endpointaddress="http://localhost:8080/HelloService/MEX/"binding="mexHttpBinding"bindingConfiguration=""
contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
</configuration>
RunyourservicewithF5.You'llnoticenodifference,butnowyoucancreateaproxywiththeserviceutil.OpentheVisualStudioCommandPrompt(inyourstartmenuunderVisualStudio2005/VisualStudioTools)andexecutethefollowingcommand:

svcutil.exe/o:client.cs/config:app.confighttp://localhost:8080/HelloService/MEX/

Ifyou'reinterested,takealookatthegeneratedfiles.Especiallythegeneratedapp.config,whichshouldcontainanABCreferencetoyourservice.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: