我正在尝试创建一个简单的ConsoleApplication,我希望在其中托管一个简单的wcf服务 .
这是我的代码
namespace HostConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(FirstWcfService.Service)))
{
host.Open();
Console.WriteLine("Sai");
Console.ReadLine();
}
}
}
}
然后我添加了一个app.config,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="FirstWcfService.Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="FirstWcfService" binding="netTcpBinding" contract="FirstWcfService.IService"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:9101/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior" >
<serviceMetadata httpGetEnabled="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
当我运行主机控制台应用程序时,我得到此异常:
System.InvalidOperationException未处理Message =“找不到与 endpoints 绑定MetadataExchangeHttpBinding的方案http匹配的基址 . 注册的基址方案为[net.tcp] . ” Source =“System.ServiceModel”StackTrace:在System.ServiceModel.ServiceHostBase.MakeAbsoluteUri(Uri relativeOrAbsoluteUri,绑定绑定,UriSchemeKeyedCollection baseAddresses)处于System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase主机,ServiceDescription描述,ServiceElement serviceElement,Action`1系统中的System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader,ServiceDescription描述,String configurationName)中的System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader,ServiceDescription description,ServiceElement serviceSection)处的SystemBay.ServiceModel.ServiceHostBase.ApplyConfiguration()处的addBaseAddress) System.ServiceModel.ServiceHost..ctor上的System.ServiceModel.ServiceHost.InitializeDescription(类型serviceType,UriSchemeKeyedCollection baseAddresses)中的.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) e serviceType,Uri [] baseAddresses)位于C:\ Documents and Settings \ navin.pathuru \ My Documents \ Visual Studio 2008 \ Projects \ Solution2 \ HostConsoleApplication \ Program.cs中的HostConsoleApplication.Program.Main(String [] args):line 13位于System.Threading的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()的System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)上的System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args) System.Threading.ThreadHelper.ThreadStart()中的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)中的.ThreadHelper.ThreadStart_Context(Object state):InnerException:
只是想知道如何解决这个问题 . 谢谢你
1 回答
好吧,我认为问题是这样的:
你有net.tcp的基地址
您已定义MEX http endpoints (但没有http基址)
基本上,如果要在http上使用MEX,则需要为MEX endpoints 提供 full address ,或者为http基址提供(如果只指定相对地址) .
Solution 1: 指定MEX endpoints 的完整地址:
Solution 2: 也定义了一个HTTP基地址:
Solution 3: 使用mexTcpBinding代替
这三个选项中的任何一个都应该解决它 .
需要注意的一点是:我发现调用服务行为配置“ServiceBehavior”是非常危险的......
我的建议:调用你的第一个和默认配置只是简单的“默认”(或“DefaultBehavior”)
如果你有多个配置,只开始给出其他名称 .
打电话给这个
ServiceBehavior
似乎在以后的某个时候要求麻烦......