一个服务端配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="netTcpBindingConfiguration">
<readerQuotas maxDepth="64" maxStringContentLength="2147483647 " maxArrayLength="2147483647 " maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
</security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="Services.MyService" behaviorConfiguration="metadataBehavior">
<endpoint address="MyService" binding="netTcpBinding" contract="Contracts.IService" bindingConfiguration="netTcpBindingConfiguration" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:9999/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
上面是WCF服务端配置文件,添加了2个终结点,一个采用的是netTcpBinding协议,一个采用的是mexTcpBinding协议。
那传输的时候,到底使用的是哪个协议啊?address为什么写成mex啊?
------解决思路----------------------
1、一个wcf服务可以有多个终节点,你可以认为终节点就是服务的接口,你这里配置了两个终结点,就意味着你的服务实现两个服务接口,同时两种binding可以提供两种不同的方式访问。
2、address="mex"这其实是一个相对地址,相对的是基地址localhost:9999/,所以他真实的完整地址是localhost:9999/mex。
3、从address="mex"这一地址的契约设定为"IMetadataExchange"来看,这一地址专门提供了一个元数据交换的服务契约接口,通过这一地址服务发布元数据。
------解决思路----------------------
1、一个wcf服务可以有多个终节点,你可以认为终节点就是服务的接口,你这里配置了两个终结点,就意味着你的服务实现两个服务接口,同时两种binding可以提供两种不同的方式访问。
2、address="mex"这其实是一个相对地址,相对的是基地址localhost:9999/,所以他真实的完整地址是localhost:9999/mex。
3、从address="mex"这一地址的契约设定为"IMetadataExchange"来看,这一地址专门提供了一个元数据交换的服务契约接口,通过这一地址服务发布元数据。
------解决思路----------------------
mexTcpBinding是看接口信息的,是由.net帮你实现的。
你自己那个是<endpoint?address="MyService"??binding="netTcpBinding"?contract="Contracts.IService"?b
------解决思路----------------------
添加服务的时候需要知道这个服务的方法名称,参数等等,是描述服务的,由mex提供。应该不影响调用你的服务http://localhost:9999/MyService。
调用服务使用的是MyService endpoint.
获得服务描述信息的时候用的是mex endpoint.
------解决思路----------------------
你说的找不到服务是说调用时找不到,还是添加引用时找不到?
------解决思路----------------------
难道net.tcp不一样?记得http的输入.svc的地址就可以了。
------解决思路----------------------
添加引用用到的是mex那个endpoint
------解决思路----------------------
添加引用其实就是visual studio帮你生成代理类,然后,你就可以像使用本地class那样使用web service/wcf. WCF/web service本身并不能描述自己,因此会有wsdl这种标准来描述web service. 而mex是比wsdl更新的描述定义。
添加Service reference...就是根据wsdl/mex对于service 描述生成代理类。真正调用service的时候还是调用MyService那个endpoint.
我这边创建了一个新的WCF 工程做了下测试:
1. 删除mex endpoint和设置serviceMetadata为false
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false"/> 这个是生成wsdl的配置。
启动 WCF Test Client, 得到下面的错误
Error: Cannot obtain Metadata from http://localhost:63246/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:63246/Service1.svc Metadata contains a reference that cannot be resolved: 'http://localhost:63246/Service1.svc'. Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:63246/Service1.svc. The client and service bindings may be mismatched. The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..HTTP GET Error URI: http://localhost:63246/Service1.svc The HTML document does not contain Web service discovery information.
2. enable mex 和serviceMetadata任何一个
启动WCF Test client, 没有任何问题,任何一个WCF方法都可以调用成功
3. a enable mex 和serviceMetadata
b启动WCF Test client, 这时候调用方法没有问题
c 不要关闭WCF Test Client, . 删除mex endpoint和设置serviceMetadata为false
d 调用WCF方法,依旧可以成功。
这是因为客户端的代理类已经在步骤b生成完成,即使步骤c删除了mex和serviceMetadata, wcf service本身依然可用。
希望这个解释能让你明白mex的作用,你也可以自己创建个测试工程把玩一下。