定义一个WCF service类
[ServiceContract] public interface ISimpleService { [OperationContract] string SimpleMethod(string msg); } class SimpleService : ISimpleService { public string SimpleMethod(string msg) { Console.WriteLine("The caller passed in " + msg); return "Hello " + msg; } }
WCF 服务发布有两种方式1:config文件,2:代码发布
- 使用config来发布WCF,
- service结点 ? 各自的作用
- behavior结点
- endpoint结点 定义发布协议?
一个定义好的config文件:
<system.serviceModel>
<services>
<service
name="Microsoft.ServiceModel.Samples.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
</baseAddresses>
</host>
<!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service -->
<endpoint address=""
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
<!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
- 客户端接收的过程:
1:在config文件中指定server端url,
如<client>
<endpoint name=""
address="http://localhost:8000/servicemodelsamples/service"
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator">
</endpoint>
</client>
2:定义相对于服务器段的接口 然后实现这个接口,这个接口其实就是server类在客户端的代理,然后就可以在客户段使用代理类了
疑点:1: 服务--客户之间协议的选择