【问题标题】:Hosting a Simple Wcf Service in Console在控制台中托管一个简单的 Wcf 服务
【发布时间】:2010-02-12 11:51:32
【问题描述】:

我正在尝试创建一个简单的 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 是 unhandled Message="找不到 匹配方案 http 的基地址 对于具有绑定的端点 元数据交换HttpBinding。 注册的基地址方案是 [net.tcp]。”
来源="System.ServiceModel"
堆栈跟踪: 在 System.ServiceModel.ServiceHostBase.MakeAbsoluteUri(Uri relativeOrAbsoluteUri,绑定 绑定,UriSchemeKeyedCollection 基本地址) 在 System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase 主机,ServiceDescription 描述, 服务元素服务元素, 动作`1 addBaseAddress) 在 System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader 配置加载器,服务描述 描述,服务元素 服务部分) 在 System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader 配置加载器,服务描述 描述,字符串配置名称) 在 System.ServiceModel.ServiceHostBase.ApplyConfiguration() 在 System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection 基本地址) 在 System.ServiceModel.ServiceHost.InitializeDescription(类型 服务类型,UriSchemeKeyedCollection 基本地址) 在 System.ServiceModel.ServiceHost..ctor(类型 serviceType, Uri[] baseAddresses) 在 HostConsoleApplication.Program.Main(字符串 [] args) 在 C:\Documents 和 设置\nav​​in.pathuru\我的 文档\Visual Studio 2008\Projects\Solution2\HostConsoleApplication\Program.cs:line 13 在 System.AppDomain._nExecuteAssembly(程序集 程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 汇编文件,证据 装配安全,字符串 [] 参数) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象 状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext 执行上下文,上下文回调 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:

只是想知道如何解决这个问题。 谢谢 否

【问题讨论】:

  • 您需要突出显示您的代码行和 XML,并使用编辑器工具栏中的“代码”按钮 (101 010) 以获得良好的结果 - 格式正确并突出显示语法!

标签: wcf console


【解决方案1】:

嗯,我认为问题是这样的:

  • 你有一个 net.tcp 的基地址
  • 您定义了 MEX http 端点(但没有 http 基地址)

基本上,如果您想通过 http 使用 MEX,您需要为 MEX 端点提供一个完整地址,或者一个 http 基地址(如果您只指定一个相对地址)。

解决方案 1: 指定 MEX 端点的完整地址:

 <services>
    <service name="FirstWcfService.Service" 
              behaviorConfiguration="ServiceBehavior">
       <endpoint 
           address="FirstWcfService" 
           binding="netTcpBinding" 
           contract="FirstWcfService.IService"/>
       <endpoint 
           address="http://localhost:9102/FirstWcfService/mex"
           binding="mexHttpBinding" 
           contract="IMetadataExchange"  />
        ......
    </service>
</services>

解决方案 2:也定义一个 HTTP 基地址:

 <services>
    <service name="FirstWcfService.Service" 
              behaviorConfiguration="ServiceBehavior">
       <endpoint 
           address="FirstWcfService" 
           binding="netTcpBinding" 
           contract="FirstWcfService.IService"/>
       <endpoint 
           address="mex"
           binding="mexHttpBinding" 
           contract="IMetadataExchange"  />
       <host>
           <baseAddresses>
               <add baseAddress="net.tcp://localhost:9101/"/>
               <add baseAddress="http://localhost:9102/"/>
           </baseAddresses>
       </host>
    </service>
</services>

解决方案 3: 改用 mexTcpBinding

 <services>
    <service name="FirstWcfService.Service" 
              behaviorConfiguration="ServiceBehavior">
       <endpoint 
           address="FirstWcfService" 
           binding="netTcpBinding" 
           contract="FirstWcfService.IService"/>
       <endpoint 
           address="mex"
           binding="mexTcpBinding" 
           contract="IMetadataExchange"  />
        ......
    </service>
</services>

这三个选项中的任何一个都应该解决它。

请注意:我觉得将您的服务行为配置称为“ServiceBehavior”是非常冒险的......

<serviceBehaviors>
    <behavior name="ServiceBehavior" >

我的建议:将您的第一个默认配置直接称为“默认”(或“默认行为”)

<serviceBehaviors>
    <behavior name="Default" >

并且只有在您有多个配置时才开始给出其他名称。

调用这个ServiceBehavior 似乎是在稍后自找麻烦.....

【讨论】:

  • 太好了,谢谢马克。我已将另一个控制台应用程序作为客户端添加到此解决方案中。您能告诉我如何使用 ConsoleHost 中的这项服务吗?谢谢N
  • 使用来自 cmd.exe 的服务启动您的第一个控制台;然后转到 Visual Studio,在您的客户端控制台应用程序中,从解决方案资源管理器上下文菜单中选择“添加服务引用”,然后输入(或粘贴)您的服务运行的 URL - 然后应该会发现该服务并创建必要的客户端位供您使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
相关资源
最近更新 更多