WCF中如何用代码加入MEX元数据
配置代码如下:直接使用配置文件能正常运行!<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="ServiceImplementation.MessageService">
<host>
<baseAddresses>
<add baseAddress="net.Tcp://localhost:8732/"/>
<add baseAddress="http://localhost:8889/"/>
</baseAddresses>
</host>
<endpoint address="Tcp" binding="netTcpBinding" contract="Services.IMessage"/>
<endpoint address="Http" binding="basicHttpBinding" contract="Services.IMessage">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
使用代码如下:vs2012 net4.5
string tcpHostName = "localhost";
int tcpPort = 8732;
string tcpEndPstr = @"net.tcp://{0}:{1}";
tcpEndPstr = string.Format(tcpEndPstr, tcpHostName, tcpPort.ToString());
Uri tcpUri = new Uri(tcpEndPstr);
string httpHostName = "localhost";
int httpPort = 8889;
string httpEndPstr = @"http://{0}:{1}";
httpEndPstr = string.Format(httpEndPstr, httpHostName, httpPort.ToString());
Uri httpUri = new Uri(httpEndPstr);
Uri[] baseAddress = new Uri[] { httpUri, tcpUri };
using (ServiceHost host = new ServiceHost(typeof(MessageService), baseAddress))
{
host.AddServiceEndpoint(typeof(IMessage),
new BasicHttpBinding(),
"Services.IMessage");
host.AddServiceEndpoint(typeof(IMessage),
new NetTcpBinding(),
"Services.IMessage");
//******* 以下两句出现err: **** 若注释点以下两行,服务不正常,没有响应
//在服务 ServiceImplementation.MessageService 实现的协定列表中找不到协定名称 "IMetadataExchange"。
//将 ServiceMetadataBehavior 添加到配置文件或直接添加到 ServiceHost,以启用对该协定的支持。
host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
//交换方式
behavior.HttpGetEnabled = true;
//元数据交换地址
behavior.HttpGetUrl = httpUri;
//将行为添加到宿主上
host.Description.Behaviors.Add(behavior);
}
host.Open();
WCF 元数据 WCF MEX 元数据 代码配置 --------------------编程问答-------------------- 没人会吗?????? --------------------编程问答-------------------- 在app.config文件里加上:
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
补充:.NET技术 , C#