您的位置:首页 > 编程语言 > ASP

How to: Configure WCF Service to Interoperate with ASP.NET Web Service Clients

2008-01-09 11:17 591 查看
ToconfigureaWindowsCommunicationFoundation(WCF)serviceendpointtobeinteroperablewithASP.NETWebserviceclients,usetheSystem.ServiceModel.BasicHttpBindingtypeasthebindingtypeforyourserviceendpoint.

YoucanoptionallyenablesupportforHTTPSandtransport-levelclientauthenticationonthebinding.ASP.NETWebserviceclientsdonotsupportMTOMmessageencoding,sotheSystem.ServiceModel.BasicHttpBinding.MessageEncodingpropertyshouldbeleftasitsdefaultvalue,whichisSystem.ServiceModel.WSMessageEncoding.Text.ASP.NetWebServiceclientsdonotsupportWS-Security,sotheSystem.ServiceModel.BasicHttpBinding.SecurityshouldbesettoTransport.

TomakethemetadataforaWCFserviceavailabletoASP.NETWebserviceproxygenerationtools(thatis,WebServicesDescriptionLanguageTool(Wsdl.exe),WebServicesDiscoveryTool(Disco.exe),andtheAddWebReferencefeatureinVisualStudio),youshouldexposeanHTTP/GETmetadataendpoint.

ToaddaWCFendpointthatiscompatiblewithASP.NETWebserviceclientsincode

CreateanewBasicHttpBindinginstance

OptionallyenabletransportsecurityforthisserviceendpointbindingbysettingthesecuritymodeforthebindingtoTransport.Fordetails,pleaseseeTransportSecurity.

Addanewapplicationendpointtoyourservicehostusingthebindinginstancethatyoujustcreated.Fordetailsabouthowtoaddaserviceendpointincode,seetheHowto:CreateaServiceEndpointinCode.

EnableanHTTP/GETmetadataendpointforyourservice.FordetailsseeHowto:PublishMetadataforaServiceUsingCode.

ToaddaWCFendpointthatiscompatiblewithASP.NETWebserviceclientsinaconfigurationfile

CreateanewBasicHttpBindingbindingconfiguration.Fordetails,seetheHowto:SpecifyaServiceBindinginConfiguration.

OptionallyenabletransportsecurityforthisserviceendpointbindingconfigurationbysettingthesecuritymodeforthebindingtoTransport.Fordetails,seeTransportSecurity.

Configureanewapplicationendpointforyourserviceusingthebindingconfigurationthatyoujustcreated.Fordetailsabouthowtoaddaserviceendpointinaconfigurationfile,seetheHowto:CreateaServiceEndpointinConfiguration.

EnableanHTTP/GETmetadataendpointforyourservice.FordetailsseetheHowto:PublishMetadataforaServiceUsingaConfigurationFile.

Example

ThefollowingexamplecodedemonstrateshowtoaddaWCFendpointthatiscompatiblewithASP.NETWebserviceclientsincodeandalternativelyinconfigurationfiles.

C#


CopyCode

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Description;
[ServiceContract]
publicinterfaceIEcho
{
[OperationContract]
stringEcho(strings);
}
publicclassMyService:IEcho
{
publicstringEcho(strings)
{
returns;
}
}
classProgram
{
staticvoidMain(string[]args)
{
stringbaseAddress="http://localhost:8080/wcfselfhost/";
ServiceHosthost=newServiceHost(typeof(MyService),newUri(baseAddress));
//CreateaBasicHttpBindinginstance
BasicHttpBindingbinding=newBasicHttpBinding();
//Addaserviceendpointusingthecreatedbinding
host.AddServiceEndpoint(typeof(IEcho),binding,"echo1");
host.Open();
Console.WriteLine("Servicelisteningon{0}...",baseAddress);
Console.ReadLine();
host.Close();
}
}


Xml


CopyCode

<configuration>
<system.serviceModel>
<services>
<servicename="MyService"behaviorConfiguration="HttpGetMetadata">
<endpointaddress="echo2"contract="IEcho"binding="basicHttpBinding"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behaviorname="HttpGetMetadata">
<serviceMetadatahttpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>


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