WCF中的服务引用根源

导航:(一)WCF之路起航

      (二)WCF端口号问题的解决办法

    (三)WCF添加服务引用时找不到应用

   (四)WCF中的服务引用根源

Q:WCF中添加服务引用是要通过一个地址去找到服务的,像下图这样:

服务引用

刚开始的很疑惑服务中的这个名字(AppointmentServiceImepletation)是怎么找到呢? 而且我在哪里定义的这个服务名称呢?

仔细想了一下,其实这个名字是通过命名空间+类名找到的,在定义服务的时候已经在配置文件中设置过了:

 1 <?xml version="1.0" encoding="utf-8"?>
2 <configuration>
3
4 <system.web>
5 <compilation debug="true" targetFramework="4.0" />
6 </system.web>
7 <system.serviceModel>
8 <services>
9 <service behaviorConfiguration="AppointmentServiceIISHost.ServiceBehavior"
10 name="com.contoso.AppointmentService.AppointmentServiceImplementation">
11 <endpoint binding="wsHttpBinding" bindingConfiguration="" name="wsHttpBinding"
12 contract="com.contoso.AppointmentService.AppointmentServiceContract" />
13 </service>
14 </services>
15 <behaviors>
16 <serviceBehaviors>
17 <behavior name="AppointmentServiceIISHost.ServiceBehavior">
18 <serviceMetadata httpGetEnabled="true" />
19 <serviceDebug includeExceptionDetailInFaults="false" />
20 </behavior>
21 </serviceBehaviors>
22 </behaviors>
23 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
24 </system.serviceModel>
25 <system.webServer>
26 <modules runAllManagedModulesForAllRequests="true"/>
27 </system.webServer>
28
29 </configuration>

最明显的这一行就是我们之前的定义,在添加服务引用的时候只显示名字,这个类是在约定的类中定义的

1 namespace com.contoso.AppointmentService
2 {
3 public class AppointmentServiceImplementation : IAppointmentServiceContract
4 {}
5 }


现在总算知道一点原理了,但是找到这个引用该怎么使用呢?

在这之前我的服务已经写好了,运行起来效果还不错哦:

查看写好服务

按照这个网页上显示的做法去执行 svcutil.exe 会自动生成两个文件:AppointmentServiceImplementation.cs和output.config,就和上述服务说明的一样,然后就在Client调用吧!

原文地址:https://www.cnblogs.com/distance/p/2307198.html